How to find out width of truncated UILabel text

后端 未结 2 1143
伪装坚强ぢ
伪装坚强ぢ 2021-02-06 07:20

I have UILabel, which contains dynamic text. Sometimes text is too long to be shown and thus automagically truncated. How do I find out width o

2条回答
  •  南笙
    南笙 (楼主)
    2021-02-06 07:38

    Robot K is correct.

    If I was you I'd do the following:

      UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 24)];
      label.text = @"this is some really long text that i want in a small label";
      [view addSubview:label];
    
      CGSize size = [label.text sizeWithFont:label.font constrainedToSize:label.frame.size  
                     lineBreakMode:label.lineBreakMode];
    

    This should give you a value less than 200 (taking into account the constrained max size and truncation method).

提交回复
热议问题