微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我们正在为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

原文地址:https://www.jb51.cc/iOS/328906.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐