1

我的环境: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: 方法工作正常。但不显示...为什么?

谢谢。

4

2 回答 2

0

我相信您对故事板的工作方式感到困惑......您在故事板中设置属性文本,然后尝试更改它的代码。根据我的经验......你做一个或另一个。我有一个表单,我在 Storyboard 中设置了带有属性的文本,它在模拟器和应用程序上都可以正常工作。但是,我不会试图覆盖我已经在情节提要中所做的事情。如果您在情节提要中创建了一些东西,它就完成了。

于 2013-05-26T09:21:10.313 回答
0

也许你需要在你的文本视图上调用 setNeedsDisplay ?

于 2012-11-29T17:42:28.467 回答