【问题标题】:Draw the highlight blue color using UIImage or Code使用 UIImage 或 Code 绘制高亮蓝色
【发布时间】:2012-07-09 08:11:25
【问题描述】:

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

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

在哪里可以找到图片?

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

【问题讨论】:

标签: iphone ios xcode uiimage uibutton


【解决方案1】:

我找到了链接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;
}

【讨论】:

    【解决方案2】:

    其实可以设置按钮的背景图片。效率更高。

    [button setBackgroundImage:buttonBackgroundPressed forState:UIControlStateHighlighted];
    

    【讨论】:

      猜你喜欢
      • 2013-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-18
      • 2019-01-07
      相关资源
      最近更新 更多