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 CTFontCreateCopyWithSymbolicTraits.
I do this line of code to generate a bold font from non-bold font:
currentFontis theCTFontRefI want to add symbolic traits towantBoldis a boolean to tell if I want to add or remove the bold trait to the fontkCTFontBoldTraitis 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
CTFontCreateCopyWithSymbolicTraitsis the value and the 5th parameter is the mask:newTrait = oldTrait | (value&mask), setting the bit corresponding tomaskto the value ofvalue.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 rightmaskthat have 1 on bits that needs to be modified and 0 on bits that don't need to be changed.[EDIT2]
I finally managed to find the solution for your specific case: you need to get the symtraits mask of your
fontas you already do… and bitwise-or it with the symtraits of yournewFontWithoutTraitsfont.This is because
newFontWithoutTraitsactually do have default symtraits (contrary to what I thought, it has a non-zeroCTFontSymbolicTraitsvalue) 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: