【发布时间】:2015-05-31 09:59:02
【问题描述】:
我需要设置一个圆角矩形作为 UIButton 的背景,我需要以编程方式生成它,与颜色相关
+ (UIImage *)buttonBackgroundWithColor:(UIColor *)color {
UIGraphicsBeginImageContextWithOptions(CGSizeMake(30.f, 30.f), NO, 1.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[color setStroke];
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(2.f, 2.f, 26.f, 26.f)
cornerRadius:7.f];
CGContextSetStrokeColorWithColor(context, color.CGColor);
bezierPath.lineWidth = 1.f;
[bezierPath stroke];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [[image copy] resizableImageWithCapInsets:UIEdgeInsetsMake(10.f, 10.f, 10.f, 10.f)];
}
我已经尝试过这里描述的 https://stackoverflow.com/a/19142851/1090590
但是,我的按钮背景是“模糊的”
我做错了什么?我怎样才能使它清晰易读?
【问题讨论】:
-
使用 button.border 和 button.layer.cornerRadius = 5;
-
我知道那个解决方案,但它对我的 UI 组件的 API 来说不是很好。
-
我怀疑问题出在
UIGraphicsBeginImageContextWithOptions的scale参数上。将其设置为0.0以使其使用设备屏幕的比例因子。 -
@bobnoble 你是对的,谢谢。如果您将其移到答案部分,我会接受您的答案。
标签: ios uibutton uiimage uikit uibezierpath