我的环境:Xcode 4.5.2、iOS 6.0 SDK
起初,我使用 IB 用 UITextView 和 Attribuded String 做了一个简单的项目。 然后,运行 iOS 模拟器,工作正常。
其次,我尝试通过绳索使用属性。代码是这样的。
视图控制器.h:
@property (strong, nonatomic) IBOutlet UITextView *textView;
视图控制器.m:
[super viewDidLoad];
UIFont *font = [UIFont systemFontOfSize:14.0f];
NSMutableDictionary *attrsDictionary = [NSMutableDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[attrsDictionary setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
NSString *text = @"In iOS 6 and later, this class supports multiple text styles through use of the attributedText property.";
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text attributes:attrsDictionary];
NSRange range = NSMakeRange(3, 5); // range of "iOS 6"
[attributedText addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.262745 green:0.262745 blue:0.262745 alpha:1] range:range];
self.textView.attributedText = attributedText;
结果无法正常工作。
所以,我试图获取日志。
NSLog(@"%@", self.textView.attributedText);
In {
NSColor = "UIDeviceRGBColorSpace 0 0 0 1";
NSFont = "<UICFFont: 0x716eef0> font-family: \".Helvetica NeueUI\"; font-weight: normal; font-style: normal; font-size: 14px";
NSKern = 0;
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
NSStrokeColor = "UIDeviceRGBColorSpace 0 0 0 1";
NSStrokeWidth = 0;
}iOS 6{
NSColor = "UIDeviceRGBColorSpace 0.262745 0.262745 0.262745 1";
NSFont = "<UICFFont: 0x716eef0> font-family: \".Helvetica NeueUI\"; font-weight: normal; font-style: normal; font-size: 14px";
NSKern = 0;
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
NSStrokeColor = "UIDeviceRGBColorSpace 0.262745 0.262745 0.262745 1";
NSStrokeWidth = 0;
} and later, this class supports multiple text styles through use of the attributedText property.
...
似乎 addAttribute: 方法工作正常。但不显示...为什么?
谢谢。