Search code examples
ioscocoadrawingcore-textitalic

What's the CoreText equivalent to AppKit's NSObliquenessAttributeName?


I'm drawing some text in Mac/iOS cross-platform code using CoreText. I may be using fonts that do not have a real "Italic" version installed in the OS for all users, but they need to be aware that the text is italic even then.

With AppKit's NSAttributedString -drawAtPoint:, I can use NSObliquenessAttributeName to make the text slanted (and thus look italic -- well, oblique). CoreText doesn't seem to have an equivalent for this attribute. At least I found none in CTStringAttributes.h (not that there's any documentation even years after CoreText was released).

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


Solution

  • Displaying a font that has no italic trait as italic is generally a bad idea. However, I can understand that there are some cases where this has to be enforced anyways.

    The only solution that comes to my mind right now is to create a custom font with a sheared font matrix:

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

    You'll have to play with the matrix and see what brings the best results. (Please not that this is a fake code mix out of my head and the internet.)