UIButton – what is the default image highlight color

Is there any approach how to create from some color (let's say it's [UIColor redColor]) the default highlighted shade of this color (some kind of dark red color in this case).

In my case, I have a UIButton (inside of UIBarButton) and I would like to setup the titleColor for UIControlStateHighlighted state just the same as the default hightlighted color of the UIImage. See the code below:

UIColor *color = [UIColor redColor];
UIColor *highlightedColor = ???;

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

[button setImage:[[UIImage imageNamed:@"back-arrow"] imageWithOverlayColor:color] forState:UIControlStateNormal];
[button setImage:[[UIImage imageNamed:@"back-arrow"] imageWithOverlayColor:highlightedColor] forState:UIControlStateHighlighted];

[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:color forState:UIControlStateNormal];
[button setTitleColor:highlightedColor forState:UIControlStateHighlighted];

button.imageEdgeInsets = UIEdgeInsetsMake(0.f, -5.f, 0.f, 0.f);

If I don't setup the image/title for the UIControlStateHighlighted state, only the image is highlighted when button is pressed:

在这里输入图像描述


You need to merge two kinds of UIColor together, First,you can get the tintColor use :

UIColor *defualtTintColor = self.navigationController.navigationItem.backBarButtonItem.tintColor;

merge redColor and the defualtTintColor together. That is my method,Inelegant, but it works:

UIColor* blend( UIColor* c1, UIColor* c2, float alpha )
{
    alpha = MIN( 1.f, MAX( 0.f, alpha ) );
    float beta = 1.f - alpha;
    CGFloat r1, g1, b1, a1, r2, g2, b2, a2;
    [c1 getRed:&r1 green:&g1 blue:&b1 alpha:&a1];
    [c2 getRed:&r2 green:&g2 blue:&b2 alpha:&a2];
    CGFloat r = r1 * beta + r2 * alpha;
    CGFloat g = g1 * beta + g2 * alpha;
    CGFloat b = b1 * beta + b2 * alpha;
    return [UIColor colorWithRed:r green:g blue:b alpha:1.f];
}

More elegant:

UIColor+Extensions.h:

@interface UIColor (Extensions)

- (UIColor*)blendWithColor:(UIColor*)color2 alpha:(CGFloat)alpha2;

@end

UIColor+Extensions.m:

@implementation UIColor (Extensions)

- (UIColor*)blendWithColor:(UIColor*)color2 alpha:(CGFloat)alpha2
{
    alpha2 = MIN( 1.0, MAX( 0.0, alpha2 ) );
    CGFloat beta = 1.0 - alpha2;
    CGFloat r1, g1, b1, a1, r2, g2, b2, a2;
    [self getRed:&r1 green:&g1 blue:&b1 alpha:&a1];
    [color2 getRed:&r2 green:&g2 blue:&b2 alpha:&a2];
    CGFloat red     = r1 * beta + r2 * alpha2;
    CGFloat green   = g1 * beta + g2 * alpha2;
    CGFloat blue    = b1 * beta + b2 * alpha2;
    CGFloat alpha   = a1 * beta + a2 * alpha2;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

@end

then you can get highlightedColor:

UIColor *color = [UIColor redColor];
UIColor *defualtTintColor = self.navigationController.navigationItem.backBarButtonItem.tintColor;
UIColor *highlightedColor = [defualtTintColor blendWithColor:color alpha:0.75];

simply add a highlightedImage for button

@implementation UIButton (YASAddition)

- (void)awakeFromNib {
    [super awakeFromNib];

    UIImage *highlightedImage = [UIImage imageWithColor:[[UIColor colorWithHex:0X1E1E1E] colorWithAlphaComponent:0.2f]];
    [self setBackgroundImage:highlightedImage forState:UIControlStateHighlighted];
}

@end

the same for UIBarButtonItem

alpha is the point

链接地址: http://www.djcxy.com/p/96254.html

上一篇: 按下其他按钮后不能更改uibutton的图像

下一篇: UIButton - 什么是默认图像高亮颜色