具有恒定大小和动态字体大小取决于文本的 UILabel
我的 UIView 上有 UILabel。我总是为我的标签获取各种数据(从后端)。我有一些在视图上显示文本的矩形(我将其设置为 uilabel 大小,该大小是恒定的)。
如何为我的 UILabel 设置动态字体的大小? 我只需要为我的矩形边界显示具有实际字体大小的文本数据。如果文本真的很长,并且最小字体将包裹文本 - 我需要应用最小字体大小来显示更多文本数据。
(我的标签可以是多行,其大小可以调整为常量矩形边界)
I have UILabel on my UIView. I always get various data for my label(from backend). I have some rect to display text on View (i set it as uilabel size, this size is constant).
How to set dynamic font for my UILabel for its size?
I just need to display text data with actual font size, for my rect bounds. If text really long, and minimal font will wrap text - i need to apply minimal font size to display more text data.
(my label can be multi lines to be sized to const rect bounds)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
xib 中的 UILable - adjustmentToFitWidth 中有一个属性,您可以在其中指定最小字体大小。因此,如果您的文本变长,它会自动减小字体大小以在标签内进行调整。
注意:在 adjustmentToFitWidth 中给出的任何值都是最小限制,因此您的字体大小始终大于或等于该值。
享受编码的乐趣:)
There is property in UILable - adjustToFitWidth in xib, where you can give minimum font size. So if your text is getting longer then it will automatically reduce font size to adjust it within the label.
Note: whatever value given in adjustToFitWidth, is minimum limit, so your font size always be greater or equals to that value.
Enjoy Coding :)
我会这样做:获取 NSString 字符数。根据该字符数计算大小。
您将使用变量 PUT_A_VALUE_HERE 直到获得所需的结果。之后,更改字体大小
I would do this in this way: Get the NSString character count. Based on that character count, calculate the size.
You are going to play with the variable PUT_A_VALUE_HERE until you get desired results. After that, change the font size