为什么 UIFont 和 CGFont/CTFont 完全分离? [英] Why are UIFont and CGFont/CTFont separated completely?
问题描述
我试图用下载的字体绘制一些文本.我查看了 API 文档并搜索了 Web.我找到了一个解决方案,但问题是它仅适用于 CoreGraphics
级别的框架.所以我寻找了一种方法将CGFont
转换为UIFont
,但没有办法做到这一点.
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.
我意识到 UIFont
和 CGFont/CTFont
是完全分离的,即使它们是从同一个字体文件创建的.CGFont
和 CTFont
可以相互转换,但对于 UIFont
,唯一的方法是重新创建其属性,如名称和大小.并且适用的代码也分开了.
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.
例如,CATextLayer
只接受 CGFont
/CTFont
— 不接受 UIFont
.但所有 UIKit
控件只接受 UIFont
.
For instance, CATextLayer
accepts only CGFont
/CTFont
— no UIFont
. But all UIKit
controls accept only UIFont
.
为什么会存在这种分离?还是我错了?(有没有办法在 UIKit
类中使用下载的字体?)
Why does this separation exist? Or am I wrong? (Any way to use a downloaded font in UIKit
classes?)
推荐答案
简短的回答:只有 Apple 人才知道"
更详细的答案,稍微挖掘一下(逆向)框架:
The more detailed answer, after digging (reversing) frameworks a bit:
UIKit.framework
不使用CGFont
类型UIKit.framework
使用 CoreTextCTFont
和GSFont
...GSFont
来自...GraphicsServices
私有框架GraphicsServices
... 使用CGFont
UIKit.framework
doesn't use at allCGFont
typeUIKit.framework
use instead CoreTextCTFont
andGSFont
...GSFont
comes from... theGraphicsServices
private frameworkGraphicsServices
... useCGFont
所以这是我的猜测(可能完全错误) :
UIFont
>GSFont
(私有) >CGFont
UIKit
>GraphicsServices
>CoreGraphics
:
UIFont
>GSFont
(Private) >CGFont
UIKit
>GraphicsServices
>CoreGraphics
:
至少在字体方面,苹果工程师在UIKit
之间建立了一个抽象层和 CoreGraphics
框架,通过晦涩的 GraphicsServices
一个.
At least Concerning fonts, Apple engineers have set up an abstraction layer between
UIKit
andCoreGraphics
frameworks, via the obscurGraphicsServices
one.
iOS
> iOS
(私有) > Mac OSX
:
为什么他们这样做,我想说 CoreGraphics
,它来自 Mac OSX,不是 iOS 设备的最佳选择,而且 UIKit
仅适用于 iOS,他们添加了一个两个api之间的层.或者更一般地说,他们计划有一天将 UIKit 与 CoreGraphics 完全分离……iOS 与 Mac OSX……谁知道呢!苹果公司,我的简短回答:)
Why they did this, I would say
CoreGraphics
, which comes from Mac OSX, wasn't optimal for iOS devices, andUIKit
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 :)
关于在 UIKit
中使用自定义字体,您可能有兴趣阅读 这个.
About using custom fonts with UIKit
, you might be interested reading this.
这篇关于为什么 UIFont 和 CGFont/CTFont 完全分离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!