What is the CoreText equivalent for AppKit NSObliquenessAttributeName?

I draw some text in cross-platform Mac / iOS code using CoreText. Perhaps I use fonts that do not have a real version of "Italic" installed in the OS for all users, but they should know that the text is already in italics.

With AppKit NSAttributedString -drawAtPoint :, I can use NSObliquenessAttributeName to make the text oblique (and therefore look italic - well, oblique). CoreText has no equivalent for this attribute. At least I did not find it in CTStringAttributes.h (there was no documentation even years after the release of CoreText).

Does anyone know how I can get oblique text from CoreText on iOS?

+4
source share
3 answers

Displaying a font that does not have italics as italics is usually a bad idea. However, I can understand that there are times when this should be enforced.

The only solution that comes to my mind right now is to create your own font with a modified font matrix:

CGAffineTransform matrix = CGAffineTransformMake(1, tan(degreesToRadians(0)), tan(degreesToRadians(20)), 1, 0, 0); CTFontRef myfont = CTFontCreateWithName(CFSTR("Helvetica"), 48, &matrix); 

You will need to play with the matrix and see what will bring the best results. (Please, not that this is fake code from my head and the Internet.)

+1
source

I'd try using the affine transform argument for CTFontCreateWithName() using a shift matrix. For instance,

 CGAffineTransform matrix = { 1, 0, 0.5, 1, 0, 0 }; CTFontRef myFont = CTFontCreateWithName(CFSTR("Helvetica"), 48, &matrix); 

This will create a very extreme skew (assuming I got it right), but you got the idea.

Update:

In fact, documentation means that this is the right way to do something .

+8
source

I haven’t tried, but according to iOS Programming Pushing Limits , passing kCTFontItalicTrait to CTFontCreateCopyWithSymbolicTraits will select true italics, if available, and vice versa. There is also kCTFontSlantTrait for manual decimal tilt up to 30 degrees.

+1
source

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


All Articles