繁体   English   中英

在iOS中为按钮文字设置颜色

[英]setting colour for button text in iOS

UIButton *ConnexionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[ConnexionButton setTitle:@"" forState:(UIControlState)UIControlStateNormal];        
[ConnexionButton addTarget:self action:@selector(ConnexionAction) forControlEvents:(UIControlEvents)UIControlEventTouchDown];

UIImage *NewImage = [UIImage imageNamed:@"connectionbtnIcon"];
[ConnexionButton setBackgroundImage:NewImage forState:UIControlStateNormal];
[ConnexionButton setTranslatesAutoresizingMaskIntoConstraints:NO];
[TopView addSubview:ConnexionButton];

[ConnexionButton setTitle:NSLocalizedString(@"Connexion", @"") forState:UIControlStateNormal];
[ConnexionButton.titleLabel setFont:PANTON_SEMIBOLD(15)];
[ConnexionButton.titleLabel setTextColor:[UIColor whiteColor]];
[TopView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[ConnexionButton]-20-|" options:0 metrics:nil views:views]];
[TopView addConstraint:[NSLayoutConstraint constraintWithItem:ConnexionButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:TopView attribute:NSLayoutAttributeTop multiplier:1.0 constant:90]];

NSLayoutConstraint *ConnexionButtonHeightConstraint = [NSLayoutConstraint constraintWithItem:ConnexionButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:35.0];
[TopView addConstraint:ConnexionButtonHeightConstraint]
UIButton *ConnexionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

替换此行。

UIButton *ConnexionButton = [UIButton buttonWithType:UIButtonTypeCustom];
ConnexionButton.frame = CGRectMake(x, y, w, h);


[ConnexionButton setTitle:@""
                 forState:(UIControlState)UIControlStateNormal];
[ConnexionButton addTarget:self
                    action:@selector(ConnexionAction)
          forControlEvents:(UIControlEvents)UIControlEventTouchDown];
UIImage *NewImage = [UIImage imageNamed:@"connectionbtnIcon"];
[ConnexionButton setBackgroundImage:NewImage forState:UIControlStateNormal];
[ConnexionButton setTranslatesAutoresizingMaskIntoConstraints:NO];
[TopView addSubview:ConnexionButton];
[ConnexionButton setTitle:NSLocalizedString(@"Connexion", @"") forState:UIControlStateNormal];
[ConnexionButton.titleLabel setFont:PANTON_SEMIBOLD(15)];
    [ConnexionButton.titleLabel setTextColor:[UIColor whiteColor]];
[TopView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[ConnexionButton]-20-|" options:0 metrics:nil views:views]];
[TopView addConstraint:[NSLayoutConstraint constraintWithItem:ConnexionButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:TopView attribute:NSLayoutAttributeTop multiplier:1.0 constant:90]]; NSLayoutConstraint *ConnexionButtonHeightConstraint = [NSLayoutConstraint constraintWithItem:ConnexionButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:35.0];
[TopView addConstraint:ConnexionButtonHeightConstraint];

设置颜色...您可以使用

-[UIButton setTitleColor:forState:]

去做这个。

[ConnexionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

请删除

UIButton *ConnexionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

UIButton *ConnexionButton = [UIButton buttonWithType:UIButtonTypeCustom];
[ConnexionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[ConnexionButton setTitleColor:[UIColor colorWithRed:150.0/256.0 green:150.0/256.0 blue:150.0/256.0 alpha:1.0] forState:UIControlStateHighlighted];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM