【问题标题】:Why wont [NSTextStorage appendAttributedString: ] accept my NSAttributedString's attributes?为什么 [NSTextStorage appendAttributedString: ] 不接受我的 NSAttributedString 的属性?
【发布时间】:2012-03-30 18:00:32
【问题描述】:

我正在尝试将具有特定字体和颜色的文本放入 NSTextView。我可以把文字放进去就好了,但它失去了我的属性:

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSFont fontWithName:@"Monaco" size:10], @"NSFontAttributeName",
    [NSColor blueColor],                     @"NSForegroundColorAttributeName",
    nil];
NSAttributedString *string = [[NSAttributedString alloc] initWithString:someNSString
                                                             attributes: dict];
[someNSTextStorage appendAttributedString:string];

我已经阅读了所有相关的类引用(NSTextView、NSTextStorage、NSAttributedString 等...),但我仍然不知道为什么它不能按预期工作。

我做错了什么/错过了什么?

【问题讨论】:

  • 我仍然不确定问题出在哪里,但我可以通过在 appendAttributedString 之后使用 [storage setForegroundColor:[NSColor blueColor]];[storage setFont:[NSFont fontWithName:@"Monaco" size:10]]; 来解决它:

标签: objective-c cocoa


【解决方案1】:

发现您的问题:您已将NSForegroundColorAttributeName 括在引号中。因此,您传递的不是宏,而是 NSString 文字。你的NSFontAttributeName 也一样。摆脱那些,你就准备好了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多