2

I want to show the Text Like as below.

enter image description here

My content of Label may be changed & according to that my Label Frame should be Adjust Automatically.

How can I do this?

My Try

//NSString* string = @"Previously scheduled for every Monday, Happy will now be moving to Wednesdays at the same time. The time from 5:00 pm - 7:00 pm will remain the same. Now starting April 14th until further notice.";

   NSString* string=@"ABCDEF";
    NSMutableParagraphStyle *style  = [[NSMutableParagraphStyle alloc] init];
    style.minimumLineHeight = 30.f;
    style.maximumLineHeight = 30.f;
    style.firstLineHeadIndent=100.0f;
    NSDictionary *attributtes = @{NSParagraphStyleAttributeName : style,};
    lbl_notification.attributedText = [[NSAttributedString alloc] initWithString:string attributes:attributtes];
user2893370
  • 691
  • 8
  • 22

4 Answers4

2

Set your label text

Yourlabel.text=[NSString stringWithFormat:@"Notification:    %@",Dynamic text];

Yourlabel.adjustsFontSizeToFitWidth = YES;
Pradhyuman sinh
  • 3,938
  • 1
  • 19
  • 38
1

Try this, it will shrink the size of font if text it is too much , in order to fit that in labe:

 displayLabel.adjustsFontSizeToFitWidth = YES;
Rana
  • 451
  • 4
  • 16
1

This works for me:

     _label = [[UILabel alloc] init];
     _label.lineBreakMode = NSLineBreakByWordWrapping;
     _label.numberOfLines = 0;
    _label.text = .....
    [_label setFrame:CGRectMake(0.0f, 0.0f, 340.0f, 300.0f)];
    _labelSize = [_label.text sizeWithFont:_label.font constrainedToSize:_label.frame.size lineBreakMode:NSLineBreakByWordWrapping];
    [_label setFrame:CGRectMake(0.0f, 0.0f, 340.0f, _labelSize.height)];
Greg
  • 24,353
  • 5
  • 47
  • 60
1

If you want the label frame to be changed according to the text, the below code will be helpful,

CGSize Size1 = yourlabel.bounds.size;
CGSize Size2 = CGRectInfinite.size;
Size2.width = Size1.width;

Size2 = [yourlabel.text sizeWithFont:yourlabel.font constrainedToSize:Size2];

yourlabel.frame = CGRectMake(yourlabel.frame.origin.x, yourlabel.frame.origin.y, Size2.width, Size2.height);
Nazik
  • 8,393
  • 26
  • 72
  • 115