具有多个标签的 iOS UIButton
我有一个 UI 按钮,我想在其上放置两个标签,类似于单元格具有标题文本和详细信息文本的方式。
我希望按钮的主要文本字体较大,而其下方的详细文本较小。
这可能吗?我尝试在按钮上放置多行,但每行需要不同的文本大小,因此设置 titleLabel 的 lineBreakMode 和 numberOfLines 并不真正有效。
I have a UI button that I'd like to put two labels on it, similar to how a cell has a title text and detail text.
I'd like the button to have a larger font for the main text, and have smaller detail text under that.
Is this possible? I've tried to put multiple lines on a button, but I need to have different text sizes for each line, so setting the lineBreakMode and numberOfLines of the titleLabel doesn't really quite work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我们最终使用的代码。王约翰的协助。
感谢大家的建议!
Here's the code we finally used. Assistance from John Wang.
Thanks to everyone for the suggestions!
我推荐的一个技巧是将具有透明内部的 UIButton 放在 UILabels 之上。我以前用过这个技巧,虽然它可能会在维护和国际化方面带来一些问题,但它的作用就像一个魅力。
这是使用上述建议的 5 分钟示例。

如果有更多时间,您可以制作更好的圆角标签。
A trick I would recommend is putting a UIButton with a transparent interior on top of UILabels. I've used this trick before and, although it may present some problems in terms of maintenance and i18n, it works like a charm.
Here is a 5 minutes sample using the suggestion above.

Given more time, you can make a better label with round corners.
您应该能够向其添加子视图。由于一切都是视图,因此一切都可能有子视图。
我将对其进行子类化并将标签放在子类中,然后您可以扩展文本和子文本的属性以更改它们的值。
并不是说它可以100%工作。但我的头顶上却没有。 UIView 可以有子视图
you should be able to add subviews to it. Since everything is a view, everything can potentially have subviews.
I would subclass it and put the labels on it within the subclass, Then you can extend properties for text and subtext to change their values.
Not saying it can 100% work. But off the top of my head. UIView can have SubViews