[英]Cocoa Touch: How To Change UIView's Border Color And Thickness?
我在检查器中看到我可以更改背景颜色,但我还想更改边框颜色和粗细,这可能吗?
您需要使用视图的图层来设置边框属性。 例如:
#import <QuartzCore/QuartzCore.h>
...
view.layer.borderColor = [UIColor redColor].CGColor;
view.layer.borderWidth = 3.0f;
您还需要链接 QuartzCore.framework 以访问此功能。
由于 Xcode 的最新版本有一个更好的解决方案:
随着@IBInspectable
您可以直接设置属性从内部Attributes Inspector
。
这将为您设置User Defined Runtime Attributes
:
有两种设置方法:
选项 1 (在 Storyboard 中实时更新)
MyCustomView
。UIView
。@IBDesignable
(这会使视图更新@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
}
}
}
* @IBDesignable
仅在class MyCustomView
的开头设置时才有效
选项 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 之间的值。
在 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 属性。 见附件图片
当我使用 Vladimir 的 CALayer 解决方案时,并且在视图之上我有一个动画,比如模态 UINavigationController 关闭,我看到很多小故障发生并有绘图性能问题。
因此,实现此目的的另一种方法,但没有故障和性能损失,是制作自定义 UIView 并实现drawRect
消息,如下所示:
- (void)drawRect:(CGRect)rect
{
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(contextRef, 1);
CGContextSetRGBStrokeColor(contextRef, 255.0, 255.0, 255.0, 1.0);
CGContextStrokeRect(contextRef, rect);
}
view.layer.borderWidth = 1.0
view.layer.borderColor = UIColor.lightGray.cgColor
试试这个代码:
view.layer.borderColor = [UIColor redColor].CGColor;
view.layer.borderWidth= 2.0;
[view setClipsToBounds:YES];
由于会导致性能下降,我不建议覆盖 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 会阻止这些 ;-)
我想将此添加到@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
@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
}
}
如果您不想编辑 UIView 的图层,您始终可以将视图嵌入另一个视图中。 父视图会将其背景颜色设置为边框颜色。 它也会稍大一些,具体取决于您希望边框有多宽。
当然,这仅在您的视图不透明并且您只想要一个边框颜色时才有效。 OP 想要视图本身的边框,但这可能是一个可行的选择。
swift 4.2 中项目的边框颜色:
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell_lastOrderId") as! Cell_lastOrder
cell.layer.borderWidth = 1
cell.layer.borderColor = UIColor.white.cgColor
cell.layer.cornerRadius = 10
[self.view.layer setBorderColor: [UIColor colorWithRed:0.265 green:0.447 blue:0.767 alpha:1.0f].CGColor];
如果你想在不同的边上添加不同的边框,可以添加一个具有特定样式的子视图是一种很容易想到的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.