UILabel textRectForBounds 在尝试创建边距时没有效果

发布于 2024-10-16 03:59:21 字数 1097 浏览 6 评论 0原文

我试图缩进 UILabel 中的文本,以便在显示背景颜色的文本周围留出一些边距。按照此处的建议,我已经重写了textRectForBounds:limitedToNumberOfLines:,如下所示:

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
    CGRect intermediate = CGRectMake(bounds.origin.x+MARGIN,bounds.origin.y+MARGIN,bounds.size.width-2*MARGIN,bounds.size.height-2*MARGIN);
    return [super textRectForBounds:intermediate limitedToNumberOfLines:numberOfLines];
}

但无论我做什么,文本最终都会紧贴矩形的左边框。似乎绘图忽略了返回的 CGRect 的原始部分(尽管它似乎尊重宽度部分,就好像我将中间的宽度减小到例如 bounds.size.width-200 textRectForBounds 返回的矩形适当狭窄,并且文本绘制在一个细长的列中)。

那么:我还需要对 UILabel 做什么,以使绘图尊重 textForRectBounds-returned-rect 的 origin.x 和 origin.y?如果可以的话,我宁愿不覆盖 UILabeldrawTextInRect

更新:这是很久以前的事了,我不记得为什么另一个问题对我不起作用。我相信这是因为我试图拥有一个包含多行的 UILabel ,并且解决方案 在这种情况下,here 不起作用。

I'm trying to indent the text in a UILabel to leave some margin around the text showing the background colour. Following the suggestion here I've overriden textRectForBounds:limitedToNumberOfLines: like so:

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
    CGRect intermediate = CGRectMake(bounds.origin.x+MARGIN,bounds.origin.y+MARGIN,bounds.size.width-2*MARGIN,bounds.size.height-2*MARGIN);
    return [super textRectForBounds:intermediate limitedToNumberOfLines:numberOfLines];
}

But no matter what I do, the text ends up tight against the left border of the rectangle. It seems as though the drawing is ignoring the origin part of the returned CGRect (although it seems to be respecting the width part, as if I reduce to width of intermediate to eg bounds.size.width-200 the rect that textRectForBounds returns is suitably narrow and the text is drawn in a long skinny column).

So: what else I need to do to the UILabel to make the drawing respect the textForRectBounds-returned-rect's origin.x and origin.y? I'd rather not override UILabel's drawTextInRect if I can help it.

Update: This was a long time ago and I can't remember exactly why the other question didn't work for me. I believe it was because I was trying to have a UILabel with multiple lines, and the solution here didn't work in that case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情独悲 2024-10-23 03:59:21

我认为你应该像这样重写 textRectForBounds:limitedToNumberOfLines:drawTextInRect:

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
    return CGRectInset(bounds, MARGIN, MARGIN);
}

- (void)drawTextInRect:(CGRect)rect
{
    [super drawTextInRect: CGRectInset(self.bounds, MARGIN, MARGIN)];
}

I think you should override both textRectForBounds:limitedToNumberOfLines: and drawTextInRect: like this:

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
    return CGRectInset(bounds, MARGIN, MARGIN);
}

- (void)drawTextInRect:(CGRect)rect
{
    [super drawTextInRect: CGRectInset(self.bounds, MARGIN, MARGIN)];
}
翻了热茶 2024-10-23 03:59:21

查看文档,可能会有所帮助。对 super 的调用可能不会返回您假设的值。

您不应该调用此方法
直接地。这个方法应该只
被想要的子类覆盖
改变接收者的边界
在执行任何其他操作之前先绘制矩形
计算。使用中的值
numberOfLines 参数来限制
返回矩形的高度为
指定的文本行数。
要调用此方法,有
必须事先调用 sizeToFit
或 sizeThatFits: 方法。注意
UITableViewCell 对象中的标签是
根据细胞尺寸确定尺寸,
并且不是所要求的尺寸
这个的默认实现
方法返回原始边界
矩形。

祝你好运!

Check the documentation, it might be of some help. calls to super might not be returning the values you assume.

You should not call this method
directly. This method should only be
overridden by subclasses that want to
change the receiver’s bounding
rectangle before performing any other
computations. Use the value in the
numberOfLines parameter to limit the
height of the returned rectangle to
the specified number of lines of text.
For this method to be called, there
must be a prior call to the sizeToFit
or sizeThatFits: method. Note that
labels in UITableViewCell objects are
sized based on the cell dimensions,
and not a requested size
The default implementation of this
method returns the original bounds
rectangle.

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文