带有多个标签的iOS UIButton

ora*_*ako 6 uibutton uilabel ios

我有一个UI按钮,我想在其上放置两个标签,类似于单元格如何具有标题文本和详细文本.

我希望按钮为主文本设置更大的字体,并在其下有更小的细节文本.

这可能吗?我试图在一个按钮上放置多行,但我需要为每一行设置不同的文本大小,因此设置titleLabel的lineBreakMode和numberOfLines并不是很有效.

ora*_*ako 6

这是我们最终使用的代码.John Wang的帮助.

感谢大家的建议!

// 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
Run Code Online (Sandbox Code Playgroud)