繁体   English   中英

如何为NSView绘制虚线边框

[英]How to draw a dash line border for NSView

在我的自定义视图中,我的代码如下:

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];

     //Drawing code here.
    [self setWantsLayer: YES];
    [self.layer setBorderWidth: 1];

    [self.layer setBorderColor:[NSColor colorWithRed:205/255.0 green:211/255.0 blue:232/255.0 alpha:1.0].CGColor];
    [self.layer setCornerRadius: 10];
}

可以为我的NSView设置边框线和颜色,但是我想设置虚线,有人知道该怎么做吗? 我从网络搜索中尝试了一些代码,但它根本没有画出边界。

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];

    // Drawing code here.
    CGFloat dashPattern[] = {10,4}; //make your pattern here
    NSBezierPath *textViewSurround = [NSBezierPath bezierPathWithRoundedRect:self.frame xRadius:10 yRadius:10];
    [textViewSurround setLineWidth:2.0f];
    [textViewSurround setLineDash:dashPattern count:2 phase:0];
    [[NSColor colorWithRed:205/255.0 green:211/255.0 blue:232/255.0 alpha:1.0] set];
    [textViewSurround stroke];
}

这是在Swift 3中使用NSView子类的完整示例:

class BorderedView: NSView {
    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        // dash customization parameters
        let dashHeight: CGFloat = 3
        let dashLength: CGFloat = 10
        let dashColor: NSColor = .red

        // setup the context
        let currentContext = NSGraphicsContext.current()!.cgContext
        currentContext.setLineWidth(dashHeight)
        currentContext.setLineDash(phase: 0, lengths: [dashLength])
        currentContext.setStrokeColor(dashColor.cgColor)

        // draw the dashed path
        currentContext.addRect(bounds.insetBy(dx: dashHeight, dy: dashHeight))
        currentContext.strokePath()
    }
}

您可以通过CGContext做到这一点。这是对我有用的答案:

如何使虚线移动

我的结果是:

在此处输入图片说明

如果要使用CAShapeLayer (Swift 4.2)设置线边界:

class StrokeWithDashedLineView: NSView {

   private let shapeLayer = CAShapeLayer()
   private let fillLayer = CALayer()
   private let textLabel = NSTextField().autolayoutView()

   override init(frame frameRect: NSRect) {
      super.init(frame: frameRect)
      setupUI()
      setupLayout()
   }

   required init?(coder decoder: NSCoder) {
      fatalError()
   }

   override var intrinsicContentSize: NSSize {
      return CGSize(intrinsicHeight: 76)
   }

   override func layout() {
      super.layout()
      updateLayers()
   }

   private func updateLayers() {
      layer?.cornerRadius = 0.5 * bounds.height // Making ourselves rounded.

      // Stroke Layer
      let shapeBounds = CGRect(width: bounds.width - shapeLayer.lineWidth, height: bounds.height - shapeLayer.lineWidth)
      let shapeRadius = 0.5 * shapeBounds.height
      let path = CGMutablePath()
      path.addRoundedRect(in: shapeBounds, cornerWidth: shapeRadius, cornerHeight: shapeRadius)
      shapeLayer.path = path
      shapeLayer.bounds = shapeBounds
      shapeLayer.position = CGPoint(x: 0.5 * shapeLayer.lineWidth, y: 0.5 * shapeLayer.lineWidth)

      // Fill Layer
      let fillBounds = CGRect(width: bounds.width - 2 * shapeLayer.lineWidth, height: bounds.height - 2 * shapeLayer.lineWidth)
      fillLayer.cornerRadius = 0.5 * fillBounds.height
      fillLayer.bounds = fillBounds
      fillLayer.position = CGPoint(x: shapeLayer.lineWidth, y: shapeLayer.lineWidth)
   }

   private func setupUI() {
      wantsLayer = true
      layer?.masksToBounds = true

      shapeLayer.lineWidth = 3
      shapeLayer.strokeColor = NSColor.red.cgColor
      shapeLayer.fillColor = nil
      shapeLayer.lineDashPattern = [11.2, 11.2]
      shapeLayer.lineCap = .round
      shapeLayer.anchorPoint = .zero

      fillLayer.backgroundColor = NSColor.yellow.cgColor
      fillLayer.anchorPoint = .zero

      layer?.addSublayer(shapeLayer)
      layer?.addSublayer(fillLayer)

      addSubview(textLabel)

      textLabel.text = "Drag Xib or Storyboard files onto\nthis window to open them"
      textLabel.alignment = .center
      textLabel.textColor = .black
      textLabel.font = NSFont.semibold(size: 13)
      textLabel.isEditable = false
      textLabel.drawsBackground = false
      textLabel.isBezeled = false
   }

   private func setupLayout() {
      textLabel.centerXAnchor.constraint(equalTo: centerXAnchor).activate()
      textLabel.centerYAnchor.constraint(equalTo: centerYAnchor).activate()
   }
}

结果:

CAShapeLayer的窗口

你可以这样

[yourView.layer setBorderWidth:5.0];
[yourView.layer setBorderColor:[[UIColor colorWithPatternImage:[UIImage imageNamed:@"DotedImage.png"]] CGColor]];

在项目中添加虚线图像,然后在项目中导入QuartzCore/QuartzCore.h

#import <QuartzCore/QuartzCore.h>

更新:

图像尺寸和视图尺寸应相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM