如何让 UIView 在边界更改时显示文本,使其表现得像 UILabel 一样?
我有一个 UIView,需要在 drawRect 中绘制文本:
- (void)drawRect:(CGRect)rect {
...
[@"some text" drawAtPoint:somePoint withFont:someFont];
...
}
因为文本需要特殊格式,所以我不能只使用 UILabel。
在我旋转设备之前,它看起来很好。然后,我的自定义 UIView 的大小发生变化(在父视图的 layoutSubviews
方法中),文本在一个方向上被拉伸,在另一个方向上被压扁。
当我用 UILabel 替换视图时,文本看起来总是很棒,即使视图的边界发生变化也是如此。
如何让我的视图表现出与 UILabel 相同的行为?
我已经研究过一些事情,但尚未取得成功:
- 将视图层的needsDisplayOnBoundsChange 设置为YES。
- 将视图的 contentStretch 设置为 CGRectZero。
- 在我的视图的layoutSubviews 中调用setNeedsDisplay。
也许我没有做对其中一件事情。还有其他人遇到过这个吗?
更新:按照 James Huddleston 的回答中的建议,我将视图的 contentMode
属性设置为 UIViewContentModeRedraw
,这让我完成了部分工作。现在,动画结束时的文本显示正确。但是,在动画开始时,文本会被压扁/拉伸以适合最终尺寸,然后在动画过程中不再压扁/拉伸。 UILabel 的情况并非如此。
I have a UIView in which I need to draw text in drawRect:
- (void)drawRect:(CGRect)rect {
...
[@"some text" drawAtPoint:somePoint withFont:someFont];
...
}
Because the text requires special formatting, I cannot just use a UILabel.
It looks fine until I rotate the device. Then the size of my custom UIView changes (in the parent view's layoutSubviews
method), and the text becomes stretched in one direction and squished in the other.
When I replace my view with a UILabel, the text always looks great, even when the bounds of the view changes.
How can I get my view to exhibit the same behavior as UILabel?
Some things I have looked into, but have yet to have success with:
- Set the view's layer's needsDisplayOnBoundsChange to YES.
- Set the view's contentStretch to CGRectZero.
- Call setNeedsDisplay in my view's layoutSubviews.
Maybe I'm not doing one of these things right. Has anyone else run into this?
Update: As recommended in James Huddleston's answer, I set the contentMode
property of the view to UIViewContentModeRedraw
, which got me part of the way there. The text now appears correct at the conclusion of the animation. However, at the start of the animation the text gets squished/stretched to fit the end dimensions and then gets unsquished/unstretched over the course of the animation. This is not the case with UILabel.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试将视图的
contentMode
属性设置为UIViewContentModeRedraw
。Try setting the
contentMode
property of your view toUIViewContentModeRedraw
.这似乎工作正常:
然后确保右下角的像素是背景颜色。为此,我在视图内容周围放置了一个像素的填充。看来 UILabel 没有单像素边框限制,所以它必须做一些不同的事情。但据我所知,这具有相同的效果。
This seems to work OK:
And then ensure that the bottom-right pixel is the background color. To do that, I put one pixel of padding around the contents of the view. It seems like UILabel doesn't have the one pixel border restriction, so it must be doing something different. But as far as I can tell, this has the same effect.
使用 UIWebView
听起来有点大材小用,但它似乎是获取比 UILabel 可以处理的更复杂的格式化文本的推荐方法。
Use a UIWebView
Sounds a bit overkill but it seems the recommended way to get formatted text that's more complicated than a UILabel can cope with.
Kevin Ballard 创建了一些名为 FontLabel 的源代码。 (代码.google.com)
这样做的好处是,它继承了 UILabel 的子类,您可以使用自己的字体;)
//编辑:好的好的,正如所告知的,我将更新我的答案以推荐子类化 UILabel
“到获得所有 UILabel 的优点”
^^There is some source code created by Kevin Ballard called FontLabel. (code.google.com)
The good thing about this, it subclasses UILabel and you can use your own Font ;)
//EDIT: ok ok, as told I will update my answer to recommend subclassing UILabel
"to get all the UILabel goodness"
^^