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?
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.
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.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.