是否可以在UITextView和UITextField中更改单个单词的颜色 [英] Is it possible to change color of single word in UITextView and UITextField
本文介绍了是否可以在UITextView和UITextField中更改单个单词的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在UITextView和UITextField中更改单个单词的颜色?
Is it possible to change color of single word in UITextView and UITextField ?
如果我输入了带有符号前面的单词(例如:@word),则可以它的颜色会改变吗?
If i have typed a word with a symbol infront (eg: @word) , can it's color be changed ?
推荐答案
是的,你需要使用 NSAttributedString
,找到 RunningAppHere 。
Yes you need to use NSAttributedString
for that, find the RunningAppHere.
扫描单词并找到单词的范围并更改其颜色。
Scan through the word and find the range of your word and change its color.
编辑:
- (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];
}
编辑2:看截图
EDIT 2 : see the screenshot
这篇关于是否可以在UITextView和UITextField中更改单个单词的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文