如何在iOS7中使用具有动态文本大小的自定义字体
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在iOS7中使用具有动态文本大小的自定义字体相关的知识,希望对你有一定的参考价值。
在ios7中,有一些新的API用于获取自动调整为用户在其首选项中设置的文本大小的字体。
它看起来像这样使用它:
UIFont *myFont = [UIFont fontWithDescriptor:[UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleHeadline] size:0];
现在,无论您为此分配的文本在用户更改其系统文本大小设置时都会以字体大小上下移动。 (请记住听name:UIContentSizeCategoryDidChangeNotification
通知并更新您的视图以考虑大小的变化)。
How do I use dynamic text with a font other than the default Helvetica-Neue?
在API的幕后,apple有一些查找表,它返回特定的字体系列,大小,有时还有符号特征(如粗体)(例如UIFontTextStyleHeadline
)和用户首选的文本大小。后者是从sharedApplication
拉出的字符串,如下所示:
[UIApplication sharedApplication].preferredContentSizeCategory;
(我注销了各种动态文字大小的all the default sizes/fonts/traits for Helvetica-Neue)。我们已经为可访问性大小添加了处理,这很重要。
所以你真正需要做的就是构建一个类似的查找表。我们的设计师为我创建了一个简单的电子表格
请注意,我们添加了几个样式(标题3和4),有8个而不是6个可供选择。
然后你会想把它放在方便的地方,比如UIFontDescriptor
上的一个类别。你会希望你的方法返回像Apple的API一样的UIFontDescriptor
,这样它仍然很容易调整符号特征等。
我的类别如下所示:
UIFontDescriptor + AvenirNext.h
#import <UIKit/UIKit.h>
extern NSString *const ANUIFontTextStyleCaption3;
@interface UIFontDescriptor (AvenirNext)
+(UIFontDescriptor *)preferredAvenirNextFontDescriptorWithTextStyle:(NSString *)style;
@end
UIFontDescriptor + AvenirNext.m
#import "UIFontDescriptor+AvenirNext.h"
NSString *const ANUIFontTextStyleCaption3 = @"ANUIFontTextStyleCaption3";
NSString *const ANUIFontTextStyleCaption4 = @"ANUIFontTextStyleCaption4";
@implementation UIFontDescriptor (AvenirNext)
+(UIFontDescriptor *)preferredAvenirNextFontDescriptorWithTextStyle:(NSString *)style {
static dispatch_once_t onceToken;
static NSDictionary *fontSizeTable;
dispatch_once(&onceToken, ^{
fontSizeTable = @{
UIFontTextStyleHeadline: @{
UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: @26,
UIContentSizeCategoryAccessibilityExtraExtraLarge: @25,
UIContentSizeCategoryAccessibilityExtraLarge: @24,
UIContentSizeCategoryAccessibilityLarge: @24,
UIContentSizeCategoryAccessibilityMedium: @23,
UIContentSizeCategoryExtraExtraExtraLarge: @23,
UIContentSizeCategoryExtraExtraLarge: @22,
UIContentSizeCategoryExtraLarge: @21,
UIContentSizeCategoryLarge: @20,
UIContentSizeCategoryMedium: @19,
UIContentSizeCategorySmall: @18,
UIContentSizeCategoryExtraSmall: @17,},
UIFontTextStyleSubheadline: @{
UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: @24,
UIContentSizeCategoryAccessibilityExtraExtraLarge: @23,
UIContentSizeCategoryAccessibilityExtraLarge: @22,
UIContentSizeCategoryAccessibilityLarge: @22,
UIContentSizeCategoryAccessibilityMedium: @21,
UIContentSizeCategoryExtraExtraExtraLarge: @21,
UIContentSizeCategoryExtraExtraLarge: @20,
UIContentSizeCategoryExtraLarge: @19,
UIContentSizeCategoryLarge: @18,
UIContentSizeCategoryMedium: @17,
UIContentSizeCategorySmall: @16,
UIContentSizeCategoryExtraSmall: @15,},
UIFontTextStyleBody: @{
UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: @21,
UIContentSizeCategoryAccessibilityExtraExtraLarge: @20,
UIContentSizeCategoryAccessibilityExtraLarge: @19,
UIContentSizeCategoryAccessibilityLarge: @19,
UIContentSizeCategoryAccessibilityMedium: @18,
UIContentSizeCategoryExtraExtraExtraLarge: @18,
UIContentSizeCategoryExtraExtraLarge: @17,
UIContentSizeCategoryExtraLarge: @16,
UIContentSizeCategoryLarge: @15,
UIContentSizeCategoryMedium: @14,
UIContentSizeCategorySmall: @13,
UIContentSizeCategoryExtraSmall: @12,},
UIFontTextStyleCaption1: @{
UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: @19,
UIContentSizeCategoryAccessibilityExtraExtraLarge: @18,
UIContentSizeCategoryAccessibilityExtraLarge: @17,
UIContentSizeCategoryAccessibilityLarge: @17,
UIContentSizeCategoryAccessibilityMedium: @16,
UIContentSizeCategoryExtraExtraExtraLarge: @16,
UIContentSizeCategoryExtraExtraLarge: @16,
UIContentSizeCategoryExtraLarge: @15,
UIContentSizeCategoryLarge: @14,
UIContentSizeCategoryMedium: @13,
UIContentSizeCategorySmall: @12,
UIContentSizeCategoryExtraSmall: @12,},
UIFontTextStyleCaption2: @{
UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: @18,
UIContentSizeCategoryAccessibilityExtraExtraLarge: @17,
UIContentSizeCategoryAccessibilityExtraLarge: @16,
UIContentSizeCategoryAccessibilityLarge: @16,
UIContentSizeCategoryAccessibilityMedium: @15,
UIContentSizeCategoryExtraExtraExtraLarge: @15,
UIContentSizeCategoryExtraExtraLarge: @14,
UIContentSizeCategoryExtraLarge: @14,
UIContentSizeCategoryLarge: @13,
UIContentSizeCategoryMedium: @12,
UIContentSizeCategorySmall: @12,
UIContentSizeCategoryExtraSmall: @11,},
ANUIFontTextStyleCaption3: @{
UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: @17,
UIContentSizeCategoryAccessibilityExtraExtraLarge: @16,
UIContentSizeCategoryAccessibilityExtraLarge: @15,
UIContentSizeCategoryAccessibilityLarge: @15,
UIContentSizeCategoryAccessibilityMedium: @14,
UIContentSizeCategoryExtraExtraExtraLarge: @14,
UIContentSizeCategoryExtraExtraLarge: @13,
UIContentSizeCategoryExtraLarge: @12,
UIContentSizeCategoryLarge: @12,
UIContentSizeCategoryMedium: @12,
UIContentSizeCategorySmall: @11,
UIContentSizeCategoryExtraSmall: @10,},
UIFontTextStyleFootnote: @{
UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: @16,
UIContentSizeCategoryAccessibilityExtraExtraLarge: @15,
UIContentSizeCategoryAccessibilityExtraLarge: @14,
UIContentSizeCategoryAccessibilityLarge: @14,
UIContentSizeCategoryAccessibilityMedium: @13,
以上是关于如何在iOS7中使用具有动态文本大小的自定义字体的主要内容,如果未能解决你的问题,请参考以下文章