gpt4 book ai didi

cocoa - NSTextView 和 NSAttributedString

转载 作者:行者123 更新时间:2023-12-03 16:04:07 25 4
gpt4 key购买 nike

我试图将属性字符串粘贴到我的 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( ScrollView 内)上,我仍然选中“允许富文本”框,并且未选中“可编辑”框。我基本上尝试像控制台输出窗口一样使用它。

最佳答案

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

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

关于cocoa - NSTextView 和 NSAttributedString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17682559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com