简体   繁体   中英

UILabel Font and height in IOS

I see that UILabel in IOS has height property along with font . I also see that if I give the height and font same value, for some characters the text gets cut in UILabel. How are these two different? While doing the UI design if height is more than the font size, there would be some extra white space which is what I want to avoid and hence wanted to know what exactly is the difference between the two.

You can use sizeThatFits: to determine correct height for UILabel

UILabel *label;
label.text = @"Some text";
CGRect labelFrame = label.frame;
labelFrame.size.height = [label sizeThatFits:CGSizeMake(labelFrame.size.width), MAXFLOAT].height;
label.frame = labelFrame;

The text size is the height from the font's baseline to its cap line, which doesn't take into consideration descenders (like on the letters 'g' and 'y') or ascenders (like 'f', in some fonts). In general, you don't have to (or even need to) set a label's height; it determines the best height based on its font size.

See https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/CustomTextProcessing/CustomTextProcessing.html for details about how font sizes work. 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM