我在UITextview
您好! 右上方的汽车是您设置汽车规格的地方 - 不用担心,只需30秒,您只需要设置一次!
我想:
我该怎么做?我对NSAttributedString
知之甚少,对文档也没有好运。
谢谢你
答案 0 :(得分:2)
答案 1 :(得分:1)
创建Mutable Attributed字符串,执行以下步骤
NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] initWithString:yourTextView.text];
1)找到Hello!
范围并设置字体大小
NSRange foundRange = [attrString rangeOfString:@"Hello!"];
if (foundRange.location != NSNotFound)
{
[attrString beginEditing];
[attrString addAttribute: NSFontAttributeName
value:[[UIFont boldSystemFontOfSize:17] fontName]
range:boldedRange];
[attrString endEditing];
}
2)only 30 seconds
的搜索范围并设置下划线样式
foundRange = [attrString rangeOfString:@"only 30 seconds"];
if (foundRange.location != NSNotFound)
{
[attrString beginEditing];
[attrString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:1] range:foundRange];
[attrString endEditing];
}
3)once!
的搜索范围并设置描边颜色。
foundRange = [attrString rangeOfString:@"once!"];
if (foundRange.location != NSNotFound)
{
[attrString beginEditing];
[attrString addAttribute:NSStrokeColorAttributeName value:[UIColor redColor] range:foundRange];
[attrString endEditing];
}
最后将属性字符串设置为textview
yourTextView.attributedText = attrString;
注意:上述内容仅适用于ios6+.
。