Objective-C - CTFont 改变字体样式?
我有一个包含字体样式和求和特征的 CTFont。
我想创建一种具有新样式的新字体,继承第一种字体的符号特征。 我怎样才能实现这个目标?
CTFontRef newFontWithoutTraits = CTFontCreateWithName((CFString)newFontName, CTFontGetSize(font), NULL);
CTFontRef newFont = CTFontCreateCopyWithSymbolicTraits(newFontWithoutTraits, CTFontGetSize(font), NULL, 0, CTFontGetSymbolicTraits(font));
此处新字体为空 我不知道应该将什么传递给 CTFontCreateCopyWithSymbolicTraits
中的 4th
参数。
I have a CTFont that contains a font style, and sumbolic traits.
I want to create a new font with a new style that inherits the symbolic traits of the first font.
How can I achieve this?
CTFontRef newFontWithoutTraits = CTFontCreateWithName((CFString)newFontName, CTFontGetSize(font), NULL);
CTFontRef newFont = CTFontCreateCopyWithSymbolicTraits(newFontWithoutTraits, CTFontGetSize(font), NULL, 0, CTFontGetSymbolicTraits(font));
the new font is null here
I don't know what should I pass to the 4th
parameter in CTFontCreateCopyWithSymbolicTraits
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我执行这行代码以从非粗体字体生成粗体字体:
currentFont
是CTFontRef
我想向wantBold
添加符号特征是一个布尔值,告诉我是否要添加或删除字体的粗体特征kCTFontBoldTrait
是我要修改字体的符号特征。第四个参数是您要应用的值。第5个是选择象征性特征的面具。
您可以将其视为应用于符号特征的位掩码,其中
CTFontCreateCopyWithSymbolicTraits
的第四个参数是值,第五个参数是掩码:mask
对应的位设置为value
的值。如果您想取消设置 symtrait 并将其从字体中删除,您可以使用值 0 作为第四个参数,iOS 可能会应用诸如
newTrait = oldTrait & 之类的东西。 ~mask
来取消设置该位。但如果需要,您还可以使用正确的
值
一次设置和取消设置多个位(因此多个符号特征),其中要设置的位为 1,要取消设置的位为 0 (或忽略),并使用正确的掩码
,其中需要修改的位为 1,不需要更改的位为 0。[EDIT2]
我终于找到了适合您具体情况的解决方案:您需要获取
字体的 symtraits mask< /code> 就像你已经做的那样......并按位或它与你的
newFontWithoutTraits
字体的符号。这是因为
newFontWithoutTraits
实际上确实有默认的 symtraits(与我的想法相反,它有一个非零的CTFontSymbolicTraits
值)作为 symtraits 值还包含字体类等信息(因此,即使是非粗体、非斜体字体也可以具有非零 symtraits 值,记录该字体的十六进制值字体的特征以便更好地理解)。所以这是您需要的代码:
I do this line of code to generate a bold font from non-bold font:
currentFont
is theCTFontRef
I want to add symbolic traits towantBold
is a boolean to tell if I want to add or remove the bold trait to the fontkCTFontBoldTrait
is the symbolic trait I want to modify on the font.The 4th parameter is the value you want to apply. The 5th is the mask to select the symbolic trait.
You may thing of it as bitmask to apply to the symbolic trait, where the 4th parameter of
CTFontCreateCopyWithSymbolicTraits
is the value and the 5th parameter is the mask:newTrait = oldTrait | (value&mask)
, setting the bit corresponding tomask
to the value ofvalue
.If you want to unset the symtrait and remove it from the font, you use the value of 0 as the 4th parameter and iOS will probably apply sthg like
newTrait = oldTrait & ~mask
to unset the bit.But if you need to, you can also set and unset multiple bits (thus multiple symbolic traits) at once, using the right
value
that have 1 on bits to set and 0 on bits to unset (or to ignore), and and using the rightmask
that have 1 on bits that needs to be modified and 0 on bits that don't need to be changed.[EDIT2]
I finally managed to find the solution for your specific case: you need to get the symtraits mask of your
font
as you already do… and bitwise-or it with the symtraits of yournewFontWithoutTraits
font.This is because
newFontWithoutTraits
actually do have default symtraits (contrary to what I thought, it has a non-zeroCTFontSymbolicTraits
value) as the symtraits value also contains info for the font class and such things (so even a non-bold, non-italic font can have a non-zero symtraits value, log the hex value of the symtraits of your font to understand better).So this is the code you need: