UIButton - what is the default highlight color for an image

Is there any approach on how to create from a certain color (say, [UIColor redColor]) the highlighted shade of this color by default (in this case it’s some kind of dark red color).

In my case, I have a UIButton (inside a UIBarButton) and I would like to set the titleColor property for the UIControlStateHighlighted state in the same way as the default color UIImage. See 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 do not configure the image / title for the UIControlStateHighlighted state, only the image is highlighted when the button is pressed:

enter image description here

+4
source share
2 answers

You need to combine the two types of UIColor together. First, you can use tintColor:

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

redColor defualtTintColor . , Inelegant, :

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

:

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

highlightColor:

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

highlightImage

@implementation UIButton (YASAddition)

- (void)awakeFromNib {
    [super awakeFromNib];

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

@end

UIBarButtonItem

alpha -

0

Source: https://habr.com/ru/post/1588981/


All Articles