如何删除一行 UILabel 下方的额外填充 [英] How to remove the extra padding below an one line UILabel
问题描述
我想用一个UILabel来显示一些从服务器发送过来的词,这些词我设置为NSMutableAttributeString有时只占一行(只有在这种情况下,问题才会出现).
I wanna to use a UILabel to show some words which is sent from Server,these words which i set them as NSMutableAttributeString sometimes only occupy one line (only in this case, the problem comes).
我打算获得标签占用的确切空间,但是当标签只占用一行时,非常重要 - 我使用 lineSpacing 设置 NSMutableAttributeString (10px,NSMutableParagraphStyle),但这,出乎意料的是,即使这里只有一行,问题也可以显示为单词下方的一些额外填充(我用一些中文单词进行测试,但注意:如果单词占用多于一行,则没有问题).和奇怪的故事是——如果单词是纯英文或阿拉伯数字,则不存在多余的填充)
I intend to get the exact space which the label would take up,but when the label only takes up one line,and very important — i set the NSMutableAttributeString with lineSpacing (10px,NSMutableParagraphStyle),but this,unexpectedly, would also works when even there is only one line here — the problem shows up as some extra padding below the word (i test with some chinese word,but attention: if the words takes up more than one line, there is no problem). and the weird story is — if the word is purely english or Arabic numerals,the extra padding won't exist)
下面的图片可能会更清楚地说明问题:
the following pics may shows the problem a little clearly:
1.英文单词——单行(无额外填充)[测试词:今天 0987776]
1.English words -- oneline (no extra padding)[test words:today 0987776]
2.中文单词——oneline (some extra padding)[测试词:今天天气好啊!]
2.Chinese words -- oneline (some extra padding)[test words:今天天气好啊!]
希望有人帮忙.非常感谢.
Hope someone would help . Thx a lot.
推荐答案
在标签上使用 sizeToFit
到文本所需的大小
use sizeToFit
on a label to the size required by the text
[yourlabelName sizeToFit];
任何其他 padding
将来自字体,标签本身不应用任何其他填充
any other padding
would be from the font, the label itself doesn't apply any other padding
这篇关于如何删除一行 UILabel 下方的额外填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!