UILabel在IOS7中错误地呈现文本 [英] UILabel render text incorrectly in IOS7
问题描述
我使用以下代码来计算 UILabel的界限
I use following code to calculate the bound of a UILabel
CGRect bound = [lblName.text boundingRectWithSize:(CGSize){206, 99999}
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:stringAttributes
context:nil];
UILabel
是嵌入在 UIScrollView
,这是 UITableViewCell
的子视图。
The UILabel
is a embedded in a UIScrollView
, which is a subview of UITableViewCell
.
这里我得到了什么
我在表格单元格中使用 UILabel
进行了测试,并且 UILabel
分别在 UIScrollView
中,结果如我所料
I made a test which use a UILabel
in a table cell, and a UILabel
in UIScrollView
separately, and results are as I expected
请注意所有设置(字体,换行模式等)在所有这些情况下, UILabel
都是相同的。在所有这些情况下, boundingRectWithSize
返回相同的结果,唯一的区别是 UILabel
呈现文本的方式。
Note that all setting (font, line break mode etc) of UILabel
are the same in all those case. The boundingRectWithSize
returns same result in all those case, only difference is the way UILabel
render the text.
这是什么问题?我错过了什么吗?
What is the problem here? did i miss sometthing?
UPDATE :只有当我从nib加载UILabel时才会发生这种情况,如果是以编程方式创建的,则没有问题。 (我的项目从xcode 4迁移到xcode 5)
UPDATE: this happen only when i load UILabel from nib, if it is created programmatically, there is no problem. (my project is migrated from xcode 4 to xcode 5)
推荐答案
我看到一些标签出现了同样的行为,在iOS 6中显得很好,但在iOS 7中,它们在图片的顶部和底部都有额外的填充。
I was seeing the same behavior with some of my labels, which looked fine in iOS 6, but in iOS 7 they had extra padding at the top and bottom as in your pictures.
这是我必须要做的才能最终得到它在 viewDidLoad
中正确布局 - 适用于iOS 6和7。
Here's what I had to do to finally get it to layout correctly in viewDidLoad
- works on both iOS 6 and 7.
self.someLabel.autoresizingMask = UIViewAutoresizingNone;
self.someLabel.frame = CGRectMake(
self.someLabel.frame.origin.x,
self.someLabel.frame.origin.y,
labelWidth, // define elsewhere if you're targeting different screen widths
self.someLabel.bounds.size.height);
[self.someLabel sizeToFit];
这篇关于UILabel在IOS7中错误地呈现文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!