Skip to content

Commit 401a751

Browse files
committed
Do not count last white space in a line
1 parent abc0a90 commit 401a751

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

Squirrel Designer/LayoutModel.swift

+9-13
Original file line numberDiff line numberDiff line change
@@ -554,17 +554,7 @@ class SquirrelView: NSView {
554554
}
555555
// Get the rectangle containing entire contents, expensive to calculate
556556
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))
568558
}
569559
// Get the rectangle containing the range of text, will first convert to glyph range, expensive to calculate
570560
func contentRect(forRange range: NSRange) -> NSRect {
@@ -573,8 +563,14 @@ class SquirrelView: NSView {
573563
var actualWidth: CGFloat = 0
574564
_text.layoutManagers[0].enumerateLineFragments(forGlyphRange: glyphRange) {
575565
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+
}
578574
}
579575
}
580576
rect.size.width = actualWidth

0 commit comments

Comments
 (0)