1

这就是我想要做的。我有一个 UILabel,但 UILabel 中的一个词应该是红色的。经过一番研究,我找到了TTTAttributedLabel 但我无法理解它。因为我的标签是多语言的,所以这很困难,因为他们都在使用 NSRange。

这是我的标签。

In dutch:
      Het 25m bad is vandaag **bezet** van 12:00 tot 15:00
In English
      The 25m pool is today **occupied** from 12:00 till 15:00

我需要以红色粗体显示的文本。

有谁能够帮我?

亲切的问候

4

2 回答 2

0

例如,您可以使用此代码段来获取NSRange“occupied”或“bezet”的位置:

NSRange occupiedRange = [str rangeOfString:NSLocalizedString(@"occupied", @"")];
if (occupiedRange.location == NSNotFound) {
    NSLog(@"Not found");
} else {
    ...
}
于 2013-04-09T10:33:54.950 回答
0

希望下面的代码片段对你有用

-(NSAttributedString*)configureToAttributedwithString:(NSString*)str
{
NSRange occupiedRange = [str rangeOfString:NSLocalizedString(@"occupied", @"")];
if (occupiedRange.location == NSNotFound)
{
    NSLog(@"Not found");
    NSAttributedString *attRStr = [[[NSAttributedString alloc] initWithString:str] autorelease];

    return (attRStr);
}
else
{
    NSString *string = [str substringToIndex:occupiedRange.location];

    str = [str substringFromIndex:occupiedRange.location];

    NSString *tarGetString = [str substringToIndex:occupiedRange.length];

    str = [str substringFromIndex:occupiedRange.length];

    CGColorRef colorRed = [[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0] CGColor];
    NSNumber *underline = [NSNumber numberWithInt:kCTUnderlineStyleSingle];
    CTFontRef sysUITargetFont = CTFontCreateUIFontForLanguage(kCTFontUIFontEmphasizedSystem,20.0, NULL);
    NSDictionary *attributesDictTarget = [NSDictionary dictionaryWithObjectsAndKeys:
                                          (id)underline, (id)kCTUnderlineStyleAttributeName,
                                          colorRed, (id)kCTForegroundColorAttributeName,
                                          colorRed, (id)kCTStrokeColorAttributeName,nil];

    CGColorRef colorBlack = [[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0] CGColor];
    CTFontRef sysUIDefaultFont = CTFontCreateUIFontForLanguage(kCTFontUIFontMessage,20.0, NULL);
    NSDictionary *attributesDictDefault = [NSDictionary dictionaryWithObjectsAndKeys:
                                           colorBlack, (id)kCTStrokeColorAttributeName,nil];

    NSMutableAttributedString *attMString = [[NSMutableAttributedString alloc] initWithString:string attributes:attributesDictDefault];

    NSAttributedString *stringToDraw = [[NSAttributedString alloc] initWithString:tarGetString
                                                                       attributes:attributesDictTarget];

    [attMString appendAttributedString:stringToDraw];

    NSAttributedString *stringRest = [[NSAttributedString alloc] initWithString:str
                                                                     attributes:attributesDictDefault];

    [attMString appendAttributedString:stringRest];

    NSLog(@"check %@", [attMString string]);

    return (attMString);
}
}
于 2013-04-09T12:13:27.070 回答