iOS7:我们可以使用除动态类型以外的Helvetica Neue字体吗? [英] iOS7: Can we use other than Helvetica Neue fonts with Dynamic Type?

查看:194
本文介绍了iOS7:我们可以使用除动态类型以外的Helvetica Neue字体吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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


用于文本样式可以根据一些动态考虑因素而变化,包括用户的内容大小类别偏好,其由UIApplication属性preferredContentSizeCategory表示。


我注意到属性 preferredContentSizeCategory 在响应设置文本大小的变化。


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

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

PreferredFontLabel.h

  @interface PreferredFontLabel:UILabel 

@property(nonatomic)UIFontDescriptor * defaultFontDescriptor;

$ end


$ b

PreferredFontLabel.m

  #importPreferredFontLabel.h
#importUIApplication + ContentSize.h

@implementation PreferredFontLabel

- (id)init
{
self = [super init];
if(self){
[self setup];
}
返回自我;

$ b $ - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self){
[self setup];
}
返回自我;

$ b - (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self){
[self setup];
}
返回自我;
}

- (void)setup
{
self.defaultFontDescriptor = self.font.fontDescriptor;
$ b [[NSNotificationCenter defaultCenter]
addObserver:self
选择器:@selector(contentSizeCategoryDidChange)
名称: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];

$ b - (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIContentSizeCategoryDidChangeNotification object:nil];

$ b $ end


$ UIApplication + ContentSize.h

  @interface UIApplication(ContentSize)

@property(nonatomic,只读)NSInteger contentSizeDelta;

@end

UIApplication + ContentSize.m



$ $ p $ $ $ $ $ $
$ $ $
$ $ $
$ b $ (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];
});

//假设UIContentSizeCategoryLarge是默认类别
NSInteger contentSizeDelta = [contentSizeCategories indexOfObject:self.preferredContentSizeCategory];

if(contentSizeDelta!= NSNotFound){
contentSizeDelta - = [contentSizeCategories indexOfObject:UIContentSizeCategoryLarge];

返回contentSizeDelta;
} else {
return 0;
}
}

@end

I添加了归因字符串支持,演示可在 GitHub 上获得


We are designing an app for iOS7 and our designer wants to use non-default font (Avenir), but I dont want to loose Dynamic Type functionality. As I understand Dynamic Type can only be used with default system font, which is Helvetica Neue. Is it possible to use other fonts or at this moment it's not an option?

解决方案

As far as I understood [UIFont preferredFontForTextStyle:] returns a font with fixed size for a particular font style regardless of text view default size. I expect that changing text size in Settings will change text sizes in my app by some delta instead of setting fixed value. As noted in 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.

I noticed that property preferredContentSizeCategory changes in response to setting text size in Settings.

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.

So, my idea is to observe appropriate notification, calculate size delta based on preferredContentSizeCategory property and apply delta to text view's default font size (which was set in IB or programmatically).


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

I added attributed string support, demo is available on GitHub

这篇关于iOS7:我们可以使用除动态类型以外的Helvetica Neue字体吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆