我正在尝试使用自定义UIColor浅灰色突出显示我的UIButton并使文本变为白色...但我不知道如何做到这一点...这是我创建UIButton的任何帮助将不胜感激。
cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
cancelButton.frame = CGRectMake(10.0, calculatorView.frame.size.height-55, 100.0, 45.0);
[cancelButton addTarget:self action:@selector(calculateMethod:)forControlEvents:UIControlEventTouchDown];
[[cancelButton titleLabel] setFont:[UIFont fontWithName: @"Super-text" size:20.0]];
[cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
答案 0 :(得分:18)
要突出显示白色文字,请执行以下操作:
[cancelButton setTitleColor:[UIColor whiteColor]
forState:UIControlStateHighlighted];
要获得更改背景,您需要执行setBackgroundImage:forState:并使用带图案颜色的UIImage或子类UIButton,并在setHighlight:方法中设置适当的背景颜色。
编辑:Swift 2.x版
cancelButton.setTitleColor(.whiteColor(), forState: Highlighted)
编辑:Swift 3.0版
cancelButton.setTitleColor(.white, for: .highlighted)
答案 1 :(得分:6)
您可以创建图像以用颜色填充背景。
[cancelButton setBackgroundImage:[self setBackgroundImageByColor:[UIColor blueColor] withFrame:cancelButton.frame cornerRadius:0] forState:UIControlStateHighlighted];
[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
此方法使用颜色 backgroundColor 的纯色背景构建按钮大小的图像。
-(UIImage *)setBackgroundImageByColor:(UIColor *)backgroundColor withFrame:(CGRect )rect cornerRadius:(float)radius{
UIView *tcv = [[UIView alloc] initWithFrame:rect];
[tcv setBackgroundColor:backgroundColor];
CGSize gcSize = tcv.frame.size;
UIGraphicsBeginImageContext(gcSize);
[tcv.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
const CGRect RECT = CGRectMake(0, 0, image.size.width, image.size.height);;
[[UIBezierPath bezierPathWithRoundedRect:RECT cornerRadius:radius] addClip];
[image drawInRect:RECT];
UIImage* imageNew = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageNew;
}
答案 2 :(得分:2)
我知道你想要一个白色突出显示颜色,但是对于其他任何不关心突出显示颜色的人来说,只需将按钮类型切换到系统(在属性检查器中)。好消息是你仍然可以使用自定义字体,颜色等。
答案 3 :(得分:0)
使用titleColor:forState:为相应states的选择/加亮/禁用设置文字颜色。
答案 4 :(得分:0)
@nitin kachhadiya的答案非常好,但我调整了它,简化了它,并使它成为UIButton的一个类别。将这两个文件添加到项目中。然后选择要设置的颜色:
[button setHighlightedColor:[UIColor blueColor]];
<强>的UIButton + FJHighlighted.h 强>
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface UIButton (FJHighlighted)
- (void)setHighlightedColor:(UIColor *)highlightColor;
@end
<强>的UIButton + FJHighlighted.m 强>
#import "UIButton+FJHighlighted.h"
@implementation UIButton (FJHighlighted)
- (void)setHighlightedColor:(UIColor *)highlightColor {
[self setBackgroundImage:[self setBackgroundImageByColor:highlightColor cornerRadius:0]
forState:UIControlStateHighlighted];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
}
- (UIImage *)setBackgroundImageByColor:(UIColor *)backgroundColor cornerRadius:(float)radius {
UIView *tcv = [[UIView alloc] initWithFrame:self.frame];
[tcv setBackgroundColor:backgroundColor];
CGSize gcSize = tcv.frame.size;
UIGraphicsBeginImageContext(gcSize);
[tcv.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
const CGRect RECT = CGRectMake(0, 0, image.size.width, image.size.height);;
[[UIBezierPath bezierPathWithRoundedRect:RECT cornerRadius:radius] addClip];
[image drawInRect:RECT];
UIImage *imageNew = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageNew;
}
@end