具有多个标签的 iOS UIButton

发布于 2024-11-04 20:21:05 字数 183 浏览 8 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(3

泪冰清 2024-11-11 20:21:05

这是我们最终使用的代码。王约翰的协助。

感谢大家的建议!

// Formats a label to add to a button.  Supports multiline buttons
// Parameters:
// button - the button to add the label to
// height - height of the label.  usual value is 44
// offset - the offset from the top of the button
// labelText - the text for the label
// color - color of the text
// formatAsBold - YES = bold NO = normal weight
// tagNumber - tag for the label

- (void) formatLabelForButton: (UIButton *) button withHeight: (double) height andVerticalOffset: (double) offset andText: (NSString *) labelText withFontSize: (double) fontSize withFontColor: (UIColor *) color andBoldFont:(BOOL) formatAsBold withTag: (NSInteger) tagNumber {

    // Get width of button
    double buttonWidth= button.frame.size.width;

    // Initialize buttonLabel
    UILabel *buttonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, offset, buttonWidth, height)];

    // Set font size and weight of label
    if (formatAsBold) {
        buttonLabel.font = [UIFont boldSystemFontOfSize:fontSize];
    }
    else {
        buttonLabel.font = [UIFont systemFontOfSize:fontSize];
    }

    // set font color of label
    buttonLabel.textColor = color;

    // Set background color, text, tag, and font
    buttonLabel.backgroundColor = [UIColor clearColor];
    buttonLabel.text = labelText;
    buttonLabel.tag = tagNumber;

    // Center label
    buttonLabel.textAlignment = UITextAlignmentCenter;

    // Add label to button
    [button addSubview:buttonLabel];

    [buttonLabel autorelease];
} // End formatLabelForButton

Here's the code we finally used. Assistance from John Wang.

Thanks to everyone for the suggestions!

// Formats a label to add to a button.  Supports multiline buttons
// Parameters:
// button - the button to add the label to
// height - height of the label.  usual value is 44
// offset - the offset from the top of the button
// labelText - the text for the label
// color - color of the text
// formatAsBold - YES = bold NO = normal weight
// tagNumber - tag for the label

- (void) formatLabelForButton: (UIButton *) button withHeight: (double) height andVerticalOffset: (double) offset andText: (NSString *) labelText withFontSize: (double) fontSize withFontColor: (UIColor *) color andBoldFont:(BOOL) formatAsBold withTag: (NSInteger) tagNumber {

    // Get width of button
    double buttonWidth= button.frame.size.width;

    // Initialize buttonLabel
    UILabel *buttonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, offset, buttonWidth, height)];

    // Set font size and weight of label
    if (formatAsBold) {
        buttonLabel.font = [UIFont boldSystemFontOfSize:fontSize];
    }
    else {
        buttonLabel.font = [UIFont systemFontOfSize:fontSize];
    }

    // set font color of label
    buttonLabel.textColor = color;

    // Set background color, text, tag, and font
    buttonLabel.backgroundColor = [UIColor clearColor];
    buttonLabel.text = labelText;
    buttonLabel.tag = tagNumber;

    // Center label
    buttonLabel.textAlignment = UITextAlignmentCenter;

    // Add label to button
    [button addSubview:buttonLabel];

    [buttonLabel autorelease];
} // End formatLabelForButton
愛放△進行李 2024-11-11 20:21:05

我推荐的一个技巧是将具有透明内部的 UIButton 放在 UILabels 之上。我以前用过这个技巧,虽然它可能会在维护和国际化方面带来一些问题,但它的作用就像一个魅力。

这是使用上述建议的 5 分钟示例。
Label Behind Button

如果有更多时间,您可以制作更好的圆角标签。

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.
Label behind button

Given more time, you can make a better label with round corners.

热情消退 2024-11-11 20:21:05

您应该能够向其添加子视图。由于一切都是视图,因此一切都可能有子视图。

我将对其进行子类化并将标签放在子类中,然后您可以扩展文本和子文本的属性以更改它们的值。

并不是说它可以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

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