0

My label size is W= 190 H =322 and the text in label is in center alignment likeenter image description here

I want an orange box character of line of text frame from UILabel

Gherbi Hicham
  • 2,416
  • 4
  • 26
  • 41
Keyur
  • 180
  • 1
  • 5
  • 20
  • 1
    I want a ferrari, my own mansion and enough money to do anything I want. I won't get any of this unless i put some effort in though, i suggest you do the same with your problem. Instead of saying what you want, attempt the problem and, if still having problems, come back and display what you tried. See [How to ask a good question](http://stackoverflow.com/help/how-to-ask) to help guide you – Takarii Aug 03 '16 at 08:18

1 Answers1

0

The frame of your UILabel should be the same size as I'ts content. Wrapped. This is how you do it:

Without auto layout:

You should call sizeToFit on UILabel to shrink it to the text size, and then check I'ts frame.

textLabel.text = "@yourText";
textLabel.numberOfLines = 1;
[textLabel sizeToFit];
CGSize size = CGSizeMake(textLabel.frame.size.width, textLabel.frame.size.height);

With autolayout:

Just set "numberOfLines" to 0, and don't give the label a width constraint, and the label size will fit automatically.

EDIT

Core text:

UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.text = @"Your text";
float yourFontSize = 20;
while ([label.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:yourFontSize]}].width > modifierFrame.size.width)
{
     yourFontSize--;
}
label.font = [UIFont systemFontOfSize:yourFontSize];
Community
  • 1
  • 1
MCMatan
  • 8,623
  • 6
  • 46
  • 85