I am making this line of code to create bold from bold:
CTFontRef newFont = CTFontCreateCopyWithSymbolicTraits(currentFont, 0.0, NULL, (wantBold?kCTFontBoldTrait:0), kCTFontBoldTrait);
currentFont is CTFontRef I want to add symbolic features towantBold - a boolean to say if I want to add or remove a bold tag to the fontkCTFontBoldTrait is the character I want to change in the font.
The fourth parameter is the value you want to apply. 5th is a mask for selecting a symbolic sign.
You can use it as a bitmask to apply to a symbolic feature, where the 4th parameter of CTFontCreateCopyWithSymbolicTraits is the value and the 5th parameter is the mask:
- If you want to install symtrait and add it to the font, iOS will probably use sthg like
newTrait = oldTrait | (value&mask) newTrait = oldTrait | (value&mask) by setting the bit corresponding to mask to the value value . If you want to disable symtrait and remove it from the font, you use the value 0 as the 4th parameter, and iOS will probably use sthg like newTrait = oldTrait & ~mask to clear the bit.
But if you need, you can set and disable several bits at the same time (for example, several symbolic signs) using the right value , which has 1 on the bits to set and 0 on the bits to cancel (or to ignore) and using the mask right, which have 1 bit that needs to be changed, and 0 on bits that do not need to be changed.
[EDIT2]
Finally, I managed to find a solution for your specific case: you need to get the symtraits mask of your font , as you already did, and a bitwise one - or this with the symtraits of your newFontWithoutTraits .
This is because newFontWithoutTraits actually do have default symmetries (contrary to what I thought, it has a non-zero CTFontSymbolicTraits value), since the symtraits value also contains font class information and such things (so even a non-greasy, non-Italian font can have non-zero symtraits value, write down the hexadecimal symtraits value of your font for a better understanding).
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);
source share