Iphone 同一标签中的不同颜色
这就是我想做的。我有一个UILabel,但其中一个单词应该是红色的。经过研究,我发现 但我不能让我的头在它周围。因为我的标签是多语言的,所以非常困难,因为它们都使用NSRange 这是我的标签Iphone 同一标签中的不同颜色,iphone,ios,objective-c,uilabel,Iphone,Ios,Objective C,Uilabel,这就是我想做的。我有一个UILabel,但其中一个单词应该是红色的。经过研究,我发现 但我不能让我的头在它周围。因为我的标签是多语言的,所以非常困难,因为它们都使用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 我需要红色的粗体文
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
我需要红色的粗体文本
有人能帮我吗
例如,您可以使用此代码段获取被占用的NSRange或边框:
NSRange occupiedRange = [str rangeOfString:NSLocalizedString(@"occupied", @"")];
if (occupiedRange.location == NSNotFound) {
NSLog(@"Not found");
} else {
...
}
希望下面的代码片段对您有用
-(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);
}
}
文本将是动态的?是的,它可以被占用或边框您想只更改被占用或边框的文本外观,或者它可能会根据文本的不同而变化?