Skip to content

Commit

Permalink
Do not count last white space in a line
Browse files Browse the repository at this point in the history
  • Loading branch information
LEOYoon-Tsaw committed Jun 1, 2022
1 parent abc0a90 commit 401a751
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions Squirrel Designer/LayoutModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -554,17 +554,7 @@ class SquirrelView: NSView {
}
// Get the rectangle containing entire contents, expensive to calculate
var contentRect: NSRect {
let glyphRange = _text.layoutManagers[0].glyphRange(for: _text.layoutManagers[0].textContainers[0])
var rect = _text.layoutManagers[0].boundingRect(forGlyphRange: glyphRange, in: _text.layoutManagers[0].textContainers[0])
var actualWidth: CGFloat = 0
_text.layoutManagers[0].enumerateLineFragments(forGlyphRange: glyphRange) {
rect, usedRect, container, usedRange, stop in
if actualWidth < usedRect.size.width {
actualWidth = usedRect.size.width
}
}
rect.size.width = actualWidth
return rect
self.contentRect(forRange: NSMakeRange(0, _text.length))
}
// Get the rectangle containing the range of text, will first convert to glyph range, expensive to calculate
func contentRect(forRange range: NSRange) -> NSRect {
Expand All @@ -573,8 +563,14 @@ class SquirrelView: NSView {
var actualWidth: CGFloat = 0
_text.layoutManagers[0].enumerateLineFragments(forGlyphRange: glyphRange) {
rect, usedRect, container, usedRange, stop in
if actualWidth < usedRect.size.width {
actualWidth = usedRect.size.width
let str = self._text.attributedSubstring(from: usedRange).string as NSString
let nonWhiteCharLocation = str.rangeOfCharacter(from: .whitespaces.inverted, options: .backwards)
if nonWhiteCharLocation.location != NSNotFound {
let newRange = NSMakeRange(usedRange.location, nonWhiteCharLocation.location+1)
let lineWidth = self._text.attributedSubstring(from: newRange).size().width
if actualWidth < lineWidth {
actualWidth = lineWidth
}
}
}
rect.size.width = actualWidth
Expand Down

0 comments on commit 401a751

Please sign in to comment.