即使等效代码在 Swift 中工作,我在 Objective C 中显示分数时也遇到了问题。我一定遗漏了一些非常明显的东西??
SWIFT代码:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//: Playground - noun: a place where people can play
let pointSize = self.label.font.pointSize
let systemFontDesc = UIFont.systemFont(ofSize: pointSize, weight: UIFontWeightLight).fontDescriptor
let fractionFontDesc = systemFontDesc.addingAttributes(
[
UIFontDescriptorFeatureSettingsAttribute: [
[
UIFontFeatureTypeIdentifierKey: kFractionsType,
UIFontFeatureSelectorIdentifierKey: kDiagonalFractionsSelector,
], ]
] )
print(fractionFontDesc)
self.label.font = UIFont(descriptor: fractionFontDesc, size:pointSize)
print("label.font.descriptor: \(self.label.font.fontDescriptor)")
}
结果:
Objective C 中的等效代码
-(void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
CGFloat pointSize = _label.font.pointSize;
UIFontDescriptor *systemFontDesc = [UIFont systemFontOfSize:pointSize weight:UIFontWeightLight].fontDescriptor;
UIFontDescriptor *fractionDescriptor = [systemFontDesc fontDescriptorByAddingAttributes:@{ UIFontDescriptorFeatureSettingsAttribute : @{
UIFontFeatureTypeIdentifierKey: @(11), // kFractionsType
UIFontFeatureSelectorIdentifierKey: @(2)}}]; // kDiagonalFractionsSelector
NSLog(@"%@\n\n", fractionDescriptor);
UIFont *fracFont = [UIFont fontWithDescriptor:fractionDescriptor size:pointSize];
NSLog(@"fracFont.fontDescriptor: %@\n\n", fracFont.fontDescriptor);
[_label setFont: fracFont];
NSLog(@"label.font.descriptor: %@\n\n", _label.font.fontDescriptor);
}
结果: