如何调整和使UILabel的宽度适合文本大小? [英] How to adjust and make the width of a UILabel to fit the text size?

查看:184
本文介绍了如何调整和使UILabel的宽度适合文本大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,有一个带文本的 UILabel 。字体大小为16pt。文本内容根据不同情况而改变。我希望它可以自动调整 UILabel 的宽度,以适应没有拉伸的文本总宽度。

In my project, there is a UILabel with text. The font size is 16pt. The text contents are changed depending on different cases. I hope it can automatically adjust the width of UILabel to fit the total width of texts without stretching.

是吗可能吗?

推荐答案

这假设你已经设置了字体:

This assumes you have already set the font:

label.text = @"some text";
[label sizeToFit];

您还需要定义最大宽度,并告诉您的程序如果sizeToFit为您提供了什么宽度大于该最大值。

You will also need to define a maximum width, and tell your program what to do if sizeToFit gives you a width greater than that maximum.

这篇关于如何调整和使UILabel的宽度适合文本大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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