NSMutableAttributedText setAttributes:range: 方法没有将我的属性应用于它的文本

问题描述 投票:0回答:1

我目前正在尝试在我的 NSTextField 中使用 Apple 系统字体(旧金山) 来显示消息。我尝试了很多方法来实例化一个

NS(Mutable)AttributedText
的实例,其中包含4行单词,每行都是斜体,粗体或常规。

其中一些方法包括:

有一些其他的但这些似乎是最可行的。当我用 HTML 做它时它工作正常,但我只得到 Times New Roman 字体并且不能让它成为旧金山苹果公司的默认字体,即使通过任何 CSS。当我将

initWithRTF:
.rtf 文件的纯文本输出提供给 NSData 对象时,文本字段中没有任何输出。 URL 一个接受 .txt 文件而不是 .rtf 文件,所以这里也没有属性文本。

唯一的其他选择是使用

NSMutableAttributedText
类手动执行此操作,您可以在其中指定包含文本的
NSMutableAttributedText
对象中的字母范围(例如,将粗体应用于字母索引 0-11 NSMakeRange)。在这里我遇到了麻烦,我正在使用这个生成器网站为此给我 Objective-C 代码。

我正在将 Python 与 PyObj-C 结合使用,所以在这里告诉我...

# Create instance of NSMutableAttributedString with my text
attributedString = NSMutableAttributedString.alloc().initWithString_(
            f"Word - word\n/phonetic/\ndefinition\ntype\n")

# Create a centered paragraph style to apply centering to text
paragraphStyle0 = NSMutableParagraphStyle.alloc().init()
paragraphStyle0.setAlignment_(1) # 1 is center

# Create set of attributes for our first line
attributes = {
        "NSParagraphStyleAttributeName": paragraphStyle0,
        # Bolded style
        "NSFontAttributeName": NSFont.fontWithName_size_("HelveticaNeue-Bold", 17)
        }
# Apply the attributes to the first line (first 1 letters)
attributedString.setAttributes_range_(attributes, NSMakeRange(0, 11))
self.definitionField.setAttributedStringValue_(attributedString)

(等效于 Objective-C)

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString: @"Word - word\n/phonetic/\na word\nnoun\n"];

NSMutableParagraphStyle *paragraphStyle0 = [[NSMutableParagraphStyle alloc] init];
paragraphStyle0.alignment = NSTextAlignmentCenter;

NSDictionary <NSAttributedStringKey, id> *attributes0 = @{
   NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Bold" size:13],
   NSParagraphStyleAttributeName: paragraphStyle0
};
[attributedString addAttributes:attributes0 range: NSMakeRange(0, 11)];

如果我打印我的

attributedString
对象,我得到这个:

Word - word{
    NSFontAttributeName = "\"HelveticaNeue-Bold 17.00 pt. P [] (0x1057b75a0) fobj=0x1057b6d30, spc=4.73\"";
    NSParagraphStyleAttributeName = "Alignment 2, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n    28L,\n    56L,\n    84L,\n    112L,\n    140L,\n    168L,\n    196L,\n    224L,\n    252L,\n    280L,\n    308L,\n    336L\n), DefaultTabInterval 0, Blocks (\n), Lists (\n), BaseWritingDirection -1, HyphenationFactor 0, TighteningForTruncation YES, HeaderLevel 0 LineBreakStrategy 0 PresentationIntents (\n) ListIntentOrdinal 0 CodeBlockIntentLanguageHint ''";
}
/phonetic/
definition
type
{
}

当我将它放入启用富文本的文本字段中时,我得到了这个(未更改的文本没有任何反应......)

我不知道为什么第一行(前 12 个字母)没有加粗,我一定是做错了什么……关于我可能遗漏了什么有什么建议吗?

python objective-c cocoa pyobjc
1个回答
0
投票

第一个评论者解决了这个问题。我在字典中的属性键是错误的。

我必须删除键末尾的

AttributeName
,这些属性对我有用!

attributes = {
    "NSParagraphStyleAttributeName": paragraphStyle0,
    "NSFontAttributeName": NSFont.fontWithName_size_("HelveticaNeue-Bold", 17)
}
attributes = {
    "NSParagraphStyle": paragraphStyle0,
    "NSFont": NSFont.fontWithName_size_("HelveticaNeue-Bold", 17)
}
© www.soinside.com 2019 - 2024. All rights reserved.