Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSMutableAttributedString set font size

Tags:

xcode

macos

Every resource I keep coming across for this is for iOS. I can't seem to get this working properly for Mac OSX. Any idea how to get this to set the font size as well? It currently sets the color and center alignment properly. Thanks

NSMutableAttributedString *libTitle = [[NSMutableAttributedString alloc] initWithString:@"Library"];
[libTitle addAttribute:NSForegroundColorAttributeName value:[NSColor whiteColor] range:NSMakeRange(0,[@"Library" length] ) ];
[libTitle setAlignment:NSCenterTextAlignment range:NSMakeRange(0, [@"Library" length])];
[libTitle addAttribute:NSFontSizeAttribute value:[NSFont systemFontOfSize:18.0] range:NSMakeRange(0, [@"Library" length])];

[self.libbtn setAttributedTitle:libTitle];
like image 937
steven Avatar asked Feb 10 '14 20:02

steven


People also ask

How do I change font size in NSAttributedString?

Answers. You could convert NSAttributedString to NSMutableAttributedString , and then set the font attribute . If you want to set font size from Forms project , you could create a new subclass and create custom renderer for the new custom class .

How do I change font size in textView in Swift?

Swift 2 & 3: import UIKit extension UITextView { func increaseFontSize () { self. font = UIFont(name: (self. font?. fontName)!, size: (self.

How do I change the font in Xcode?

Add the Font File to Your Xcode Project To add a font file to your Xcode project, select File > Add Files to “Your Project Name” from the menu bar, or drag the file from Finder and drop it into your Xcode project. You can add True Type Font (. ttf) and Open Type Font (. otf) files.


1 Answers

Try setting your font attributes from the get go, in the init method:

NSFont *systemFont = [NSFont systemFontOfSize:18.0f];
NSDictionary * fontAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:systemFont, NSFontAttributeName, nil];
NSMutableAttributedString *libTitle = [[NSMutableAttributedString alloc] initWithString:@"Library" attributes:fontAttributes];
NSRange rangeOfTitle = NSMakeRange(0,[libTitle length]);
[libTitle addAttribute:NSForegroundColorAttributeName value:[NSColor whiteColor] range:rangeOfTitle];
[libTitle setAlignment:NSCenterTextAlignment range:rangeOfTitle];

[self.libbtn setAttributedTitle:libTitle];
like image 78
Michael Dautermann Avatar answered Nov 15 '22 08:11

Michael Dautermann