UILabel:调整边距以匹配 UITextView

发布于 2024-09-05 16:15:37 字数 142 浏览 4 评论 0原文

我有一个 UILabel,如果我调整文本的大小,我可以使它看起来像 UITextView,但是左边距不同,在 UIlabel 上,文本正好靠在 UITextView 有轻微边距的左边框上。我如何调整 UILabel 以便当这些控件放置在另一个控件之上时,它们看起来一致?

I have a UILabel and if i adjust the size of the text i can make it look loke a UITextView however the left margin is different, on the UIlabel the text is right up against the left border where the UITextView has a slight margin. How do i adjust the UILabel so that when these controls are placed above one another, they look consistent?

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

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

发布评论

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

评论(1

何以畏孤独 2024-09-12 16:15:37

只需更改标签的框架:

CGRect frame = label.frame;
CGRect newFrame = CGRectMake(frame.origin.x + MARGIN, frame.origin.y, frame.size.width - MARGIN, frame.size.height);
label.frame = newFrame;

当然,将 MARGIN 替换为您想要的边距。

或者您可以子类 UILabel 并重写 textRectForBounds:limitedToNumberOfLines: 如下所示:

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

希望这会有所帮助!

Simply change the label's frame:

CGRect frame = label.frame;
CGRect newFrame = CGRectMake(frame.origin.x + MARGIN, frame.origin.y, frame.size.width - MARGIN, frame.size.height);
label.frame = newFrame;

Of course replace MARGIN with whatever you want your margin to be.

Or you could subclass UILabel and override textRectForBounds:limitedToNumberOfLines: like so:

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

Hope this helps!

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