How to remove the extra padding below an one line UILabel

独自空忆成欢 提交于 2020-06-26 07:16:04

问题


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).

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.English words -- oneline (no extra padding)[test words:today 0987776]

2.Chinese words -- oneline (some extra padding)[test words:今天天气好啊!]

Hope someone would help . Thx a lot.


回答1:


use sizeToFit on a label to the size required by the text


[yourlabelName sizeToFit];

any other padding would be from the font, the label itself doesn't apply any other padding




回答2:


// baselineOffset, It was totally hit and miss Hope to help you

[attributedString addAttribute:NSBaselineOffsetAttributeName value:@(baselineOffset) range:range];



来源:https://stackoverflow.com/questions/33969276/how-to-remove-the-extra-padding-below-an-one-line-uilabel

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