如何为状态UIControlStateHighlighted设置按钮标签文本颜色 [英] How to set the buttons label text color for state UIControlStateHighlighted
本文介绍了如何为状态UIControlStateHighlighted设置按钮标签文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个iPhone应用程序,其中我有一个自定义按钮。我通过创建标签并将其添加为子视图来设置按钮标题。现在当按钮突出显示时,我想更改标签文本颜色。
I am creating an iPhone application in which i have a custom button. i have set the buttons title by creating a Label and adding it as subview. now when the button is highlighted i want to change the labels text color.
这是我的代码,
UIButton *button1= [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(68,162, 635, 101)];
[button1 setImage:[UIImage imageNamed:@"startwithouttext.png"] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:@"startactivewithouttext.png"] forState:UIControlStateHighlighted];
UILabel *buttonLabel = [[UILabel alloc] initWithFrame:CGRectMake(button1.bounds.origin.x+50, button1.bounds.origin.y+20, button1.bounds.size.width-100, button1.bounds.size.height-40)];
[buttonLabel setFont:[UIFont fontWithName:@"Helvetica" size:28]];
buttonLabel.backgroundColor=[UIColor clearColor];
buttonLabel.textColor=[UIColor colorWithRed:83.0/255.0 green:83.0/255.0 blue:83.0/255.0 alpha:1.0];
buttonLabel.highlightedTextColor=[UIColor whiteColor];
buttonLabel.text = @"Long text string";
[button1 addSubview:buttonLabel];
[button1 bringSubviewToFront:buttonLabel];
[button1 setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[button1 setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[button1 addTarget:self action:@selector(button1clicked:) forControlEvents:
[mainView button1];
当按钮突出显示时,任何正文都可以帮我改变文字颜色吗?
can any body help me to change the text color when the button is highlighted?
推荐答案
在StackOverflow上的另一个问题中找到答案:
UIButton颜色问题
Found the answer in a different question on StackOverflow: UIButton color issues
[button1 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
如果您可以在不创建Label的情况下工作并将其添加为上面提到的子视图。
This is if you can work without creating a Label and adding it as subview as you mention above.
这篇关于如何为状态UIControlStateHighlighted设置按钮标签文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文