私の環境:Xcode 4.5.2、iOS 6.0 SDK
最初に、IB を使用して UITextView と Attribuded String で簡単なプロジェクトを作成しました。
次に、iOS シミュレーターを実行すると、正常に動作します。
第二に、コード化による属性の使用を試みました。コードはこんな感じ。
ViewController.h:
@property (strong, nonatomic) IBOutlet UITextView *textView;
ViewController.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: メソッドは正常に動作するようです。しかし、表示されません...なぜですか?
ありがとう。