是否可以更改 UITextView 和 UITextField 中单个单词的颜色?
如果我输入了一个前面带有符号的单词(例如:@word),它的颜色可以更改吗?
是的,你需要使用NSAttributedString
为此,找到在这里运行应用程序.
扫描单词并找到单词的范围并更改其颜色。
EDIT:
- (IBAction)colorWord:(id)sender {
NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:self.text.text];
NSArray *words=[self.text.text componentsSeparatedByString:@" "];
for (NSString *word in words) {
if ([word hasPrefix:@"@"]) {
NSRange range=[self.text.text rangeOfString:word];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
}
}
[self.text setAttributedText:string];
}
EDIT 2 : see the screenshot
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)