作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
尺寸不适用。
如果我想让字体大于或小于怎么办?
我该怎么办?
我认为这取决于 UILabel 的大小。但是,调整 UILabel 的大小将不起作用。
最佳答案
iOS 7 使用一种叫做动态类型的东西。无需指定确切的字体及其大小和所有内容,您可以通过语义描述它,例如,该字体用于标题、正文或任何您想要的。实际字体取决于可以动态更改的各种参数,其中之一是用户在“首选项/常规/文本大小”中选择的首选字体大小。您不能只选择标题的大小,因为那样会使整个概念变得毫无意义。这不是您的选择,而是用户的选择。您只需要倾听用户的选择并做出相应的回应。但是,您可以通过编程方式缩放首选字体。 UIFontDescriptor对象用于获取当前标题大小,之后 fontWithDescriptor:size:
方法用于获取具有相同描述符但新缩放大小的新字体。
@interface SomeViewController ()
@property (weak, nonatomic) IBOutlet UILabel *headlineLabel;
@end
@implementation SomeViewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// We need to setup our fonts whenever controller appears.
// to account for changes that happened while
// controller wasn't on screen.
[self setupFonts];
// Subscribing to UIContentSizeCategoryDidChangeNotification
// to get notified when user chages the preferred size.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(preferredFontChanged:)
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillAppear:animated];
// Unsubscribe from UIContentSizeCategoryDidChangeNotification.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
- (void)setupFonts
{
// In this method we setup fonts for all the labels and text views
// by calling 'preferredFontForTextStyle:scale:'.
self.headlineLabel.font = [self preferredFontForTextStyle:UIFontTextStyleHeadline scale:0.8];
}
- (UIFont *)preferredFontForTextStyle:(NSString *)style scale:(CGFloat)scale
{
// We first get prefered font descriptor for provided style.
UIFontDescriptor *currentDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:style];
// Then we get the default size from the descriptor.
// This size can change between iOS releases.
// and should never be hard-codded.
CGFloat headlineSize = [currentDescriptor pointSize];
// We are calculating new size using the provided scale.
CGFloat scaledHeadlineSize = lrint(headlineSize * scale);
// This method will return a font which matches the given descriptor
// (keeping all the attributes like 'bold' etc. from currentDescriptor),
// but it will use provided size if it's greater than 0.0.
return [UIFont fontWithDescriptor:currentDescriptor size:scaledHeadlineSize];
}
- (void)preferredFontChanged:(NSNotification *)notification
{
[self setupFonts];
}
@end
关于objective-c - 如果我们使用系统字体,如何在 iOS 7 中重新排列字体大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19993022/
我是一名优秀的程序员,十分优秀!