1

私の環境: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: メソッドは正常に動作するようです。しかし、表示されません...なぜですか?

ありがとう。

4

2 に答える 2

0

たぶん、テキストビューでsetNeedsDisplayを呼び出す必要がありますか?

于 2012-11-29T17:42:28.467 に答える
0

ストーリーボードの仕組みを混乱させていると思います...ストーリーボードに属性付きのテキストを設定し、そのコードを変更しようとしています。私の経験では...どちらかを行います。属性付きテキストを使用してストーリーボードに設定したフォーム シートがあり、シミュレーターとアプリの両方で正常に機能します。しかし、ストーリーボードで既に行ったことを上書きしようとはしません。ストーリーボードで何かを作成すれば完了です。

于 2013-05-26T09:21:10.313 に答える