在iOS上以小型大写字体排版字体

mad*_*ulf 10 fonts ios

在iOS上,我通过将其文件名(.otf文件)添加到info.plist文件然后使用以下代码行在项目中加载自定义字体:

UIFont myFont*= [UIFont fontWithName:titleFontName size:titleFontSize];

我获得了可以在UILabels和UITextViews中使用的字体.

我怎么能得到这个字体只显示小型大写字母?如果我在Photoshop中使用它,可以打开小型大写字母开关,将所有单词排版成小型大写字母(因此,我得出结论,字体没有任何缺失).我怎么能在iOS上获得类似的效果?

由于其他原因,将我的字符串转换为大写不是可行的选择.

更多信息:该字体在其系列中只有一个成员,我可以通过使用以下代码理解,该系列中没有独立的小型成员.

 for (NSString * familyName in [UIFont familyNames]) {

        NSLog(@"---------------- %@ ---------------", familyName);
        for (NSString * fontName in[UIFont fontNamesForFamilyName:familyName] )
           NSLog(@"- %@", fontName);
   }
Run Code Online (Sandbox Code Playgroud)

Ant*_*tox 18

通过打开类型功能在字体中启用小型大写字母.在iOS 7中,我们可以使用字体描述符来访问开放式功能并启用小型上限.

这个问题涉及如何使用核心文本打开小型大写字母,但同样可以轻松地为UIFonts和UIKit视图做同样的事情.您需要创建一个UIFontDescriptor并设置UIFontDescriptorFeatureSettingsAttribute为要启用的功能的字典数组.

每个字体要素字典都包含用于指定要素类型的键和值,以及要素选择器的键和值.根据您使用的字体,您需要找到与小型大写相对应的正确值.您可以在注释部分记录的数组中找到它们.

UIFont类别

此类别将生成启用了小型大写的UIFont对象.您需要添加正确的字体名称.

#import "UIFont+SmallCaps.h"
#import <CoreText/CoreText.h>

@implementation UIFont (SmallCaps)

+ (UIFont *) applicationSmallCapsFontWithSize:(CGFloat) size {
    /*
    // Use this to log all of the properties for a particular font
    UIFont *font = [UIFont fontWithName: fontName size: fontSize];
    CFArrayRef  fontProperties  =  CTFontCopyFeatures ( ( __bridge CTFontRef ) font ) ;
    NSLog(@"properties = %@", fontProperties);
    */

    NSArray *fontFeatureSettings = @[ @{ UIFontFeatureTypeIdentifierKey: @(kLowerCaseType),
                                         UIFontFeatureSelectorIdentifierKey : @(kLowerCaseSmallCapsSelector) } ];

    NSDictionary *fontAttributes = @{ UIFontDescriptorFeatureSettingsAttribute: fontFeatureSettings ,
                                      UIFontDescriptorNameAttribute: FONT_NAME } ;

    UIFontDescriptor *fontDescriptor = [ [UIFontDescriptor alloc] initWithFontAttributes: fontAttributes ];

    return [UIFont fontWithDescriptor:fontDescriptor size:size];
}

@end
Run Code Online (Sandbox Code Playgroud)

  • 注意:如果字体没有小型大写字母,则不应在较大的大写字母旁边使用假小型大写字母.对比度的差异看起来很奇怪.如果你想要所有小型大写的文字,你可以通过画一个略小的大小的大写字符串来近似外观,并跟踪字母(我想用`NSExpansionAttributeName`). (2认同)

Bar*_*zyk 10

UIFont在Swift中的扩展:

extension UIFont {

    func smallCaps() -> UIFont {

        let settings = [[UIFontFeatureTypeIdentifierKey: kLowerCaseType, UIFontFeatureSelectorIdentifierKey: kLowerCaseSmallCapsSelector]]
        let attributes: [String: AnyObject] = [UIFontDescriptorFeatureSettingsAttribute: settings, UIFontDescriptorNameAttribute: fontName]

        return UIFont(descriptor: UIFontDescriptor(fontAttributes: attributes), size: pointSize)
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

label.font = UIFont(name: "SourceSansPro-Regular", size: 12)?.smallCaps()
Run Code Online (Sandbox Code Playgroud)

例:

在此输入图像描述 在此输入图像描述

macOS版本:

extension NSFont {
    func smallCaps() -> NSFont? {
        let settings = [[NSFontFeatureTypeIdentifierKey: kLowerCaseType, NSFontFeatureSelectorIdentifierKey: kLowerCaseSmallCapsSelector]]
        let attributes: [String: AnyObject] = [NSFontFeatureSettingsAttribute: settings as AnyObject, NSFontNameAttribute: fontName as AnyObject]

        return NSFont(descriptor: NSFontDescriptor(fontAttributes: attributes), size: pointSize)
    }
}
Run Code Online (Sandbox Code Playgroud)

重要:

  • 不是每种字体都适用它,它取决于这些字符是否包含在字体中,如@Anthony Mattox所说.

  • 记得将字符串设置为:Example而不是EXAMPLE.

  • 设置是Array<NDSictionary>,而不是NSDictonary.