在ios sdk中将标签文本显示为段落

时间:2013-12-02 10:57:26

标签: ios iphone objective-c uilabel text-alignment

我想将Text Like显示如下。

enter image description here

我的标签内容可能会更改&根据我的标签框架应该自动调整。

我该怎么做?

我的尝试

//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];

4 个答案:

答案 0 :(得分:2)

设置标签文字

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

Yourlabel.adjustsFontSizeToFitWidth = YES;

答案 1 :(得分:1)

试试这个,如果文字太多,它会缩小字体的大小,以便适合labe:

 displayLabel.adjustsFontSizeToFitWidth = YES;

答案 2 :(得分:1)

这对我有用:

     _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)];

答案 3 :(得分:1)

如果您希望根据文字更改标签框,下面的代码将会很有用,

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);