如何判断UILabel何时被截断和/或其换行符位置将会改变 [英] How to tell when a UILabel will be truncated an/or its line break position will change
问题描述
我有一个多行 UILabel
( numberOfLines
= 0)。它的宽度可以在运行时改变,有时这导致截断和/或重新包装。一些示例最好地说明了这一点:
示例1:宽度减少导致不同的换行符
>
示例2:宽度减小导致截断
示例3:宽度减少导致截断和不同的换行符位置
示例4:宽度减少对截断或换行符位置没有任何影响
由于格式化的变化可能相当刺激,我打算掩盖它一些动画(可能是淡入/淡出)。但是,第一个障碍是确定何时我需要这样做。我不想在标签重新调整大小时应用动画 - 只有当它会导致截断或换行符位置发生更改时。
我如何测试这个?测试应该返回YES,例如1,2和3,但不是例如4.
注意:调整大小绝不会改变我的示例中的行数。
注意2:如果有人有更好的标签与文本格式相关,我很想知道他们 - 随时可以编辑。
注意3:如果你有兴趣看到这个行为完成,请在iPhone上尝试Apple的mail.app。查看收件箱时,滑动电子邮件,并在其重新换行和/或截断时(但不需要时)淡入/淡出汇总行。
您可以知道显示特定 NSString
实例所需的标签大小。例如,你可以使用那个:
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize: (CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode
如果使用指定的约束来渲染字符串,则返回字符串的大小。
所以你想要的是获得 CGSize
特殊字符串并检查它是否不大于 UILabel
size:
UILabel * label;
CGSize sizeNeeded = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(label.bounds.size.width,MAXFLOAT)];
if(sizeNeeded.height> label.bounds.size.height)
{
NSLog(@String is truncated);
}
更有用 NSString
方法,您可以在这里找到: NSString UIKit添加参考
好的,另一种方式来做你想做的事情:
1)创建2 UILabel
具有相同的属性,但第二个( label2
)将与另一个 c $ c>。
2)设置 alpha 属性
label2 $ c
3)当编辑模式开始时,这样的动画:
// label1.alpha == 1.0,label2.alpha == 0.0
{[UIView animateWithDuration:0.5 animations: ^ {
label1.alpha = 0.0;
label2.alpha = 1.0;
}];
4)编辑模式结束时:
{[UIView animateWithDuration:0.5 animations:^ {
label1.alpha = 1.0;
label2.alpha = 0.0;
}];
这将给出与Mail.app中相同的结果
I have a multi-line UILabel
(numberOfLines
= 0). It's width can change at runtime, and sometimes this leads to truncation and/or re-wrapping. Some examples illustrate this best:
Example 1: the reduction in width leads to a different line break point
Example 2: the reduction in width leads to truncation
Example 3: the reduction in width leads to both truncation and a different line break position
Example 4: the reduction in width does not have any effect on truncation or line break position
Since this change in formatting can be quite jarring, I intend to mask it behind some animation (probably a fade in/fade out). However, the first hurdle is identifying when I need to do this. I don't want to apply the animation whenever the label re-sizes - only when it will cause a change in either truncation or line break positions.
How might I test this? The test should return YES for example 1, 2, and 3, but NO for example 4.
Note: the resizing will never alter the number of lines in my example.
Note 2: if anyone has some better tags related to text formatting I'd love to know them - feel free to edit.
Note 3: if you are interested in seeing this behavior accomplished, try Apple's mail.app on the iPhone. When viewing the inbox, swipe an email and watch the summary line fade-in/out as it re-wraps and/or truncates (but not when it doesn't need to).
You could know the size of label that is needed to display a particular NSString
instance. For example, you could use that one:
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode
Returns the size of the string if it were rendered with the specified constraints.
So what you want is to get CGSize
for a particular string and check if it is not larger then UILabel
size:
UILabel *label;
CGSize sizeNeeded = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(label.bounds.size.width, MAXFLOAT)];
if (sizeNeeded.height > label.bounds.size.height)
{
NSLog(@"String is truncated");
}
More useful NSString
methods you could find here: NSString UIKit Additions Reference
Ok, another way of doing what you want:
1) Create 2 UILabel
with the same properties but second one (label2
) will be with another one width
.
2) Set alpha
property of label2
to 0.0
in non-edit mode.
3) When edit mode begins make such animation:
// label1.alpha == 1.0, label2.alpha == 0.0
{[UIView animateWithDuration:0.5 animations:^{
label1.alpha = 0.0;
label2.alpha = 1.0;
}];
4) When edit mode ends:
{[UIView animateWithDuration:0.5 animations:^{
label1.alpha = 1.0;
label2.alpha = 0.0;
}];
That will give you the same result as in Mail.app
这篇关于如何判断UILabel何时被截断和/或其换行符位置将会改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!