2013-04-09 37 views
1

這是我想要做的。我有一個UILabel,但是UILabel的一個詞應該是紅色。經過一番研究後,我發現TTTAttributedLabel 但是我無法把它放在頭上。因爲我的標籤是多語言的,所以它非常困難,因爲它們都與NSRange一起工作。不同的顏色在一個UILabel

這是我的標籤。

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 

我需要在紅色粗體文字。

任何人都可以幫助我嗎?

親切的問候

+0

文字將動態? – Exploring 2013-04-09 10:22:38

+0

是的,它可以被佔用或bezet – Steaphann 2013-04-09 10:29:24

+0

你想改變文本的外觀只爲佔用或bezet或它可能會有所不同,取決於文本? – Exploring 2013-04-09 10:31:14

回答

0

例如,你可以使用這個片段來獲得NSRange其中「佔領」或「bezet」是:

NSRange occupiedRange = [str rangeOfString:NSLocalizedString(@"occupied", @"")]; 
if (occupiedRange.location == NSNotFound) { 
    NSLog(@"Not found"); 
} else { 
    ... 
} 
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); 
} 
}