我们正在为iOS7设计一个应用程序,我们的设计人员希望使用非默认字体(Avenir),但我不想松散动态类型功能。据我所知,Dynamic只能用于默认的系统字体,即Helvetica Neue。是否可以使用其他字体,或者此时它不是一种选择?
发布于 2013-09-26 09:50:28
据我所知,[UIFont preferredFontForTextStyle:]返回特定字体样式的固定大小的字体,而不管文本视图默认大小如何。我希望在设置中更改文本大小将改变我的应用程序中的文本大小,而不是设置固定值。正如在iOS文本编程指南中所指出的,
用于文本样式描述的实际字体可以根据许多动态考虑因素而有所不同,包括用户的内容大小类别首选项,它由UIApplication属性preferredContentSizeCategory表示。
我注意到属性preferredContentSizeCategory在设置文本大小时会发生变化。
观察UIContentSizeCategoryDidChangeNotification也很重要,这样您就可以在用户更改内容大小类别时重新布局文本。当应用程序收到通知时,它应该将invalidateIntrinsicContentSize消息发送到由自动布局定位的视图,或者将setNeedsLayout发送到手动定位的用户界面元素。它应该使首选字体或字体描述符失效,并根据需要获得新的字体。
因此,我的想法是观察适当的通知,基于preferredContentSizeCategory属性计算大小增量,并将增量应用于文本视图的默认字体大小(在IB中设置或以编程方式设置)。
PreferredFontLabel.h
@interface PreferredFontLabel : UILabel
@property (nonatomic) UIFontDescriptor *defaultFontDescriptor;
@endPreferredFontLabel.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];
}
@endUIApplication+ContentSize.h
@interface UIApplication (ContentSize)
@property (nonatomic, readonly) NSInteger contentSizeDelta;
@endUIApplication+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上使用。
发布于 2014-04-16 10:38:18
使用自定义字体的示例。
https://github.com/jszumski/dynamic-type
请参阅@implementation UIFont (AvenirContentSize)
有关如何调整字体以与默认大小匹配。
https://stackoverflow.com/questions/18758227
复制相似问题