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

4

大小不适用。

如果我想要字体比较大或小怎么办?

我该怎么做?

enter image description here

我原以为这取决于UILabel的大小。但是,调整UILabel的大小并不能起作用。

4个回答

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

2
希望你需要这个,
label.font = [UIFont fontWithName:@"HelveticaNeue-Light"  size:20 ];
             OR     
label.font = [UIFont systemFontOfSize:20];

这里列出了所有的iOS系统字体


我不想要Verdana字体,我想要系统字体。 - user4951
OP在询问如何更改TextStyles的大小。你的答案是将其替换为固定字体,这是不正确的。 - Aleksandar Vacić

0

不确定您所说的“大小不适用”是什么意思。

您可以在.xib中更改UILabel的字体大小。打开它,在右侧的实用程序面板中打开属性选项卡。您将在那里看到您的字体设置,也可以更改大小。

如果您想以编程方式执行此操作,则命令为:

float fontSize = (whatever you want the size to be);
myUILabel.font = [UIFont systemFontOfSize:fontSize];

0
你需要这个:
label.font = [[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline] fontWithSize:label.font.pointSize * scale];

其中scale是您调整原始字体大小的比例。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接