是否有任何方法如何从某种颜色(比如它的[UIColor redColor])创建这种颜色的默认突出显示颜色(在这种情况下是某种深红色)。
在我的情况下,我有一个UIButton(在UIBarButton内部),我想为UIControlStateHighlighted状态设置titleColor,与UIImage的默认高亮颜色相同。请参阅以下代码:
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);
如果我没有为UIControlStateHighlighted状态设置图像/标题,则只有按下按钮时图像会突出显示:
答案 0 :(得分:1)
你需要将两种UIColor合并在一起, 首先,你可以使用tintColor:
UIColor *defualtTintColor = self.navigationController.navigationItem.backBarButtonItem.tintColor;
将redColor和defualtTintColor合并在一起。 这是我的方法,非常优雅,但它确实有效:
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 :(得分:0)
只需为按钮添加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
是重点