NSTextView和NSAttributedString

Gar*_*yle 11 cocoa nstextview nsattributedstring

我试图将一个属性字符串粘贴到我的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];
Run Code Online (Sandbox Code Playgroud)

在NSTextView上(在滚动视图内)我已经选中了"允许富文本"框,并且未选中"可编辑"框.我基本上试图像控制台输出窗口一样使用它.

CRD*_*CRD 5

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

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