首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITextView替代

UITextView替代
EN

Stack Overflow用户
提问于 2012-12-18 12:01:44
回答 1查看 1.6K关注 0票数 1

正如我从苹果文档中了解到的,UITextView的UITextView属性:

默认情况下,此属性为零。为该属性分配一个新值也会用相同的字符串数据替换text属性的值,尽管没有任何格式化信息。此外,指定一个新的值将更新字体、textColor和textAlignment属性中的值,以便它们反映从属性化字符串的位置0开始的样式信息。

因此,我不能通过代码在不同位置添加具有多个属性的attributedString。

有人能帮我在iOS6中通过代码创建以下效果吗?这是可以使用nib文件和更改UITextView中的文本部分的范围属性,但我似乎不能通过代码再现效果。

<‘字体systemFontOfSize=18>Desired<'/font> 效果 <'textColor = UIColor redColor> ,以便编写b<'/textColor>y代码

假设标记对应于属性。

我不想使用CATextLayers,因为我试图在UITextView中使用AutoLayout特性。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-18 12:28:08

您可以使用包含所需所有属性的NSAttributedString来构建NSDictionary:

代码语言:javascript
复制
NSAttributedString* attString = [[NSAttributedString alloc] 
               initWithString: @"Desired effect to be written by code" 
                   attributes: (NSDictionary*)attributes];

或者使用它的可变子类NSMutableAttributedString:

代码语言:javascript
复制
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc]  
               initWithString: @"Desired effect to be written by code"];

    [attString addAttribute: NSForegroundColorAttributeName 
                      value: [UIColor redColor] 
                      range: NSMakeRange(15,15)];

...etc

然后..。

代码语言:javascript
复制
myTextView.attributedText = attString;

每个属性都应用于字符串的NSRange (位置、长度)。不同的属性类型(如颜色、字体大小)可以重叠不同的范围。

更新

使用NSMutableAttributedString示例。NSAttributedString's initWithString:attributes将在字符串的整个范围内应用属性,这正是您想要避免的,不好意思混淆了。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13932550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档