gpt4 book ai didi

ios - 如何判断字体家族中哪个iOS字体名称是 "regular"版本?

转载 作者:行者123 更新时间:2023-11-29 04:09:49 24 4
gpt4 key购买 nike

我知道我可以使用 [UIFont familyNames] 获取字体系列名称列表,并使用 [UIFont fontNamesForFamilyName:] 迭代该系列的字体名称。有什么方法可以告诉哪个字体名称代表“正常”字体吗?命名没有任何一致性(“罗马”、“常规”,甚至没有修饰形容词)。我最终试图更改 NSAttributedString 子字符串的字体,同时保留现有的特征和装饰。提前致谢。

最佳答案

嗯,我需要更仔细地阅读 CoreText 文档。如果您使用正确的 CoreText 例程,则传递字体系列名称就足够了...

NSString *fontFamilyName = ...(font family name)...;
// Make a mutable copy of the attributed text
NSMutableAttributedString *workingAttributedText = [self.attributedText mutableCopy];

// Over every attribute run in the selected range...
[workingAttributedText enumerateAttributesInRange:self.selectedRange
options:(NSAttributedStringEnumerationOptions) 0
usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) {
// get the old font
CTFontRef oldFont = (__bridge CTFontRef)[attrs objectForKey:NSFontAttributeName];
// make a new one, with our new desired font family name
CTFontRef newFontRef = CTFontCreateCopyWithFamily(oldFont, 0.0f, NULL, (__bridge CFStringRef)fontFamilyName);
// Convert it to a UIFont
UIFont *newFont = [UIFont fontWithCTFont:newFontRef];
// Add it to the attributed text
[workingAttributedText addAttribute:NSFontAttributeName value:newFont range:range];
}];

// Replace the attributed text with the new version
[self setAttributedText:workingAttributedText];

如果有更简单的方法来做到这一点,我很想听听。

关于ios - 如何判断字体家族中哪个iOS字体名称是 "regular"版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14589075/

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