正如我从苹果文档中了解到的,UITextView的UITextView属性:
默认情况下,此属性为零。为该属性分配一个新值也会用相同的字符串数据替换text属性的值,尽管没有任何格式化信息。此外,指定一个新的值将更新字体、textColor和textAlignment属性中的值,以便它们反映从属性化字符串的位置0开始的样式信息。
因此,我不能通过代码在不同位置添加具有多个属性的attributedString。
有人能帮我在iOS6中通过代码创建以下效果吗?这是可以使用nib文件和更改UITextView中的文本部分的范围属性,但我似乎不能通过代码再现效果。
<‘字体systemFontOfSize=18>Desired<'/font> 效果 <'textColor = UIColor redColor> ,以便编写b<'/textColor>y代码。
假设标记对应于属性。
我不想使用CATextLayers,因为我试图在UITextView中使用AutoLayout特性。
发布于 2012-12-18 12:28:08
您可以使用包含所需所有属性的NSAttributedString来构建NSDictionary:
NSAttributedString* attString = [[NSAttributedString alloc]
initWithString: @"Desired effect to be written by code"
attributes: (NSDictionary*)attributes];或者使用它的可变子类NSMutableAttributedString:
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc]
initWithString: @"Desired effect to be written by code"];
[attString addAttribute: NSForegroundColorAttributeName
value: [UIColor redColor]
range: NSMakeRange(15,15)];...etc
然后..。
myTextView.attributedText = attString;每个属性都应用于字符串的NSRange (位置、长度)。不同的属性类型(如颜色、字体大小)可以重叠不同的范围。
更新
使用NSMutableAttributedString示例。NSAttributedString's initWithString:attributes将在字符串的整个范围内应用属性,这正是您想要避免的,不好意思混淆了。
https://stackoverflow.com/questions/13932550
复制相似问题