以下代码将字符串 @"10 的 2 次方 = 10[2]" 并将红色设置为数字 2
问:我想要这个(花哨的)效果:
(请注意,我正在寻找一个代表一个小的、略高于行号的通用效果 - 就在一个字符串之后,不一定是“到...的力量”,但它应该看起来像上图):
-(void)textAttribute
{
NSString *myString = @"ten to the power of two = 10[2]";
NSRange start = [myString rangeOfString:@"["];
NSRange end = [myString rangeOfString:@"]"];
if (start.location != NSNotFound && end.location != NSNotFound && end.location > start.location) {
//NSString *betweenBraces = [myString substringWithRange:NSMakeRange(start.location+1, end.location-(start.location+1))];
NSRange myRange = NSMakeRange(start.location+1, end.location-(start.location+1));
NSMutableAttributedString *s =
[[NSMutableAttributedString alloc] initWithString:myString];
UIColor *powerColor = [UIColor redColor];
[s addAttribute:NSForegroundColorAttributeName value:powerColor range:myRange];
triggerLabel.text = [NSString stringWithFormat:@"Trigger words:%@",powerColor];
triggerLabel.attributedText = s;
}
}