gpt4 book ai didi

ios - 使用 CTFontCreateCopyWithSymbolicTraits 更改字体样式

转载 作者:行者123 更新时间:2023-11-29 12:38:10 27 4
gpt4 key购买 nike

我在名为 _label 的标签中有一个常规字体,我试图将其设为粗体,然后仅设为斜体(因此我想删除粗体样式)

为了让它变粗,我使用了下面的代码:

CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)_label.font.fontName, _label.font.pointSize, NULL);
CTFontRef boldFont = CTFontCreateCopyWithSymbolicTraits(font, 0.0, NULL, kCTFontBoldTrait, kCTFontBoldTrait);
CFStringRef boldName = CTFontCopyFullName(boldFont);
_label.font = [UIFont fontWithName:(__bridge NSString *)boldName size:_label.font.pointSize];

这很好用。但是,当我尝试使用以下代码将其设为斜体时,字体最终变为粗体和斜体:

font = CTFontCreateWithName((__bridge CFStringRef)_label.font.fontName, _label.font.pointSize, NULL);
boldFont = CTFontCreateCopyWithSymbolicTraits(font, 0.0, NULL, kCTFontBoldTrait | kCTFontItalicTrait, kCTFontItalicTrait); // I also tried 'kCTFontItalicTrait, kCTFontItalicTrait' for the last two parameters
boldName = CTFontCopyFullName(boldFont);
_label.font = [UIFont fontWithName:(__bridge NSString *)boldName size:_label.font.pointSize];

显然我没有正确使用 CTFontCreateCopyWithSymbolicTraits。如何去除粗体样式?

最佳答案

你应该使用:

boldFont = CTFontCreateCopyWithSymbolicTraits(font, 0.0, NULL, kCTFontItalicTrait, kCTFontBoldTrait | kCTFontItalicTrait);

(尽管您可能想更改变量名称)。

symTraitMask 参数(最后一个)指示调用应影响特征掩码的哪些位。您想要同时影响“粗体”和“斜体”位。

symTraitValue 参数(倒数第二个)指示受影响位的新值。您希望将这些位更改为“斜体”而不是“粗体”。

关于ios - 使用 CTFontCreateCopyWithSymbolicTraits 更改字体样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25697364/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com