【发布时间】:2012-10-19 02:13:21
【问题描述】:
在使用 NSAttributedString 时,我遇到了 UITextView 的一些奇怪行为。假设我有两个属性:
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UITextView *textView;
在这些属性的拥有控制器中,我有以下代码:
NSDictionary *attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:20.],
NSForegroundColorAttributeName: [UIColor redColor]};
NSAttributedString *as = [[NSAttributedString alloc] initWithString:@"Hello there!" attributes:attributes];
NSMutableAttributedString *mas = [[NSMutableAttributedString alloc] initWithString:@"Hello where?" attributes:nil];
[mas addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(3, 5)];
self.label.attributedText = as;
self.label.attributedText = mas;
self.textView.attributedText = as;
self.textView.attributedText = mas;
在模拟器中运行时,标签看起来(呃,发挥你的想象力)如下,使用系统默认字体:
<black>Hel</black><yellow>lo wh</yellow><black>ere?</black>
文本视图如下,使用20.0大小的系统字体:
<red>Hel</red><yellow>lo wh</yellow><red>ere?</red>
似乎文本视图正在组合来自两个属性字符串的属性。我发现这是一个令人惊讶的结果,并希望它表现得像标签一样。
我怀疑这是一个错误。如果不是,那么 UITextView 如何以及为什么以与 UILabel 不同的方式对待属性文本?
(XCode 版本 4.5.1)
【问题讨论】:
-
投票结束,因为问题是“为什么 API 是这样的”,没有人能回答这个问题。 API就是这样。我已经解释了如何使用它来解决您描述的行为;这就是任何人都可以做的。
-
因不具建设性而关闭???版主的权利必须受到限制。
标签: ios uilabel uitextview nsattributedstring