I
I
int02h2012-12-03 15:44:26
iOS
int02h, 2012-12-03 15:44:26

How to set size for UILabel so that there is no padding between text and top border?

Hello, dear Habra community!
Most likely a noob question or I didn’t search well on Google, but still I ask you to help.
I have UI Label. When I call SizeToFit() I get a frame whose height is greater than the height of the text.
8ab05277127a401516b143504cb871e7.png
(marked with label's Frame in red)
I think there is space for a capital letter at the top, but it's not in my text. Is there any way to make the text fit snugly against the top border of the UILabel? Thanks for the help!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
IbrahimKZ, 2012-12-03
@IbrahimKZ

Why not write this after calling sizeToFit:

yourLabel.frame = CGRectMake(yourLabel.frame.origin.x, yourLabel.frame.origin.y, yourLabel.frame.size.width, 14.0);

If you want to force 14.

K
Krypt, 2012-12-04
@Krypt

UIKit has such a header, UIStringDrawing.h. I don't remember if it either needs to be explicitly connected, or it is connected together with UIKit.
There's this thing: sizeWithFont
developer.apple.com/library/ios/documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html#//apple_ref/occ/instm/NSString/sizeWithFont :
The result is this slightly strange construction:

CGSize textSize = [label.title sizeWithFont:label.font];
[label setFrame:CGRectmake(label.frame.origin.x, label.frame.origin.y, textSize.width, textSize.heidht)];

I
iStyx, 2012-12-04
@iStyx

Setting Baseline to None doesn't help?

E
Egor Merkushev, 2012-12-04
@egormerkushev

I think that the author of habrahabr.ru/company/appsministry/blog/144942/ will help you. Write him.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question