触摸时UIButton没有显示高亮显示

时间:2023-01-17 21:51:32

I am trying to highlight my UIButton with a custom UIColor light grey and make the text go white... however I am not sure how to do this... this is how far I have gotten with creating the UIButton any help would be appreciated.

我试图突出我的UIButton与自定义UIColor浅灰色,并使文本变白...但我不知道如何做到这...这是我创建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];

5 个解决方案

#1


17  

To get the white text on highlight just do:

要获得突出显示的白色文本,请执

[cancelButton setTitleColor:[UIColor whiteColor]
                       forState:UIControlStateHighlighted];

To get the background to change you need to either do setBackgroundImage:forState: and use a UIImage with the pattern color, or subclass UIButton and set the appropriate background color in the setHighlight: method.

要获得更改背景,您需要执行setBackgroundImage:forState:并使用带图案颜色的UIImage或子类UIButton,并在setHighlight:方法中设置适当的背景颜色。

EDIT: Swift 2.x version

编辑:Swift 2.x版本

cancelButton.setTitleColor(.whiteColor(), forState: Highlighted)

EDIT: Swift 3.0 version

编辑:Swift 3.0版本

cancelButton.setTitleColor(.white, for: .highlighted)

#2


5  

You can create an image to fill in the background with color.

您可以创建图像以使用颜色填充背景。

[cancelButton setBackgroundImage:[self setBackgroundImageByColor:[UIColor blueColor] withFrame:cancelButton.frame cornerRadius:0] forState:UIControlStateHighlighted];
[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];

This method builds an image the size of the button with a solid background of the color backgroundColor.

此方法使用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;
}

#3


1  

I know you wanted a white highlight color, but for anyone else that doesn't care specifically what color the highlight is, just switch the button type to System (in the Attribute Inspector). The nice thing is that you can still use custom fonts, colors, etc.

我知道你想要一个白色突出显示颜色,但对于其他任何不关心突出显示颜色的人来说,只需将按钮类型切换到系统(在属性检查器中)。好消息是你仍然可以使用自定义字体,颜色等。

#4


0  

Use titleColor:forState: to set the color of text on selection/highlight/disable for the respective states.

使用titleColor:forState:为各个状态设置选择/突出显示/禁用的文本颜色。

#5


0  

The answer by @nitin kachhadiya is very good, but I tweaked it, simplified it, and made it a category of UIButton. Add these two files to your project. Then to choose the color you want to set:

@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

#1


17  

To get the white text on highlight just do:

要获得突出显示的白色文本,请执

[cancelButton setTitleColor:[UIColor whiteColor]
                       forState:UIControlStateHighlighted];

To get the background to change you need to either do setBackgroundImage:forState: and use a UIImage with the pattern color, or subclass UIButton and set the appropriate background color in the setHighlight: method.

要获得更改背景,您需要执行setBackgroundImage:forState:并使用带图案颜色的UIImage或子类UIButton,并在setHighlight:方法中设置适当的背景颜色。

EDIT: Swift 2.x version

编辑:Swift 2.x版本

cancelButton.setTitleColor(.whiteColor(), forState: Highlighted)

EDIT: Swift 3.0 version

编辑:Swift 3.0版本

cancelButton.setTitleColor(.white, for: .highlighted)

#2


5  

You can create an image to fill in the background with color.

您可以创建图像以使用颜色填充背景。

[cancelButton setBackgroundImage:[self setBackgroundImageByColor:[UIColor blueColor] withFrame:cancelButton.frame cornerRadius:0] forState:UIControlStateHighlighted];
[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];

This method builds an image the size of the button with a solid background of the color backgroundColor.

此方法使用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;
}

#3


1  

I know you wanted a white highlight color, but for anyone else that doesn't care specifically what color the highlight is, just switch the button type to System (in the Attribute Inspector). The nice thing is that you can still use custom fonts, colors, etc.

我知道你想要一个白色突出显示颜色,但对于其他任何不关心突出显示颜色的人来说,只需将按钮类型切换到系统(在属性检查器中)。好消息是你仍然可以使用自定义字体,颜色等。

#4


0  

Use titleColor:forState: to set the color of text on selection/highlight/disable for the respective states.

使用titleColor:forState:为各个状态设置选择/突出显示/禁用的文本颜色。

#5


0  

The answer by @nitin kachhadiya is very good, but I tweaked it, simplified it, and made it a category of UIButton. Add these two files to your project. Then to choose the color you want to set:

@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