From e21d5accc8ac1fc4988cd0bbe61fedb7311e9deb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20B=2E=20St=C3=B8vring?= Date: Sat, 16 Mar 2024 16:28:28 +0100 Subject: [PATCH] Adds TabWidthMeasurer (#360) * Adds TabWidthMeasurer * Fixes typo --- Sources/Runestone/Library/TabWidthMeasurer.swift | 12 ++++++++++++ .../Runestone/TextView/Indent/IndentController.swift | 7 +------ .../Runestone/TextView/Indent/IndentStrategy.swift | 2 +- 3 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 Sources/Runestone/Library/TabWidthMeasurer.swift diff --git a/Sources/Runestone/Library/TabWidthMeasurer.swift b/Sources/Runestone/Library/TabWidthMeasurer.swift new file mode 100644 index 000000000..94bc5b69a --- /dev/null +++ b/Sources/Runestone/Library/TabWidthMeasurer.swift @@ -0,0 +1,12 @@ +import UIKit + +enum TabWidthMeasurer { + static func tabWidth(tabLength: Int, font: UIFont) -> CGFloat { + let str = String(repeating: " ", count: tabLength) + let maxSize = CGSize(width: CGFloat.greatestFiniteMagnitude, height: .greatestFiniteMagnitude) + let options: NSStringDrawingOptions = [.usesFontLeading, .usesLineFragmentOrigin] + let attributes: [NSAttributedString.Key: Any] = [.font: font] + let bounds = str.boundingRect(with: maxSize, options: options, attributes: attributes, context: nil) + return round(bounds.size.width) + } +} diff --git a/Sources/Runestone/TextView/Indent/IndentController.swift b/Sources/Runestone/TextView/Indent/IndentController.swift index e4c0fba89..22d92dc12 100644 --- a/Sources/Runestone/TextView/Indent/IndentController.swift +++ b/Sources/Runestone/TextView/Indent/IndentController.swift @@ -30,12 +30,7 @@ final class IndentController { if let tabWidth = _tabWidth { return tabWidth } else { - let str = String(repeating: " ", count: indentStrategy.tabLength) - let maxSize = CGSize(width: CGFloat.greatestFiniteMagnitude, height: .greatestFiniteMagnitude) - let options: NSStringDrawingOptions = [.usesFontLeading, .usesLineFragmentOrigin] - let attributes: [NSAttributedString.Key: Any] = [.font: indentFont] - let bounds = str.boundingRect(with: maxSize, options: options, attributes: attributes, context: nil) - let tabWidth = round(bounds.size.width) + let tabWidth = TabWidthMeasurer.tabWidth(tabLength: indentStrategy.tabLength, font: indentFont) if tabWidth != _tabWidth { _tabWidth = tabWidth delegate?.indentControllerDidUpdateTabWidth(self) diff --git a/Sources/Runestone/TextView/Indent/IndentStrategy.swift b/Sources/Runestone/TextView/Indent/IndentStrategy.swift index b0b4aaa2b..67532c30e 100644 --- a/Sources/Runestone/TextView/Indent/IndentStrategy.swift +++ b/Sources/Runestone/TextView/Indent/IndentStrategy.swift @@ -2,7 +2,7 @@ import Foundation /// Strategy to use when indenting text. public enum IndentStrategy: Equatable { - /// Indent using tabs. The length specified length is used to determine the width of the tab measured in space characers. + /// Indent using tabs. The specified length is used to determine the width of the tab measured in space characers. case tab(length: Int) /// Indent using a number of spaces. case space(length: Int)