大佬教程收集整理的这篇文章主要介绍了iOS7:我们可以使用除Helvetica Neue字体以外的动态类型吗?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我注意到属性preferredContentSizeCategory响应于设置中的文本大小而改变.
所以,我的想法是观察适当的通知,根据preferredContentSizeCategory属性计算大小增量,并将增量应用于文本视图的默认字体大小(以IB或编程方式设置).
PreferredFontLabel.h
@interface PreferredFontLabel : UILabel @property (nonatomiC) UIFontDescriptor *defaultFontDescriptor; @end
PreferredFontLabel.m
#import "PreferredFontLabel.h" #import "UIApplication+ContentSize.h" @implementation PreferredFontLabel - (id)init { self = [super init]; if (self) { [self setup]; } return self; } - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self setup]; } return self; } - (id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { [self setup]; } return self; } - (void)setup { self.defaultFontDescriptor = self.font.fontDescriptor; [[NsnotificationCenter defaultCenter] addObserver:self SELEctor:@SELEctor(contentSizeCategoryDidChangE) name:UIContentSizeCategoryDidChangeNotification object:nil]; [self contentSizeCategoryDidChange]; } - (void)setDefaultFontDescriptor:(UIFontDescriptor *)defaultFontDescriptor { _defaultFontDescriptor = defaultFontDescriptor; [self contentSizeCategoryDidChange]; } - (void)contentSizeCategoryDidChange { CGFloat preferredSize = [self.defaultFontDescriptor.fontAttributes[UIFontDescriptorSizeAttribute] floatValue]; preferredSize += [UIApplication sharedApplication].contentSizeDelta; self.font = [UIFont fontWithDescriptor:self.defaultFontDescriptor size:preferredSize]; [self invalidateIntrinsicContentSize]; } - (void)dealloc { [[NsnotificationCenter defaultCenter] removeObserver:self name:UIContentSizeCategoryDidChangeNotification object:nil]; } @end
UIApplication ContentSize.h
@interface UIApplication (ContentSizE) @property (nonatomic,readonly) NSInteger contentSizeDelta; @end
UIApplication ContentSize.m
#import "UIApplication+ContentSize.h" @implementation UIApplication (ContentSizE) - (NSInteger)contentSizeDelta { static NSArray *contentSizeCategories; static dispatch_once_t onCEToken; dispatch_once(&onCEToken,^{ contentSizeCategories = @[UIContentSizeCategoryExtrasmall,UIContentSizeCategorysmall,UIContentSizeCategorymedium,UIContentSizeCategoryLarge,UIContentSizeCategoryExtraLarge,UIContentSizeCategoryExtraExtraLarge,UIContentSizeCategoryExtraExtraExtraLarge UIContentSizeCategoryAccessibilitymedium,UIContentSizeCategoryAccessibilityLarge,UIContentSizeCategoryAccessibilityExtraLarge,UIContentSizeCategoryAccessibilityExtraExtraLarge,UIContentSizeCategoryAccessibilityExtraExtraExtraLarge]; }); // assume UIContentSizeCategoryLarge is default category NSInteger contentSizeDelta = [contentSizeCategories indexOfObject:self.preferredContentSizeCategory]; if(contentSizeDelta != NsnotFound) { contentSizeDelta -= [contentSizeCategories indexOfObject:UIContentSizeCategoryLarge]; return contentSizeDelta; } else { return 0; } } @end
以上是大佬教程为你收集整理的iOS7:我们可以使用除Helvetica Neue字体以外的动态类型吗?全部内容,希望文章能够帮你解决iOS7:我们可以使用除Helvetica Neue字体以外的动态类型吗?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。