【问题标题】:NSTextView and NSAttributedStringNSTextView 和 NSAttributedString
【发布时间】:2013-07-16 16:54:16
【问题描述】:

我正在尝试将属性字符串粘贴到我的 NSTextView 中,但它只是显示为纯文本,没有属性。我像这样创建了我的字符串:

    NSString *str = @"Parsing Directory Structure\n\n";

NSMutableAttributedString *attrstr = [[NSMutableAttributedString alloc] initWithString:str];
NSDictionary *attributes = @{
                             NSForegroundColorAttributeName : [NSColor blueColor],
                             NSFontAttributeName : [NSFont fontWithName:@"HelveticaNeue-Bold" size:20.f]
                             };
[attrstr setAttributes:attributes range:NSRangeFromString(str)];

[[self.textView textStorage] appendAttributedString:attrstr];

在 NSTextView(滚动视图内)上,我仍然选中了“允许富文本”框,并且未选中“可编辑”框。我基本上是在尝试像控制台输出窗口一样使用它。

【问题讨论】:

    标签: cocoa nstextview nsattributedstring


    【解决方案1】:

    NSMakeRangeFromString 解析范围的文本表示,它不会创建覆盖字符串的范围。由于您的文本不包含整数,因此它返回范围 {0, 0} - 位置和长度均为零。因此,您的文本没有样式。

    替换为NSMakeRange(0, str.length),您的代码应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-16
      • 1970-01-01
      相关资源
      最近更新 更多