的UILabel:调整字体大小与自动版式高度 [英] UILabel: adjust font size to height with AutoLayout

查看:265
本文介绍了的UILabel:调整字体大小与自动版式高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有几个标签以下任一其他。本的尺寸与自动版式,我想有字体大小尽可能大。 adjustsFontSizeToFitWidth 仅适用于宽度,但我得到以下结果。

I'd like to have several labels one below the other. The are sized with AutoLayout, I'd like to have the font size as big as possible. adjustsFontSizeToFitWidth only works for the width, but the I get the following result

推荐答案

如果您的字体是恒定的,你可以从像这样的字体计算出标签的必要高度:

If your font is constant, you can calculate the necessary height of the labels from the font like so:

UIFont *font = [UIFont fontWithName:@"Helvetica" size:18];
UILabel *label = [UILabel new];
[label setFont:font];
[view addSubview:label];

double labelHeight = font.pointSize + fabs(font.descender);
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[label(labelHeight)]"
                                                             options:0
                                                             metrics:@{@"labelHeight" : @(labelHeight)}
                                                               views:@{@"label" : label}]];

这将保证该字体的整个高度将设置约束时加以考虑。

This will guarantee that the full height of the font will be taken into account when setting constraints.

这篇关于的UILabel:调整字体大小与自动版式高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆