Vik*_*ica 8 cocoa-touch typography objective-c nsattributedstring ios7
我有一个客户要求在iOS 7项目中使用某种字体,因为它有中世纪的数字.
有没有办法为NSAttributedString激活这些数字?由于使用了默认的衬里编号,因此也包含在字体中.
这是一个例子.两条线都有相同的字体,没有变体(常规),一旦激活了中世纪数字,第二条线就有默认的衬里数字.

Tri*_*ops 11
这些被称为小写数字,可以使用UIFontDescriptor.
首先,您需要为某些常量导入CoreText:
#import <CoreText/SFNTLayoutTypes.h>
or
@import CoreText.SFNTLayoutTypes;
Run Code Online (Sandbox Code Playgroud)
然后使用字体描述符创建字体.在这里我使用乔治亚家庭:
NSDictionary *lowercaseNumbers = @{
UIFontFeatureTypeIdentifierKey: @(kNumberCaseType),
UIFontFeatureSelectorIdentifierKey: @(kLowerCaseNumbersSelector),
};
UIFontDescriptor *descriptor = [[UIFontDescriptor alloc] initWithFontAttributes:
@{
UIFontDescriptorFamilyAttribute: @"Georgia",
UIFontDescriptorFeatureSettingsAttribute:@[ lowercaseNumbers ],
}];
UIFont *font = [UIFont fontWithDescriptor:descriptor size:15];
Run Code Online (Sandbox Code Playgroud)
结果:

编辑:正如@ Random832指出的那样,格鲁吉亚只有小写数字,所以结果无关紧要.但是,@ vikingosegundo确认此代码适用于支持的字体.谢谢.

顶线是用.生成的
UIFont *font = [UIFont fontWithName:@"DIN Next LT Pro" size:12];
if (font)
label.font = font;
Run Code Online (Sandbox Code Playgroud)
第二行
NSDictionary *lowercaseNumbers = @{ UIFontFeatureTypeIdentifierKey:@(kNumberCaseType), UIFontFeatureSelectorIdentifierKey: @(kLowerCaseNumbersSelector)};
UIFontDescriptor *descriptor = [[UIFontDescriptor alloc] initWithFontAttributes:
@{UIFontDescriptorFamilyAttribute: @"DIN Next LT Pro",UIFontDescriptorFeatureSettingsAttribute:@[ lowercaseNumbers ]}];
UIFont *font = [UIFont fontWithDescriptor:descriptor size:12];
if (font)
label.font = font;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
683 次 |
| 最近记录: |