gpt4 book ai didi

ios - 如何找到包含给定字符的字体?

转载 作者:可可西里 更新时间:2023-11-01 03:43:31 25 4
gpt4 key购买 nike

我有一种用于我的应用程序的字体,它只使用英文字符。我使用 CoreText 来显示文本,我注意到每当有人输入字体中未包含的字符(如阿拉伯字符)时,程序就会卡在这一行:

CTFramesetterCreateFrame(textFramesetter, CFRangeMake(0, 0), textMutablePath, NULL); 

我有几个问题:

  1. 是否可以知道某个字符是否包含在字体中?
  2. 系统能否找到包含未知字符的字体?

相关:Check if certain character is supported by UIFont

最佳答案

1) 是否可以知道字体中是否包含某个字符?

BOOL FontContainsCharacter(UIFont *font, unichar character) {
NSCharacterSet *characterSet = [font.fontDescriptor objectForKey:UIFontDescriptorCharacterSetAttribute];
return [characterSet characterIsMember:character];
}

2) 系统是否可以找到包含未知字符的字体?

NSArray *FontDescriptorsForFontsContainingCharactersInString(NSString *string) {
NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:string];
NSDictionary *fontAttributes = @{UIFontDescriptorCharacterSetAttribute:characterSet};
UIFontDescriptor *fontDescriptor = [UIFontDescriptor fontDescriptorWithFontAttributes:fontAttributes];
return [fontDescriptor matchingFontDescriptorsWithMandatoryKeys:nil];
}

例如:

NSLog(@"%d", FontContainsCharacter([UIFont fontWithName:@"Helvetica" size:12], 0x0041)); // "A"
NSLog(@"%d", FontContainsCharacter([UIFont fontWithName:@"Helvetica" size:12], 0x4F60)); // "你"

for (UIFontDescriptor *fontDescriptor in FontDescriptorsForFontsContainingCharactersInString(@"你好")) {
NSLog(@"%@", [UIFont fontWithDescriptor:fontDescriptor size:12].fontName);
}

输出:

1
0
STHeitiSC-Light
STHeitiSC-Medium
STHeitiTC-Light
STHeitiTC-Medium
HiraKakuProN-W3
HiraKakuProN-W6
HiraMinProN-W3
HiraMinProN-W6

关于ios - 如何找到包含给定字符的字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20735902/

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