据this WWDC doc我所知,我的代码应该有用,但它没有:
NSString *fontName = [@"MaisonNeue-" stringByAppendingString:@"Light"];
UIFont *originalFont = [UIFont fontWithName:fontName size:size];
UIFontDescriptor *originalDescriptor = [originalFont fontDescriptor];
NSMutableCharacterSet *cset = [[originalDescriptor objectForKey:UIFontDescriptorCharacterSetAttribute] mutableCopy];
[cset formIntersectionWithCharacterSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
NSDictionary *newAttributes = @{UIFontDescriptorCharacterSetAttribute: cset};
UIFontDescriptor *monospaceDescriptor = [originalDescriptor fontDescriptorByAddingAttributes:newAttributes];
UIFont *newFont = [UIFont fontWithDescriptor:monospaceDescriptor size:0.0];
出了什么问题?新创建的字体仍包含我专门删除的字符:
(lldb) p [cset characterIsMember:'2']
(bool) $0 = false
(lldb) po [(NSCharacterSet *)[(id)monospaceDescriptor objectForKey:UIFontDescriptorCharacterSetAttribute] characterIsMember:'2']
false
(lldb) po [(NSCharacterSet *)[[newFont fontDescriptor] objectForKey:UIFontDescriptorCharacterSetAttribute] characterIsMember:'2']
true