Question

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?

Was it helpful?

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.)

OTHER TIPS

I’d try using the affine transform argument to CTFontCreateWithName() with a shear matrix. For instance

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

That will create quite an extreme skew (assuming I got it right), but you get the idea.

Update:

In fact, the documentation appears to imply that this is the right way to do things.

Haven't tried, but according to iOS Programming Pushing The Limits, passing kCTFontItalicTrait to CTFontCreateCopyWithSymbolicTraits will choose true italic if available, and oblique otherwise. There's also kCTFontSlantTrait for manual decimal slant up to 30 degrees.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top