首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS7:除了Helvetica字体之外,我们还可以使用动态类型字体吗?

iOS7:除了Helvetica字体之外,我们还可以使用动态类型字体吗?
EN

Stack Overflow用户
提问于 2013-09-12 07:36:16
回答 2查看 3.1K关注 0票数 10

我们正在为iOS7设计一个应用程序,我们的设计人员希望使用非默认字体(Avenir),但我不想松散动态类型功能。据我所知,Dynamic只能用于默认的系统字体,即Helvetica Neue。是否可以使用其他字体,或者此时它不是一种选择?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-09-26 09:50:28

据我所知,[UIFont preferredFontForTextStyle:]返回特定字体样式的固定大小的字体,而不管文本视图默认大小如何。我希望在设置中更改文本大小将改变我的应用程序中的文本大小,而不是设置固定值。正如在iOS文本编程指南中所指出的,

用于文本样式描述的实际字体可以根据许多动态考虑因素而有所不同,包括用户的内容大小类别首选项,它由UIApplication属性preferredContentSizeCategory表示。

我注意到属性preferredContentSizeCategory在设置文本大小时会发生变化。

观察UIContentSizeCategoryDidChangeNotification也很重要,这样您就可以在用户更改内容大小类别时重新布局文本。当应用程序收到通知时,它应该将invalidateIntrinsicContentSize消息发送到由自动布局定位的视图,或者将setNeedsLayout发送到手动定位的用户界面元素。它应该使首选字体或字体描述符失效,并根据需要获得新的字体。

因此,我的想法是观察适当的通知,基于preferredContentSizeCategory属性计算大小增量,并将增量应用于文本视图的默认字体大小(在IB中设置或以编程方式设置)。

PreferredFontLabel.h

代码语言:javascript
复制
@interface PreferredFontLabel : UILabel

@property (nonatomic) UIFontDescriptor *defaultFontDescriptor;

@end

PreferredFontLabel.m

代码语言:javascript
复制
#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

代码语言:javascript
复制
@interface UIApplication (ContentSize)

@property (nonatomic, readonly) NSInteger contentSizeDelta;

@end

UIApplication+ContentSize.m

代码语言:javascript
复制
#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上使用。

票数 16
EN

Stack Overflow用户

发布于 2014-04-16 10:38:18

使用自定义字体的示例。

https://github.com/jszumski/dynamic-type

请参阅@implementation UIFont (AvenirContentSize)

有关如何调整字体以与默认大小匹配。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18758227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档