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

use*_*951 4 objective-c xcode4.5 ios7

尺寸不适用.

如果我希望字体大于或小于?

我该怎么办?

在此输入图像描述

我认为这取决于UILabel的大小.但是,调整UILabel的大小将不起作用.

Ivi*_* M. 5

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
Run Code Online (Sandbox Code Playgroud)