iOS7:除了Helvetica Neue字体,我们能否在动态类型中使用其他字体?

10

我们正在设计一款适用于iOS7的应用程序,我们的设计师想使用非默认字体(Avenir),但我不想失去动态类型功能。据我所知,动态类型只能与默认系统字体Helvetica Neue一起使用。在此刻,是否可以使用其他字体,还是不可行?


iOS7仍然受NDA保护,这意味着可能没有人会回答你的问题。 - rckoenes
抱歉,我以为使用黄金大师(Golden Masters)iOS7的DNA已经结束了 :) - tadasz
不是的,甚至在GM上面的黄色信息中都有说明。 - rckoenes
这是可能的,但太复杂了。由于保密协议,无法提供更多详细信息或链接。 - Sulthan
2个回答

16
根据我的理解,[UIFont preferredFontForTextStyle:]返回一个特定字体样式的固定大小字体,而不考虑文本视图默认大小。我期望在设置中更改文本大小会对我的应用程序中的文本大小产生一些变化,而不是设置固定值。如iOS文本编程指南所述,由文本样式描述的实际字体可以基于许多动态考虑因素而异,包括用户的内容大小类别偏好,它由UIApplication属性preferredContentSizeCategory表示。
我注意到属性preferredContentSizeCategory会在设置中更改文本大小时发生变化。
因此,我的想法是观察适当的通知,根据preferredContentSizeCategory属性计算大小差值,并将该差值应用于文本视图的默认字体大小(该大小在IB或以编程方式设置)。
同时,需要观察UIContentSizeCategoryDidChangeNotification通知,以便在用户更改内容大小类别时重新布局文本。当您的应用程序收到该通知时,它应向由自动布局放置的视图发送invalidateIntrinsicContentSize消息,或者向手动放置的用户界面元素发送setNeedsLayout。并且需要根据需要使优先字体或字体描述无效并获取新的。
@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上找到。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接