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
kCTFontAttributeNamemust be aCTFontRef, not aUIFont *. You can’t directly convert aUIFontto aCTFont. You should be able to just useCTFontCreateWithNameto create it. You will need to useCFReleaseon it when you are done, to avoid a memory leak. Check out theCTFontReference.Also, the value for
kCTForegroundColorAttributeNamemust be aCGColorRef, not aUIColor *. You can fix this easily by saying[UIColor blackColor].CGColor.UPDATE
If you’re using UIKit attributed string support (which was added in iOS 6.0), you can use the
NSFontAttributeNamekey with aUIFontas the value. You can also use theNSForegroundColorAttributeNamekey with aUIColorvalue. See NSAttributedString UIKit Additions Reference.