【问题标题】:Show Label Text as Paragraph in ios sdk在 ios sdk 中将标签文本显示为段落
【发布时间】:2013-12-18 01:26:16
【问题描述】:

我想显示如下文本。

我的标签内容可能会改变,据此我的标签框架应该自动调整。

我该怎么做?

我的尝试

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

【问题讨论】:

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


【解决方案1】:

设置标签文本

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

Yourlabel.adjustsFontSizeToFitWidth = YES;

【讨论】:

    【解决方案2】:

    如果你想根据文字改变标签框,下面的代码会有所帮助,

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

    【讨论】:

      【解决方案3】:

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

       displayLabel.adjustsFontSizeToFitWidth = YES;
      

      【讨论】:

        【解决方案4】:

        这对我有用:

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

        【讨论】:

          猜你喜欢
          • 2022-01-14
          • 2018-05-09
          • 1970-01-01
          • 1970-01-01
          • 2018-12-11
          • 1970-01-01
          • 2012-05-31
          • 1970-01-01
          • 2016-09-14
          相关资源
          最近更新 更多