将 UIImage 创建为带颜色的边框
create UIImage to be a border with color
我正在使用FSCalendar我想做的是改变显示事件的方式。我想在带有事件的单元格周围放置一个彩色矩形边框。我通过编辑单元格背景层来做到这一点并且工作正常,但现在我意识到将我的代码更新到最新版本的 FSCalendar 是错误的地方,这将覆盖我的更改。
日历代表可以访问的内容之一是将图像设置为单元格,因此我想将图像创建为带有事件颜色的矩形边框。
这是我想要的图像:
欢迎任何建议。
提前致谢。
您可以为 ImageView 设置边框,而不是将图像设置为矩形边框
cell.imageView.layer.borderColor = [[UIColor redColor] CGColor];
cell.imageView.layer.borderWidth = 1.0f;
此方法将绘制一个带边框的矩形。我会做成class方法,放到UIImage类里方便使用。
- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)imageSize andBorderWidth:(CGFloat)borderWidth fillWithColor:(BOOL)fillWithColor{
UIGraphicsBeginImageContext(imageSize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect rect = CGRectMake(0, 0, imageSize.width, imageSize.height);
if(fillWithColor) {
[color setFill];
CGContextFillRect(context, rect);
} else {
[color setStroke];
CGContextStrokeRectWithWidth(context, rect, borderWidth);
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
编辑:添加了fillWithColor
参数
我正在使用FSCalendar我想做的是改变显示事件的方式。我想在带有事件的单元格周围放置一个彩色矩形边框。我通过编辑单元格背景层来做到这一点并且工作正常,但现在我意识到将我的代码更新到最新版本的 FSCalendar 是错误的地方,这将覆盖我的更改。
日历代表可以访问的内容之一是将图像设置为单元格,因此我想将图像创建为带有事件颜色的矩形边框。
这是我想要的图像:
欢迎任何建议。 提前致谢。
您可以为 ImageView 设置边框,而不是将图像设置为矩形边框
cell.imageView.layer.borderColor = [[UIColor redColor] CGColor];
cell.imageView.layer.borderWidth = 1.0f;
此方法将绘制一个带边框的矩形。我会做成class方法,放到UIImage类里方便使用。
- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)imageSize andBorderWidth:(CGFloat)borderWidth fillWithColor:(BOOL)fillWithColor{
UIGraphicsBeginImageContext(imageSize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect rect = CGRectMake(0, 0, imageSize.width, imageSize.height);
if(fillWithColor) {
[color setFill];
CGContextFillRect(context, rect);
} else {
[color setStroke];
CGContextStrokeRectWithWidth(context, rect, borderWidth);
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
编辑:添加了fillWithColor
参数