我发现了一种在UIButton上添加两行文字的方法, 但我想要的是这些文本行 中的每一行都有不同的字体(例如一个是粗体,其他不)。 怎么可能做到这一点?UIButton上每行不同字体的两行文字
谢谢。
我发现了一种在UIButton上添加两行文字的方法, 但我想要的是这些文本行 中的每一行都有不同的字体(例如一个是粗体,其他不)。 怎么可能做到这一点?UIButton上每行不同字体的两行文字
谢谢。
你应该将2个UILabel作为子视图添加到UIButton中。
你可以不喜欢它:
UIButton *testButton = [UIButton buttonWithType:UIButtonTypeCustom];
testButton.frame = CGRectMake(0, 0, 200, 40);
[self.view addSubview:testButton];
UILabel *firstLineTestButton = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
firstLineTestButton.text = @"First line";
firstLineTestButton.font = [UIFont systemFontOfSize:12];
[testButton addSubview:firstLineTestButton];
UILabel *secondLineTestButton = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 200, 20)];
secondLineTestButton.text = @"Second line";
secondLineTestButton.font = [UIFont boldSystemFontOfSize:12];
[testButton addSubview:secondLineTestButton];
也作了对UILabels突出可能的,你需要做的按钮自定义的突出显示。
因此,将操作添加到按钮,然后检查标签的按钮子视图并更改其颜色。
[testButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[testButton addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchDown];
[testButton addTarget:self action:@selector(touchCancel:) forControlEvents:UIControlEventTouchDragExit];
-(void)buttonAction:(UIButton*)sender
{
[self touchCancel:sender];
/* DO SOME MORE ACTIONS */
}
-(void)changeColor:(UIButton*)sender
{
sender.backgroundColor = [UIColor grayColor];
for(UIView *subview in sender.subviews){
if([subview isKindOfClass:[UILabel class]]){
UILabel *subViewLabel = (UILabel*)subview;
subViewLabel.textColor = [UIColor blueColor];
}
}
}
-(void)touchCancel:(UIButton*)sender
{
sender.backgroundColor = [UIColor clearColor];
for(UIView *subview in sender.subviews){
if([subview isKindOfClass:[UILabel class]]){
UILabel *subViewLabel = (UILabel*)subview;
subViewLabel.textColor = [UIColor blackColor];
}
}
}
您可以添加两个UILabels作为按钮的子视图,然后可以设置标签的文本
[lbl1 setFont:[UIFont fontWithName:@"Arial-Bold" size:18]];
[lbl2 setFont:[UIFont fontWithName:@"Arial" size:16]];
罗兰的解决方案是好的,另一种方式来做到这一点是使用一个NSAttributedString
。缺点是,它只适用于iOS 6及以上版本。
如果这是没有问题的,这里是代码
- (void)viewDidLoad
{
[super viewDidLoad];
// We want 2 lines for our buttons' title label
[[self.button titleLabel] setNumberOfLines:2];
// Setup the string
NSMutableAttributedString *titleText = [[NSMutableAttributedString alloc] initWithString:@"This should be bold,\n and this should not."];
// Set the font to bold from the beginning of the string to the ","
[titleText addAttributes:[NSDictionary dictionaryWithObject:[UIFont boldSystemFontOfSize:14] forKey:NSFontAttributeName] range:NSMakeRange(0, 20)];
// Normal font for the rest of the text
[titleText addAttributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] range:NSMakeRange(20, 22)];
// Set the attributed string as the buttons' title text
[self.button setAttributedTitle:titleText forState:UIControlStateNormal];
}
嗨,谢谢。这是一个问题,因为我也需要支持iOS 4.3。但是,正如我上面评论的那样,我遇到了罗兰德方法的一个问题。也许你知道一个解决方法?谢谢。 – user1903992
对于正确的动画,你应该继承的UIButton和其状态改变时
- (void)drawRect:(CGRect)rect
{
if (!self.firstLineTestButton) {
self.firstLineTestButton = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
self.firstLineTestButton.text = @"First line";
self.firstLineTestButton.font = [UIFont systemFontOfSize:12];
[self addSubview:self.firstLineTestButton];
}
if (!self.secondLineTestButton) {
self.secondLineTestButton = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 200, 20)];
self.secondLineTestButton.text = @"Second line";
self.secondLineTestButton.font = [UIFont boldSystemFontOfSize:12];
[self addSubview:self.secondLineTestButton];
}
if (!self.highlighted) {
// Do custom drawing such as changing text color
} else {
// Do custom drawing such as changing text color
}
}
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
// Force redraw of button
[self setNeedsDisplay];
}
有点问题的话重绘。现在,当用户选择按钮时,标签保持不变,即没有突出显示等。在这种情况下做什么?你能帮忙吗? – user1903992
只需将标签的'backgroundColor'属性设置为'[UIColor clearColor]',你应该没问题 –
我已经更新了我的答案并突出显示了按钮 –