Objective-C - CTFont change font style?

I have a CTFont that contains font style and sumbolic features.

I want to create a new font with a new style that inherits the symbolic features 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 here is null I don’t know what I should pass to the 4th parameter in CTFontCreateCopyWithSymbolicTraits .

+1
source share
1 answer

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 to
  • wantBold - a boolean to say if I want to add or remove a bold tag to the font
  • kCTFontBoldTrait 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); // 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); 
+9
source

Source: https://habr.com/ru/post/1386679/


All Articles