commit 8b08e6e2c4df24682ed10ffb8ddb98f7bf523697 parent 696dfc84922362363a75859aeb6b3bf3c8366dc3 Author: Marc Stibane <marc@taler.net> Date: Fri, 13 Oct 2023 10:31:38 +0200 width of rendered string Diffstat:
| M | TalerWallet1/Helper/TalerStrings.swift | | | 13 | ++++++++++++- |
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/TalerWallet1/Helper/TalerStrings.swift b/TalerWallet1/Helper/TalerStrings.swift @@ -3,9 +3,9 @@ * See LICENSE.md */ import Foundation +import UIKit extension StringProtocol { - func trimURL() -> String { if let url = URL(string: String(self)) { if let host = url.host { @@ -15,3 +15,14 @@ extension StringProtocol { return String(self) } } + +extension String { + func widthOfString(usingUIFont font: UIFont) -> CGFloat { + let fontAttributes = [NSAttributedString.Key.font: font] + let size = self.size(withAttributes: fontAttributes) + return size.width + } +} +// This would be used like so: +// let uiFont = UIFont.systemFont(ofSize: 17, weight: .bold) +// let width = "SomeString".widthOfString(usingUIFont: uiFont)