iOS7:我们可以使用除Helvetica Neue字体以外的动态类型吗?

iOS7:我们可以使用除Helvetica Neue字体以外的动态类型吗?,第1张

概述我们正在为iOS7设计一个应用程序,我们的设计师希望使用非默认字体(Avenir),但我不想松动Dynamic Type功能.据了解,Dynamic Type只能使用默认的系统字体,即Helvetica Neue.是否可以使用其他字体,在这一刻它不是一个选择? 据我所知[UIFont preferredFontForTextStyle:]返回具有固定大小的特定字体的字体,无论文本视图的默认大小如何 我们正在为iOS7设计一个应用程序,我们的设计师希望使用非默认字体(Avenir),但我不想松动Dynamic Type功能.据了解,Dynamic Type只能使用默认的系统字体,即Helvetica Neue.是否可以使用其他字体,在这一刻它不是一个选择?解决方法 据我所知[UIFont preferredFontForTextStyle:]返回具有固定大小的特定字体的字体,无论文本视图的默认大小如何.我希望在“设置”中更改文本大小可以通过某种增量更改我的应用中的文字大小,而不是设置固定值.如 Text Programming Guide for iOS年所述,

The actual Font used for the purpose described by a text style can vary based on a number of dynamic consIDerations,including the user’s content size category preference,which is represented by the UIApplication property preferredContentSizecategory.

我注意到属性preferredContentSizecategory响应于设置中的文本大小而改变.

It’s also important to observe the UIContentSizecategoryDIDChangeNotification so that you can re-layout the text when the user changes the content size category. When your app receives that notification,it should send the invalIDateIntrinsicContentSize message to vIEws positioned by auto Layout or send setNeedsLayout to user interface elements positioned manually. And it should invalIDate preferred Fonts or Font descriptors and acquire new ones as needed.

所以,我的想法是观察适当的通知,根据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

我添加了归因字符串支持,演示可在GitHub

总结

以上是内存溢出为你收集整理的iOS7:我们可以使用除Helvetica Neue字体以外的动态类型吗?全部内容,希望文章能够帮你解决iOS7:我们可以使用除Helvetica Neue字体以外的动态类型吗?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1094988.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存