My label size is W= 190 H =322
and the text in label is in center alignment like
I want an orange box character of line of text frame from UILabel
My label size is W= 190 H =322
and the text in label is in center alignment like
I want an orange box character of line of text frame from UILabel
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.
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];