iphone - 如何为状态 UIControlStateHighlighted 设置按钮标签文本颜色

标签 iphone objective-c ipad

我正在创建一个 iPhone 应用程序,其中有一个自定义按钮。我通过创建标签并将其添加为 subview 来设置按钮标题。现在,当按钮突出显示时,我想更改标签文本颜色。

这是我的代码,

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];   

任何人都可以帮我在突出显示按钮时更改文本颜色吗?

最佳答案

在 StackOverflow 上的另一个问题中找到了答案: UIButton color issues

[button1 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];

如果您可以在不创建标签并将其添加为上面提到的 subview 的情况下工作。

关于iphone - 如何为状态 UIControlStateHighlighted 设置按钮标签文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7806840/

相关文章:

javascript - 在viewDidLoad中调用UIWebView的stringByEvaluatingJavaScriptFromString是否正确?

iPhone 推送通知 - 如何使用默认声音

iphone - 如何从推送 segue 中解雇带有 Storyboard的 viewController?

ios - 如何查找特定字符在 NSString 中以大写形式出现的次数?

objective-c - 更改 Popover 内的 View 外观?

ios - iTunes 风格模态视图 Controller 链(表单)

ios - 无法在 Testflight 上更新配置文件

ios - 如何通过颜色选择器方法在按钮上设置颜色?

iphone - 我可以在多少台设备上测试 iPhone 应用程序

iphone - 如何在 iOS 中动态声明的自定义 View 中添加 UITableView?