How to resize UILabels to fit content?

∥☆過路亽.° 提交于 2019-12-23 04:45:45

问题


My program currently has thirty UILabels that may contain numbers between one and three digits. I want my UILabels to expand and shrink horizontally as the numbers within them change size. At the moment the text is shrinking and the UILabels size remains the same.

If this can be done just using the xib file this would be preferable to doing so programmatically but if this isn't possible either would be great.

Thanks


回答1:


After setting the text of the label, call sizeToFit on the label. This will make the label just big enough for the text.

BTW - why do you need to do this? Why not just make the labels big enough for the possible values and leave them that size? What benefit do you think you will get by shrinking the labels?



来源:https://stackoverflow.com/questions/13237353/how-to-resize-uilabels-to-fit-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!