How to find out width of truncated UILabel text

倖福魔咒の 提交于 2019-12-03 03:12:19

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

I don't understand why the width would be different that the width of the UILabel if the text is being truncated. Regardless, you can use sizeWithFont:constrainedToSize: to calculate the size of a string with a given font but limited to a "constraining size".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!