我在检查工具中看到可以更改背景颜色,但我还想更改边框颜色和宽度,这是否可行?
我在检查工具中看到可以更改背景颜色,但我还想更改边框颜色和宽度,这是否可行?
您需要使用视图的层(layer)来设置边框属性。例如:
#import <QuartzCore/QuartzCore.h>
...
view.layer.borderColor = [UIColor redColor].CGColor;
view.layer.borderWidth = 3.0f;
您还需要链接QuartzCore.framework才能访问此功能。
__bridge CGColorRef
,但这种方法不起作用。正确的写法是使用[UIColor blackColor].CGColor
,就像回答中提到的那样。 - GoodSp33d#import <QuartzCore/QuartzCore.h>
。 - meaning-matters自从 Xcode 的最新版本,这个问题有了更好的解决方案:
使用 @IBInspectable
,你可以直接在 Attributes Inspector
中设置属性。
这会为您设置 User Defined Runtime Attributes
:
有两种方法可以设置:
选项 1(在故事板中实时更新)
MyCustomView
。UIView
。@IBDesignable
(这使得视图实时更新)。*@IBInspectable
设置运行时属性(边框等)MyCustomView
@IBDesignable
class MyCustomView: UIView {
@IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
layer.cornerRadius = cornerRadius
layer.masksToBounds = cornerRadius > 0
}
}
@IBInspectable var borderWidth: CGFloat = 0 {
didSet {
layer.borderWidth = borderWidth
}
}
@IBInspectable var borderColor: UIColor? {
didSet {
layer.borderColor = borderColor?.CGColor
}
}
}
* 只有在 class MyCustomView
开始处设置 @IBDesignable
才起作用。
选项2(自Swift 1.2以来不可行,请查看注释)
扩展你的 UIView 类:
extension UIView {
@IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
layer.cornerRadius = cornerRadius
layer.masksToBounds = cornerRadius > 0
}
}
@IBInspectable var borderWidth: CGFloat = 0 {
didSet {
layer.borderWidth = borderWidth
}
}
@IBInspectable var borderColor: UIColor? {
didSet {
layer.borderColor = borderColor?.CGColor
}
}
}
这样做,您的默认视图始终在Attributes Inspector
中具有额外的可编辑字段。另一个优点是,您不必每次更改类别为MycustomView
。
然而,这种方法的一个缺点是,您只能在运行应用程序时看到更改。
您还可以使用您想要的颜色创建边框。
view.layer.borderColor = [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0].CGColor;
*r,g,b的值在0到255之间。
r
、g
和b
应该使用浮点数;除数也应该是浮点数:r / 255.0
。 - user577537在 UIView 的扩展中添加以下 @IBInspectables
extension UIView {
@IBInspectable var borderWidth: CGFloat {
get {
return layer.borderWidth
}
set(newValue) {
layer.borderWidth = newValue
}
}
@IBInspectable var borderColor: UIColor? {
get {
if let color = layer.borderColor {
return UIColor(CGColor: color)
}
return nil
}
set(newValue) {
layer.borderColor = newValue?.CGColor
}
}
}
接下来,您应该能够直接从属性检查器中设置borderColor和borderWidth属性。请参见附图。
view.layer.borderWidth = 1.0
view.layer.borderColor = UIColor.lightGray.cgColor
- (void)drawRect:(CGRect)rect
{
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(contextRef, 1);
CGContextSetRGBStrokeColor(contextRef, 255.0, 255.0, 255.0, 1.0);
CGContextStrokeRect(contextRef, rect);
}
setNeedsDisplay
。这将强制重新绘制 UIView,并隐式地重新调用 drawRect
。尚未测试... - Nicu Surdu请看以下代码:
view.layer.borderColor = [UIColor redColor].CGColor;
view.layer.borderWidth= 2.0;
[view setClipsToBounds:YES];
我想在@marczking的答案(选项1)下面添加评论,但是我的StackOverflow低级别身份阻止了这一点。
我将@marczking的答案移植到Objective C中。效果很好,谢谢@marczking!
UIView+Border.h:
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface UIView (Border)
-(void)setBorderColor:(UIColor *)color;
-(void)setBorderWidth:(CGFloat)width;
-(void)setCornerRadius:(CGFloat)radius;
@end
UIView+Border.m:
#import "UIView+Border.h"
@implementation UIView (Border)
// Note: cannot use synthesize in a Category
-(void)setBorderColor:(UIColor *)color
{
self.layer.borderColor = color.CGColor;
}
-(void)setBorderWidth:(CGFloat)width
{
self.layer.borderWidth = width;
}
-(void)setCornerRadius:(CGFloat)radius
{
self.layer.cornerRadius = radius;
self.layer.masksToBounds = radius > 0;
}
@end
我不建议重写drawRect方法,因为这会导致性能下降。
相反,我建议在自定义的UIView中修改类的属性,如下所示:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.layer.borderWidth = 2.f;
self.layer.borderColor = [UIColor redColor].CGColor;
}
return self;
采用以上方法时我没有看到任何故障 - 不确定为什么加入initWithFrame会停止这些故障;-)
@IBInspectable在iOS 9和Swift 2.0上对我有效
extension UIView {
@IBInspectable var borderWidth: CGFloat {
get {
return layer.borderWidth
}
set(newValue) {
layer.borderWidth = newValue
}
}
@IBInspectable var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set(newValue) {
layer.cornerRadius = newValue
}
}
@IBInspectable var borderColor: UIColor? {
get {
if let color = layer.borderColor {
return UIColor(CGColor: color)
}
return nil
}
set(newValue) {
layer.borderColor = newValue?.CGColor
}
}