iOS单间距自定义字体
我有一个自定义字体包含在我的Xcode 7,iOS 9目标项目中。我想把字体改成等距。我试过了,但没有成功:iOS单间距自定义字体,ios,fonts,core-text,monospace,Ios,Fonts,Core Text,Monospace,我有一个自定义字体包含在我的Xcode 7,iOS 9目标项目中。我想把字体改成等距。我试过了,但没有成功: let originalFont = UIFont(name: "My Custom Font", size: 18) let originalFontDescriptor = originalFont!.fontDescriptor() let fontDescriptorFeatureSettings = [ [ UIFontFeatureTypeIdent
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
无论是否有上述代码,标签均以适当的自定义字体显示。上面的代码不起任何作用。您使用的代码没有使字体为单间距 它正在调整字体以在单空间模式下渲染数字。因此,所有使用这种字体的数字将具有相同的宽度 下面是一个带有4个标签的示例,1个标签使用自定义字体Docis Light,2个标签使用单间距数字,3个标签使用相同大小的系统字体,4个标签使用单间距数字的系统字体: 如您所见,此自定义字体已支持开箱即用的单空格数字功能,无需调整 如果您需要使用单间距字体(不仅仅是数字),您必须使用自定义单间距字体(设计为单间距),或者您可以使用内置的iOS单间距字体,如Courier或Menlo(请参阅上的所有可用iOS字体) 在相同的场景中,它们是这样的: 无论是否调整,它们都是单间距的,数字也是单间距的
我在这里回答了类似的问题,也许,我应该只链接图像而不是图像,但它更直观。您使用的代码不是使字体为单间距 它正在调整字体以在单空间模式下渲染数字。因此,所有使用这种字体的数字将具有相同的宽度 下面是一个带有4个标签的示例,1个标签使用自定义字体Docis Light,2个标签使用单间距数字,3个标签使用相同大小的系统字体,4个标签使用单间距数字的系统字体: 如您所见,此自定义字体已支持开箱即用的单空格数字功能,无需调整 如果您需要使用单间距字体(不仅仅是数字),您必须使用自定义单间距字体(设计为单间距),或者您可以使用内置的iOS单间距字体,如Courier或Menlo(请参阅上的所有可用iOS字体) 在相同的场景中,它们是这样的: 无论是否调整,它们都是单间距的,数字也是单间距的
我在这里回答了类似的问题,也许,我应该只链接图像而不是图像,但它更直观。别忘了导入头文件。希望它能起作用。此解决方案位于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]];
#导入
UIFont*const existingFont=[UIFont preferredFontForTextStyle:UIFontTextStyleBody];
UIFontDescriptor*常量existingDescriptor=[ExistingFontDescriptor];
NSDictionary*常量fontAttributes=@{
UIFontFeatureTypeIdentifierKey
UIFontDescriptorFeatureSettingsAttribute:@[
@{
UIFontFeatureTypeIdentifierKey:@(kNumberSpacingType),
UIFontFeatureSelectorIdentifierKey:@(KmonospacedNumber选择器)
}]
};
UIFontDescriptor*常量monospacedDescriptor=[existingDescriptor FontDescriptor ByAddingAttribute:fontAttributes];
UIFont*常量比例字体=[UIFont fontWithDescriptor:monospacedDescriptor size:[existingFont pointSize]];
别忘了导入头文件。希望它能起作用。此解决方案位于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]];
#导入
UIFont*const existingFont=[UIFont preferredFontForTextStyle:UIFontTextStyleBody];
UIFontDescriptor*常量existingDescriptor=[ExistingFontDescriptor];
NSDictionary*常量fontAttributes=@{
UIFontFeatureTypeIdentifierKey
UIFontDescriptorFeatureSettingsAttribute:@[
@{
UIFontFeatureTypeIdentifierKey:@(kNumberSpacingType),
UIFontFeatureSelectorIdentifierKey:@(KmonospacedNumber选择器)
}]
};
UIFontDescriptor*常量monospacedDescriptor=[existingDescriptor FontDescriptor ByAddingAttribute:fontAttributes];
UIFont*常量比例字体=[UIFont fontWithDescriptor:monospacedDescriptor size:[existingFont pointSize]];
我下面的答案是只对现有字体进行单倍行距编号(而不是整个字体)(如果字体支持)
至少当我找到这条线的时候,我是在寻找单倍行距的数字。因此,我希望它会有所帮助,尽管它回答了另一个问题
这工作正常,在Swift 5和iOS14+13上进行了测试:
(只要“您的字体支持单间距数字功能”。)
扩展UIFont{
var monospacedgitfont:UIFont{
让oldFontDescriptor=fontDescriptor
让newFontDescriptor=oldFontDescriptor.monospacedDigitFontDescriptor
返回UIFont(描述符:newFontDescriptor,大小:0)
}
}
专用扩展UIFontDescriptor{
var monospacedGitFontDescriptor:UIFontDescriptor{
让fontDescriptorFeatureSettings=[[UIFontDescriptor.FeatureKey.featureIdentifier:kNumberPacingType,UIFontDescriptor.FeatureKey.typeIdentifier:KMonoSpacedNumber选择器]]
让fontDescriptorAttributes=[UIFontDescriptor.AttributeName.featureSettings:fontDescriptorFeatureSettings]
让fontDescriptor=self.addingAttributes(fontDescriptorAttributes)
返回字体描述符
}
}
然后,您可以在任何标签上使用它,如下所示:
///激活单间距标签
myLabel.font=myLabel.font.monospacedDigitFontDescriptor
///单间距标签未激活(默认为比例间距)
myLabel.font=myLabel.font
(来源:)我下面的答案是只对现有字体进行单倍行距编号(而不是整个字体)(如果字体支持的话)
至少当我找到这条线的时候,我是在寻找单倍行距的数字。因此,我希望它会有所帮助,尽管它回答了另一个问题
这工作正常,在Swift 5和iOS14+13上进行了测试:
(只要“您的字体支持单间距数字功能”。)
扩展UIFont{
var monospacedgitfont:UIFont{
让oldFontDescriptor=fontDescriptor
让newFontDescriptor=oldFontDescriptor.monospacedDigitFontDescriptor
返回UIFo