objective-c - 如果我们使用系统字体,如何在 iOS 7 中重新排列字体大小?

标签 objective-c ios7 xcode4.5

尺寸不适用。

如果我想让字体大于或小于怎么办?

我该怎么办?

enter image description here

我认为这取决于 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/

相关文章:

ios - 如何优化 cellForRowAtIndexPath : code

iphone - UITableViewCell 背景颜色与 alpha 不同的行为

ios - 如何在 swift 中使自定义 UITableViewCell 中的链接可点击

ios - 在 iOS 中从 PNG 图像中提取形状轮廓点

iphone - 循环遍历 uitableview 部分以更改部分标题框架

xcode - 如何从 .p12 文件中获取发行版身份的名称

objective-c - 找不到用于迁移的映射模型 - UIManagedDocument 核心数据迁移

ios - sqlite数据库只更新1条记录

iphone - 如何在 UIWebView 上显示垂直或水平滚动条?

iphone - 我能否使使用 iOS7 SDK 构建的应用程序在 iOS 7 中看起来像 iOS 6 应用程序?