问题描述
我有 UILabel ,其中包含动态文字。有时文字太长而无法显示,因此自动截断。如何查找截断文本的可见部分的宽度?
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 of the visible part of truncated text?
sizeThatFits返回未截断文本的长度,所以目前我只能检测何时进行截断。需要知道多少是可见的,包括那三个点。任何提示?
sizeThatFits returns length of untruncated text, so at the moment I can only detect when truncation will be done. Need to know how much is visible, including those three dots. Any tips?
澄清:当文本被截断时,它通常比UILabel宽度短。
Clarification: when text is truncated, it's usually shorter than UILabel width.
推荐答案
机器人K是正确的。
如果我是你,我会做以下事情:
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];
这应该给你一个小于200的值(考虑到约束的最大尺寸和截断方法) 。
This should give you a value less than 200 (taking into account the constrained max size and truncation method).
这篇关于如何找出截断的UILabel文本的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!