@@ -554,17 +554,7 @@ class SquirrelView: NSView {
554
554
}
555
555
// Get the rectangle containing entire contents, expensive to calculate
556
556
var contentRect : NSRect {
557
- let glyphRange = _text. layoutManagers [ 0 ] . glyphRange ( for: _text. layoutManagers [ 0 ] . textContainers [ 0 ] )
558
- var rect = _text. layoutManagers [ 0 ] . boundingRect ( forGlyphRange: glyphRange, in: _text. layoutManagers [ 0 ] . textContainers [ 0 ] )
559
- var actualWidth : CGFloat = 0
560
- _text. layoutManagers [ 0 ] . enumerateLineFragments ( forGlyphRange: glyphRange) {
561
- rect, usedRect, container, usedRange, stop in
562
- if actualWidth < usedRect. size. width {
563
- actualWidth = usedRect. size. width
564
- }
565
- }
566
- rect. size. width = actualWidth
567
- return rect
557
+ self . contentRect ( forRange: NSMakeRange ( 0 , _text. length) )
568
558
}
569
559
// Get the rectangle containing the range of text, will first convert to glyph range, expensive to calculate
570
560
func contentRect( forRange range: NSRange ) -> NSRect {
@@ -573,8 +563,14 @@ class SquirrelView: NSView {
573
563
var actualWidth : CGFloat = 0
574
564
_text. layoutManagers [ 0 ] . enumerateLineFragments ( forGlyphRange: glyphRange) {
575
565
rect, usedRect, container, usedRange, stop in
576
- if actualWidth < usedRect. size. width {
577
- actualWidth = usedRect. size. width
566
+ let str = self . _text. attributedSubstring ( from: usedRange) . string as NSString
567
+ let nonWhiteCharLocation = str. rangeOfCharacter ( from: . whitespaces. inverted, options: . backwards)
568
+ if nonWhiteCharLocation. location != NSNotFound {
569
+ let newRange = NSMakeRange ( usedRange. location, nonWhiteCharLocation. location+ 1 )
570
+ let lineWidth = self . _text. attributedSubstring ( from: newRange) . size ( ) . width
571
+ if actualWidth < lineWidth {
572
+ actualWidth = lineWidth
573
+ }
578
574
}
579
575
}
580
576
rect. size. width = actualWidth
0 commit comments