@@ -54,62 +54,64 @@ class EditTextView: NSTextView, NSTextFinderClient, NSSharingServicePickerDelega
5454 override func draw( _ dirtyRect: NSRect ) {
5555 super. draw ( dirtyRect)
5656
57- let range = NSRange ( location: 0 , length: textStorage!. length)
58- attributedString ( ) . enumerateAttributes ( in: range, options: . reverse) {
59- attributes, range, stop in
57+ if #available( OSX 10 . 16 , * ) {
58+ let range = NSRange ( location: 0 , length: textStorage!. length)
59+ attributedString ( ) . enumerateAttributes ( in: range, options: . reverse) {
60+ attributes, range, stop in
6061
61- let tag = attributedString ( ) . attributedSubstring ( from: range) . string
62- guard attributes. index ( forKey: . tag) != nil , let font = attributes [ . font] as? NSFont else { return }
62+ let tag = attributedString ( ) . attributedSubstring ( from: range) . string
63+ guard attributes. index ( forKey: . tag) != nil , let font = attributes [ . font] as? NSFont else { return }
6364
64- guard let container = self . textContainer else { return }
65- guard let activeRange = self . layoutManager? . glyphRange ( forCharacterRange: range, actualCharacterRange: nil ) else { return }
65+ guard let container = self . textContainer else { return }
66+ guard let activeRange = self . layoutManager? . glyphRange ( forCharacterRange: range, actualCharacterRange: nil ) else { return }
6667
67- guard var tagRect = self . layoutManager? . boundingRect ( forGlyphRange: activeRange, in: container) else { return }
68+ guard var tagRect = self . layoutManager? . boundingRect ( forGlyphRange: activeRange, in: container) else { return }
6869
69- tagRect. origin. x += self . textContainerOrigin. x;
70- tagRect. origin. y += self . textContainerOrigin. y;
71- tagRect = self . convertToLayer ( tagRect)
70+ tagRect. origin. x += self . textContainerOrigin. x;
71+ tagRect. origin. y += self . textContainerOrigin. y;
72+ tagRect = self . convertToLayer ( tagRect)
7273
73- let tagAttributes = attributedString ( ) . attributes ( at: range. location, effectiveRange: nil )
74- let oneCharSize = ( " a " as NSString ) . size ( withAttributes: tagAttributes)
74+ let tagAttributes = attributedString ( ) . attributes ( at: range. location, effectiveRange: nil )
75+ let oneCharSize = ( " a " as NSString ) . size ( withAttributes: tagAttributes)
7576
76- let height = oneCharSize. height > tagRect. size. height ? tagRect. size. height : oneCharSize. height
77- let tagBorderRect = NSRect ( origin: CGPoint ( x: tagRect. origin. x- oneCharSize. width*0. 1 , y: tagRect. origin. y) , size: CGSize ( width: tagRect. size. width+ oneCharSize. width*0. 3 , height: height) )
77+ let height = oneCharSize. height > tagRect. size. height ? tagRect. size. height : oneCharSize. height
78+ let tagBorderRect = NSRect ( origin: CGPoint ( x: tagRect. origin. x- oneCharSize. width*0. 1 , y: tagRect. origin. y) , size: CGSize ( width: tagRect. size. width+ oneCharSize. width*0. 3 , height: height) )
7879
79- NSGraphicsContext . saveGraphicsState ( )
80+ NSGraphicsContext . saveGraphicsState ( )
8081
81- let path = NSBezierPath ( roundedRect: tagBorderRect, xRadius: 3 , yRadius: 3 )
82+ let path = NSBezierPath ( roundedRect: tagBorderRect, xRadius: 3 , yRadius: 3 )
8283
83- let fillColor = NSColor . tagColor
84- let strokeColor = NSColor . gray
85- let textColor = NSColor . white
84+ let fillColor = NSColor . tagColor
85+ let strokeColor = NSColor . gray
86+ let textColor = NSColor . white
8687
87- path. addClip ( )
88- fillColor. setFill ( )
89- strokeColor. setStroke ( )
90- tagBorderRect. fill ( using: . sourceOver)
88+ path. addClip ( )
89+ fillColor. setFill ( )
90+ strokeColor. setStroke ( )
91+ tagBorderRect. fill ( using: . sourceOver)
9192
92- // let transform = NSAffineTransform()
93- // transform.translateX(by: 0.5, yBy: 0.5)
94- // path.transform(using: transform as AffineTransform)
95- // path.stroke()
96- // transform.translateX(by: -1.5, yBy: -1.5)
97- // path.transform(using: transform as AffineTransform)
98- // path.stroke()
93+ // let transform = NSAffineTransform()
94+ // transform.translateX(by: 0.5, yBy: 0.5)
95+ // path.transform(using: transform as AffineTransform)
96+ // path.stroke()
97+ // transform.translateX(by: -1.5, yBy: -1.5)
98+ // path.transform(using: transform as AffineTransform)
99+ // path.stroke()
99100
100- let resFont = NSFontManager . shared. convert ( font, toSize: font. pointSize)
101- let dict = NSMutableDictionary ( dictionary: tagAttributes)
101+ let resFont = NSFontManager . shared. convert ( font, toSize: font. pointSize)
102+ let dict = NSMutableDictionary ( dictionary: tagAttributes)
102103
103- dict. addEntries ( from: [
104- NSAttributedString . Key. font: resFont,
105- NSAttributedString . Key. foregroundColor: textColor
106- ] )
107- dict. removeObject ( forKey: NSAttributedString . Key. link)
104+ dict. addEntries ( from: [
105+ NSAttributedString . Key. font: resFont,
106+ NSAttributedString . Key. foregroundColor: textColor
107+ ] )
108+ dict. removeObject ( forKey: NSAttributedString . Key. link)
108109
109- let newRect = tagRect. offsetBy ( dx: 0 , dy: - 1 )
110- ( tag as NSString ) . draw ( in: newRect, withAttributes: ( dict as! [ NSAttributedString . Key : Any ] ) )
110+ let newRect = tagRect. offsetBy ( dx: 0 , dy: - 1 )
111+ ( tag as NSString ) . draw ( in: newRect, withAttributes: ( dict as! [ NSAttributedString . Key : Any ] ) )
111112
112- NSGraphicsContext . restoreGraphicsState ( )
113+ NSGraphicsContext . restoreGraphicsState ( )
114+ }
113115 }
114116 }
115117
0 commit comments