iOS Monospaced自定义字体 [英] iOS Monospaced Custom Font

查看:330
本文介绍了iOS Monospaced自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Xcode 7,iOS 9目标项目中包含自定义字体。我想使字体等宽。我试过这个,但没有用:

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 = UIFont(name: "My Custom Font", size: 18)
let originalFontDescriptor = originalFont!.fontDescriptor()

let fontDescriptorFeatureSettings = [
    [
        UIFontFeatureTypeIdentifierKey: kNumberSpacingType,
        UIFontFeatureSelectorIdentifierKey: kMonospacedNumbersSelector
    ]
]

let fontDescriptorAttributes = [UIFontDescriptorFeatureSettingsAttribute: fontDescriptorFeatureSettings]
let fontDescriptor = originalFontDescriptor.fontDescriptorByAddingAttributes(fontDescriptorAttributes)
let font = UIFont(descriptor: fontDescriptor, size: 0)

topLabel.font = font

有或没有上述代码,标签以适当的自定义字体显示。它只是代码上面没有做任何事情。

With or without above code, the label displayed in proper custom font. It's just above code doesn't do anything.

推荐答案

不要忘记导入标题文件。希望它会奏效。此解决方案位于 Objective-C

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

#import <CoreTextArcView.h>

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]];

这篇关于iOS Monospaced自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆