gpt4 book ai didi

ios - UIFont:如何使用文体替代字符?

转载 作者:行者123 更新时间:2023-12-01 16:01:32 25 4
gpt4 key购买 nike

在我的应用程序中,我想使用 stylistic alternate font对于 'a' 而不是 system font .

附件 screenshot这解释了字体的不同渲染。

enter image description here

如何为 UILabel 启用此行为和 UITextView以便它呈现正确的一层'a'?

我确实找到了 YouTube video link这完全解释了这一点,但他使用的是 custom font它是硬编码的。我想用system font只是但有了这个替代字符。

我也许可以硬编码 UILabelcustom character ,我不确定,因为我想使用 System font .我不想用 custom Font .怎么样UITextView哪个是可编辑的?我们怎样才能让它使用替代a当用户输入时?

最佳答案

这是一种称为“替代文体集”的字体功能,您可以使用 CoreText 对其进行配置。请记住,并非所有字体都有此选项,但系统字体有。但是,您需要弄清楚您想要哪种替代方案。

首先,创建您感兴趣的字体:

import CoreText
import UIKit

let baseFont = UIFont.systemFont(ofSize: 72)

然后打印出它的特征:
print(CTFontCopyFeatures(baseFont)!)

找到关于替代文体集的部分,特别是你想要的集,“一层楼:”
    {
CTFeatureTypeIdentifier = 35;
CTFeatureTypeName = "Alternative Stylistic Sets";
CTFeatureTypeSelectors = (
{
CTFeatureSelectorIdentifier = 2;
CTFeatureSelectorName = "Straight-sided six and nine";
},
{
CTFeatureSelectorIdentifier = 4;
CTFeatureSelectorName = "Open four";
},
{
CTFeatureSelectorIdentifier = 6;
CTFeatureSelectorName = "Vertically centered colon";
},
{
CTFeatureSelectorIdentifier = 10;
CTFeatureSelectorName = "Vertically compact forms";
},
{
CTFeatureSelectorIdentifier = 12;
CTFeatureSelectorName = "High legibility";
},
{
CTFeatureSelectorIdentifier = 14;
CTFeatureSelectorName = "One storey a";
},
...

重要的数字是选择器 (CTFeatureSelectorIdentifier),14。有了它,您可以创建一个新的字体描述符和新字体:
let descriptor = CTFontDescriptorCreateCopyWithFeature(
baseFont.fontDescriptor,
kStylisticAlternativesType as CFNumber,
14 as CFNumber)

或者,如果更方便,您可以直接在 UIKit 中执行此操作:
let settings: [UIFontDescriptor.FeatureKey: Int] = [
.featureIdentifier: kStylisticAlternativesType,
.typeIdentifier: 14
]

let descriptor = baseFont.fontDescriptor.addingAttributes([.featureSettings: [settings]])

(请注意 .featureIdentifier 是“CTFeature 类型 标识符”和 .typeIdentifier 是“CTFeature 选择器 标识符”这一有点令人惊讶的事实。)

然后你可以创建一个新字体(零大小意味着保持大小不变​​):
let font = UIFont(descriptor: descriptor, size: 0)

您可以在任何接受 UIFont 的地方使用它。

关于ios - UIFont:如何使用文体替代字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59464935/

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