gpt4 book ai didi

具有多个标签的 iOS UIButton

转载 作者:可可西里 更新时间:2023-11-01 03:40:27 25 4
gpt4 key购买 nike

我有一个 UI 按钮,我想在其上放置两个标签,类似于单元格具有标题文本和详细信息文本的方式。

我希望按钮的主要文本具有较大的字体,并且在其下方具有较小的详细文本。

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

最佳答案

这是我们最终使用的代码。 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

关于具有多个标签的 iOS UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5864383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com