Chirone
Chirone

Reputation: 211

Can we know the space between text and the top of the UILabel?

With UILabels there seems to be a permanent border of emptiness around the text. Trying things like

label.layoutMargins = UIEdgeInsetsZero;

doesn't do anything and the gap between the top of the text and the top of the label seems to be proportionate to the font size.

Is there a way to make the gap go away? Or is there a way to know how big the gap is?

Upvotes: 0

Views: 160

Answers (1)

Houssem Sdiri
Houssem Sdiri

Reputation: 315

This should give you the gap value:

    CGSize labelSize=label.frame.size;
    CGSize textSize = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(labelSize.width, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
    // Assuming that the text is centered inside the label
    CGFloat topGap = (labelSize.height-textSize.height)/2

Upvotes: 1

Related Questions