Guest User

Untitled

a guest
Feb 18th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. - (void)drawRect:(NSRect)dirtyRect {
  2. [super drawRect:dirtyRect];
  3.  
  4. //Drawing code here.
  5. [self setWantsLayer: YES];
  6. [self.layer setBorderWidth: 1];
  7.  
  8. [self.layer setBorderColor:[NSColor colorWithRed:205/255.0 green:211/255.0 blue:232/255.0 alpha:1.0].CGColor];
  9. [self.layer setCornerRadius: 10];
  10. }
  11.  
  12. - (void)drawRect:(NSRect)dirtyRect {
  13. [super drawRect:dirtyRect];
  14.  
  15. // Drawing code here.
  16. CGFloat dashPattern[] = {10,4}; //make your pattern here
  17. NSBezierPath *textViewSurround = [NSBezierPath bezierPathWithRoundedRect:self.frame xRadius:10 yRadius:10];
  18. [textViewSurround setLineWidth:2.0f];
  19. [textViewSurround setLineDash:dashPattern count:2 phase:0];
  20. [[NSColor colorWithRed:205/255.0 green:211/255.0 blue:232/255.0 alpha:1.0] set];
  21. [textViewSurround stroke];
  22. }
  23.  
  24. [yourView.layer setBorderWidth:5.0];
  25. [yourView.layer setBorderColor:[[UIColor colorWithPatternImage:[UIImage imageNamed:@"DotedImage.png"]] CGColor]];
  26.  
  27. #import <QuartzCore/QuartzCore.h>
  28.  
  29. class BorderedView: NSView {
  30. override func draw(_ dirtyRect: NSRect) {
  31. super.draw(dirtyRect)
  32.  
  33. // dash customization parameters
  34. let dashHeight: CGFloat = 3
  35. let dashLength: CGFloat = 10
  36. let dashColor: NSColor = .red
  37.  
  38. // setup the context
  39. let currentContext = NSGraphicsContext.current()!.cgContext
  40. currentContext.setLineWidth(dashHeight)
  41. currentContext.setLineDash(phase: 0, lengths: [dashLength])
  42. currentContext.setStrokeColor(dashColor.cgColor)
  43.  
  44. // draw the dashed path
  45. currentContext.addRect(bounds.insetBy(dx: dashHeight, dy: dashHeight))
  46. currentContext.strokePath()
  47. }
  48. }
  49.  
  50. class StrokeWithDashedLineView: NSView {
  51.  
  52. private let shapeLayer = CAShapeLayer()
  53. private let fillLayer = CALayer()
  54. private let textLabel = NSTextField().autolayoutView()
  55.  
  56. override init(frame frameRect: NSRect) {
  57. super.init(frame: frameRect)
  58. setupUI()
  59. setupLayout()
  60. }
  61.  
  62. required init?(coder decoder: NSCoder) {
  63. fatalError()
  64. }
  65.  
  66. override var intrinsicContentSize: NSSize {
  67. return CGSize(intrinsicHeight: 76)
  68. }
  69.  
  70. override func layout() {
  71. super.layout()
  72. updateLayers()
  73. }
  74.  
  75. private func updateLayers() {
  76. layer?.cornerRadius = 0.5 * bounds.height // Making ourselves rounded.
  77.  
  78. // Stroke Layer
  79. let shapeBounds = CGRect(width: bounds.width - shapeLayer.lineWidth, height: bounds.height - shapeLayer.lineWidth)
  80. let shapeRadius = 0.5 * shapeBounds.height
  81. let path = CGMutablePath()
  82. path.addRoundedRect(in: shapeBounds, cornerWidth: shapeRadius, cornerHeight: shapeRadius)
  83. shapeLayer.path = path
  84. shapeLayer.bounds = shapeBounds
  85. shapeLayer.position = CGPoint(x: 0.5 * shapeLayer.lineWidth, y: 0.5 * shapeLayer.lineWidth)
  86.  
  87. // Fill Layer
  88. let fillBounds = CGRect(width: bounds.width - 2 * shapeLayer.lineWidth, height: bounds.height - 2 * shapeLayer.lineWidth)
  89. fillLayer.cornerRadius = 0.5 * fillBounds.height
  90. fillLayer.bounds = fillBounds
  91. fillLayer.position = CGPoint(x: shapeLayer.lineWidth, y: shapeLayer.lineWidth)
  92. }
  93.  
  94. private func setupUI() {
  95. wantsLayer = true
  96. layer?.masksToBounds = true
  97.  
  98. shapeLayer.lineWidth = 3
  99. shapeLayer.strokeColor = NSColor.red.cgColor
  100. shapeLayer.fillColor = nil
  101. shapeLayer.lineDashPattern = [11.2, 11.2]
  102. shapeLayer.lineCap = .round
  103. shapeLayer.anchorPoint = .zero
  104.  
  105. fillLayer.backgroundColor = NSColor.yellow.cgColor
  106. fillLayer.anchorPoint = .zero
  107.  
  108. layer?.addSublayer(shapeLayer)
  109. layer?.addSublayer(fillLayer)
  110.  
  111. addSubview(textLabel)
  112.  
  113. textLabel.text = "Drag Xib or Storyboard files ontonthis window to open them"
  114. textLabel.alignment = .center
  115. textLabel.textColor = .black
  116. textLabel.font = NSFont.semibold(size: 13)
  117. textLabel.isEditable = false
  118. textLabel.drawsBackground = false
  119. textLabel.isBezeled = false
  120. }
  121.  
  122. private func setupLayout() {
  123. textLabel.centerXAnchor.constraint(equalTo: centerXAnchor).activate()
  124. textLabel.centerYAnchor.constraint(equalTo: centerYAnchor).activate()
  125. }
  126. }
Add Comment
Please, Sign In to add comment