iOS Monospaced Custom Font

前端 未结 3 1891
生来不讨喜
生来不讨喜 2021-01-24 09:08

I have a custom font included in my Xcode 7, iOS 9 targeted project. I want to make the font monospaced. I tried this, and didn\'t work:

let originalFont = UIF         


        
3条回答
  •  花落未央
    2021-01-24 09:32

    Don't forget to import the header file. Hope it will work. This solution is in Objective-C

    #import 
    
    UIFont *const existingFont = [UIFont preferredFontForTextStyle:   UIFontTextStyleBody];
    UIFontDescriptor *const existingDescriptor = [existingFont fontDescriptor];
    
    NSDictionary *const fontAttributes = @{
    
    UIFontFeatureTypeIdentifierKey 
    
    UIFontDescriptorFeatureSettingsAttribute: @[
     @{
       UIFontFeatureTypeIdentifierKey: @(kNumberSpacingType),
       UIFontFeatureSelectorIdentifierKey: @(kMonospacedNumbersSelector)
      }]
    };
    
    UIFontDescriptor *const monospacedDescriptor = [existingDescriptor  fontDescriptorByAddingAttributes: fontAttributes];
    UIFont *const proportionalFont = [UIFont fontWithDescriptor: monospacedDescriptor size: [existingFont pointSize]];
    

提交回复
热议问题