在iOS上以小型大写字体排版字体 [英] Typesetting a font in small caps on iOS

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

问题描述

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


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

我可以在UILabels和UITextViews中使用的字体。



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

由于其他原因,将我的字符串转换为大写字符不是一个可行的选项。

更多信息:字体在其家族中只有一个成员,正如我通过使用下面的代码可以理解的那样,家族中没有独立的小型成员。 ($ {

NSLog(@ - $ {

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' - - - - - - - %@ - - - - - - - -, 姓);
for(NSString * fontName in [UIFont fontNamesForFamilyName:familyName])
NSLog(@ - %@,fontName);


解决方案

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



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

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



UIFont类别



这个类别将生成一个UIFont对象启用了小型大写字母。您需要添加正确的字体名称。

  #importUIFont + SmallCaps.h
#import < CoreText / CoreText.h>
$ b @implementation UIFont(SmallCaps)

+(UIFont *)applicationSmallCapsFontWithSize:(CGFloat)size {
/ *
//使用它记录所有特定字体的属性
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


On iOS, I load a custom font in my project by adding its file name (an .otf file) to the info.plist file and then using this line of code:

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

I obtain the font that I can use in UILabels and UITextViews.

How could I obtain that this font is displayed only in small caps? If I use it in Photoshop, it's possible to turn on the small caps switch to have all words typeset in small caps (and so, I conclude that there is nothing missing with the font). How could I obtain a similar effect on iOS?

Converting my strings to uppercase is not a viable option for other reasons.

Further information : the font has only one member in its family, as I could understand by using the following code, there is no standalone small caps member in the family.

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

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

解决方案

Small caps are enabled in the font through an open type feature. In iOS 7 we can use a font descriptor to access open type features and enable small caps.

This question goes into how to turn on small caps using core text, but the same can be done for UIFonts and UIKit views just as easily. You'll need to create a UIFontDescriptor and set the UIFontDescriptorFeatureSettingsAttribute to an array of dictionaries for the features you want to enable.

Each font feature dictionary contains a key and value to specify the feature type, and a key and value for the feature selector. Depending on the font you're using, you'll need to find the correct values corresponding to small caps. You can find these in the array that the commented section logs.

UIFont Category

This category will generate a UIFont object with small caps enabled. You'll need to add the correct font name.

#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

这篇关于在iOS上以小型大写字体排版字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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