Why are UIFont and CGFont/CTFont separated completely?

感情迁移 提交于 2019-12-12 10:33:26

问题


I was trying to draw some text with a downloaded font. I looked at API documentation and searched the Web. I found a solution, but the problem is that it's applicable to only the CoreGraphics level framework. So I searched for a way to convert CGFont to UIFont, but there was no way to do this.

I realized UIFont and CGFont/CTFont are separated completely, even though they're created from same font file. CGFont and CTFont are convertible from/to each other, but for UIFont, the only way is recreating with its attributes like name and size. And applicable codes are also separated.

For instance, CATextLayer accepts only CGFont/CTFont — no UIFont. But all UIKit controls accept only UIFont.

Why does this separation exist? Or am I wrong? (Any way to use a downloaded font in UIKit classes?)


回答1:


The short answer : "Only Apple folks knows"

The more detailed answer, after digging (reversing) frameworks a bit:

  • UIKit.framework doesn't use at all CGFont type
  • UIKit.framework use instead CoreText CTFont and GSFont...
  • GSFont comes from... the GraphicsServices private framework
  • GraphicsServices... use CGFont

So here is my guess (Might be completely wrong) :

  • UIFont > GSFont(Private) > CGFont
  • UIKit > GraphicsServices > CoreGraphics :

    At least Concerning fonts, Apple engineers have set up an abstraction layer between UIKit and CoreGraphics frameworks, via the obscur GraphicsServices one.

  • iOS > iOS (Private) > Mac OSX :

    Why they did this, I would say CoreGraphics, which comes from Mac OSX, wasn't optimal for iOS devices, and UIKit being iOS only, they added a layer between both apis. Or more generally they plan one day to separate completely UIKit from CoreGraphics... iOS from Mac OSX... Who knows! The apple folks does, my short answer :)

About using custom fonts with UIKit, you might be interested reading this.



来源:https://stackoverflow.com/questions/5799128/why-are-uifont-and-cgfont-ctfont-separated-completely

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!