使用UIImage或Code绘制高亮蓝色

时间:2012-07-09 08:11:25

标签: iphone ios xcode uiimage uibutton

来自http://replyz.com/c/1244746-does-anyone-know-the-exact-cgcolorref-used-to-create-the-selection-highlight-when-using-uitableviewcellselectionstyleblue#

我看到有人用Code做过,他说突出显示的颜色来自UIImage。

我在哪里可以找到图像?

(我想用UITableViewCellSelectionStyleBlue颜色突出显示UIButton)

2 个答案:

答案 0 :(得分:0)

我找到了链接http://replyz.com/c/1244746-does-anyone-know-the-exact-cgcolorref-used-to-create-the-selection-highlight-when-using-uitableviewcellselectionstyleblue#,其中包含一些可用于创建突出显示的代码。我用代码创建了一个新的UIImage。

以下是代码:

+ (UIImage *) imageForSelectedBlue {
    CGRect rect = CGRectMake(0, 0, 10, 10);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    size_t num_locations = 2;
    CGFloat locations[2] = { 0.0, 1.0 };
    CGFloat components[8] = { 0.021, 0.548, 0.962, 1.000, 0.008, 0.364, 0.900, 1.000 };

    CGColorSpaceRef rgbColorspace = CGColorSpaceCreateDeviceRGB();
    CGGradientRef selectionGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);

    CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
    CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));

    CGContextDrawLinearGradient(context, selectionGradient, startPoint, endPoint, 0);

    CGGradientRelease(selectionGradient);
    CGColorSpaceRelease(rgbColorspace);

    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

答案 1 :(得分:0)

实际上,您可以设置按钮的背景图像。效率更高。

[button setBackgroundImage:buttonBackgroundPressed forState:UIControlStateHighlighted];