I currently am trying to attributed text in my NSTextField that uses the Apple system font (San Francisco) to show a message. I have tried many methods to instantiate an instance of a NS(Mutable)AttributedText that contains 4 lines of words, each are either italicized, bolded, or regular.
Some of these methods include:
There are some others ones but these seemed the most doable. When I do it with HTML it works fine, but I only get the Times New Roman font and cant have it be San Francisco apple's default one, even through any CSS. When I give initWithRTF: the plain text output of a .rtf file into a NSData object nothing outputs in the text field. The URL one takes .txt files but not .rtf files so no attributed text here either.
The only other option is do this manually with the NSMutableAttributedText class where you specify ranges of letters in a NSMutableAttributedText object containing your text (e.g. apply bold to index of letters 0-11 NSMakeRange). Here I am having trouble, I am using this generator website to give me Objective-C code for this.
I am using Python with PyObj-C so bare wit me here...
# 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 equivalent)
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)];
If I print my attributedString object I get this:
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
{
}
When I put it into the Rich Text enabled text field I get this (unchanged text nothing happened...)

I have no idea why the top line (first 12 letters) aren't bolded, I must being doing something wrong... Any suggestions as to what I may be missing?
The first commenter solved the problem. My keys in my dictionary for the attributes were wrong.
I had to remove the
AttributeNameat the end of my keys and the attributes worked for me!