How do I set the font for a NSAtributedString on the iPhone. I found online how to do it for the mac but it is not the same. When I tried to covert it to the iOS platform it didn't work. I need to set the font name and the font size.
NSDictionary *attributes = [[NSDictionary alloc] initWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica" size:26], kCTFontAttributeName,
[UIColor blackColor], kCTForegroundColorAttributeName, nil];
The value for kCTFontAttributeName
must be a CTFontRef
, not a UIFont *
. You can't directly convert a UIFont
to a CTFont
. You should be able to just use CTFontCreateWithName
to create it. You will need to use CFRelease
on it when you are done, to avoid a memory leak. Check out the CTFont
Reference.
Also, the value for kCTForegroundColorAttributeName
must be a CGColorRef
, not a UIColor *
. You can fix this easily by saying [UIColor blackColor].CGColor
.
If you're using UIKit attributed string support (which was added in iOS 6.0), you can use the NSFontAttributeName
key with a UIFont
as the value. You can also use the NSForegroundColorAttributeName
key with a UIColor
value. See NSAttributedString UIKit Additions Reference.