Objective-C – CTFont change font style?
I have a CTFont that contains a font style, and sumbolic traits.
I want to create a new font with a new style that inherits the symbolic traits of the first font.
How can I achieve this?
CTFontRef newFontWithoutTraits = CTFontCreateWithName((CFString)newFontName, CTFontGetSize(font), NULL); CTFontRef newFont = CTFontCreateCopyWithSymbolicTraits(newFontWithoutTraits, CTFontGetSize(font), NULL, 0, CTFontGetSymbolicTraits(font));
the new font is null here
I don’t know what should I pass to the
4th parameter in
Solutions Collect From Internet About “Objective-C – CTFont change font style?”
I do this line of code to generate a bold font from non-bold font:
CTFontRef newFont = CTFontCreateCopyWithSymbolicTraits(currentFont, 0.0, NULL, (wantBold?kCTFontBoldTrait:0), kCTFontBoldTrait);
CTFontRefI want to add symbolic traits to
wantBoldis a boolean to tell if I want to add or remove the bold trait to the font
kCTFontBoldTraitis the symbolic trait I want to modify on the font.
The 4th parameter is the value you want to apply. The 5th is the mask to select the symbolic trait.
You may thing of it as bitmask to apply to the symbolic trait, where the 4th parameter of
CTFontCreateCopyWithSymbolicTraits is the value and the 5th parameter is the mask:
- If you want to set the symtrait and add it to the font, iOS will probably apply sthg like
newTrait = oldTrait | (value&mask), setting the bit corresponding to
maskto the value of
If you want to unset the symtrait and remove it from the font, you use the value of 0 as the 4th parameter and iOS will probably apply sthg like
newTrait = oldTrait & ~maskto unset the bit.
But if you need to, you can also set and unset multiple bits (thus multiple symbolic traits) at once, using the right
valuethat have 1 on bits to set and 0 on bits to unset (or to ignore), and and using the right
maskthat have 1 on bits that needs to be modified and 0 on bits that don’t need to be changed.
I finally managed to find the solution for your specific case: you need to get the symtraits mask of your
font as you already do… and bitwise-or it with the symtraits of your
This is because
newFontWithoutTraits actually do have default symtraits (contrary to what I thought, it has a non-zero
CTFontSymbolicTraits value) as the symtraits value also contains info for the font class and such things (so even a non-bold, non-italic font can have a non-zero symtraits value, log the hex value of the symtraits of your font to understand better).
So this is the code you need:
CTFontRef font = CTFontCreateWithName((CFStringRef)@"Courier Bold", 12, NULL); CGFloat fontSize = CTFontGetSize(font); CTFontSymbolicTraits fontTraits = CTFontGetSymbolicTraits(font); CTFontRef newFontWithoutTraits = CTFontCreateWithName((CFStringRef)@"Arial", fontSize, NULL); fontTraits |= CTFontGetSymbolicTraits(newFontWithoutTraits); CTFontRef newFont = CTFontCreateCopyWithSymbolicTraits(newFontWithoutTraits, fontSize, NULL, fontTraits, fontTraits); // Check the results (yes, this NSLog create leaks as I don't release the CFStrings, but this is just for debugging) NSLog(@"font:%@, newFontWithoutTraits:%@, newFont:%@", CTFontCopyFullName(font), CTFontCopyFullName(newFontWithoutTraits), CTFontCopyFullName(newFont)); // Clear memory (CoreFoundation "Create Rule", objects needs to be CFRelease'd) CFRelease(newFont); CFRelease(newFontWithoutTraits); CFRelease(font);
- Is iOS developer able to view file system?
- Why dereferencing a NSString pointer is not necessary?
- No debugging symbols are available for the OS version installed on Apple Watch
- ios sdk stop multitouch functionality
- Facebook App Invites iOS v4 SDK error
- Adaptive Layout in Xcode 8
- Difference between Xcode version (CFBundleShortVersionString) and build (CFBundleVersion)
- How to get Keyboard on double tap on a text field
- How to apply partial fade in-out in IOS?
- How to access Swift-objects from a c++ class?
- How can I get transparent background in my iOS7 app (like Newsstand) so I can see the Home Screen wallpaper?
- How dangerous is it to use pointer-style assignment versus setter-methods in Objective-C?
- How can I make a function complete before calling others in an IBAction?
- Is it possible to show a tooltip in an iOS app?
- Convert an iOS objective c object to a JSON string