gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 11:01:06 27 4
gpt4 key购买 nike

我们正在为 iOS7 设计一个应用程序,我们的设计师想要使用非默认字体 (Avenir),但我不想失去动态类型功能。据我了解,动态类型只能与默认系统字体一起使用,即 Helvetica Neue。是否可以使用其他字体或目前无法使用?

最佳答案

据我所知,[UIFont preferredFontForTextStyle:] 为特定字体样式返回固定大小的字体,而不管 TextView 的默认大小如何。我希望在“设置”中更改文本大小会通过一些增量更改我应用程序中的文本大小,而不是设置固定值。如 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 属性计算大小增量,并将增量应用于 TextView 的默认字体大小(在 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 以外的动态类型字体吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18758227/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com