From c136c6ac1e8cd022831d39048d09d4680f5a1210 Mon Sep 17 00:00:00 2001 From: Marc Stibane Date: Mon, 30 Oct 2023 18:22:37 +0100 Subject: use sizeCategory --- TalerWallet1/Helper/TalerStrings.swift | 9 ++++++-- TalerWallet1/Views/Balances/PendingRowView.swift | 27 ++++++++++++++-------- TalerWallet1/Views/HelperViews/AmountRowV.swift | 7 ------ .../Views/Transactions/TransactionRowView.swift | 17 ++++++++------ 4 files changed, 34 insertions(+), 26 deletions(-) diff --git a/TalerWallet1/Helper/TalerStrings.swift b/TalerWallet1/Helper/TalerStrings.swift index 64fb58a..117b5ee 100644 --- a/TalerWallet1/Helper/TalerStrings.swift +++ b/TalerWallet1/Helper/TalerStrings.swift @@ -49,16 +49,21 @@ extension String { func widthOfString(usingUIFont font: UIFont, _ sizeCategory: ContentSizeCategory) -> CGFloat { let width = widthOfString(usingUIFont: font) let correctForSize = correctForSize(sizeCategory) - return width * correctForSize } + public func width(largeAmountFont: Bool, _ sizeCategory: ContentSizeCategory) -> CGFloat { + let uiFont = TalerFont.uiFont(largeAmountFont ? .title : .title2) + return widthOfString(usingUIFont: uiFont, sizeCategory) + } + // This would be used like so: // let uiFont = UIFont.systemFont(ofSize: 17, weight: .bold) // let width = "SomeString".widthOfString(usingUIFont: uiFont) /// fileprivate func correctForSize(_ sizeCategory: ContentSizeCategory) -> CGFloat { + // empirical values let corrValue = switch sizeCategory { case .extraSmall: 0.7 case .small: 0.8 @@ -75,7 +80,7 @@ extension String { default: 1.0 } if ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" { - // empirical values + // directly return the empirical value return corrValue } else { // preview doesn't use ContentSizeCategory for widthOfString(usingUIFont) diff --git a/TalerWallet1/Views/Balances/PendingRowView.swift b/TalerWallet1/Views/Balances/PendingRowView.swift index 223b080..f45d1fa 100644 --- a/TalerWallet1/Views/Balances/PendingRowView.swift +++ b/TalerWallet1/Views/Balances/PendingRowView.swift @@ -10,24 +10,25 @@ struct PendingRowContentV: View { let isHorizontal: Bool let incoming: Bool - public static func width(titles: (String, String?), isHorizontal: Bool) -> CGFloat { + public static func width(titles: (String, String?), isHorizontal: Bool, + sizeCategory: ContentSizeCategory) -> CGFloat { let imageFont = TalerFont.uiFont(.largeTitle) let uiFont = TalerFont.uiFont(.body) let image = "++" - let imageWidth = image.widthOfString(usingUIFont: imageFont) + 8.0 // spacing: 8 + let imageWidth = image.widthOfString(usingUIFont: imageFont, sizeCategory) + 8.0 // spacing: 8 let (title1, title2) = titles - let title1Width = title1.widthOfString(usingUIFont: uiFont) + let title1Width = title1.widthOfString(usingUIFont: uiFont, sizeCategory) var title2Width = 0.0 var totalWidth = title1Width if let title2 { - title2Width = title2.widthOfString(usingUIFont: uiFont) + title2Width = title2.widthOfString(usingUIFont: uiFont, sizeCategory) let totalStr = title1 + " " + title2 - totalWidth = totalStr.widthOfString(usingUIFont: uiFont) + totalWidth = totalStr.widthOfString(usingUIFont: uiFont, sizeCategory) } -// let logStr = String(format: "image: %.2f title: %.2f total: %.2f", imageWidth, max(title1Width, title2Width), totalWidth) -// print(logStr) + let logStr = String(format: "image: %.2f title: %.2f total: %.2f", imageWidth, max(title1Width, title2Width), totalWidth) + print(logStr) return imageWidth + (isHorizontal ? totalWidth : max(title1Width, title2Width)) } @@ -55,6 +56,7 @@ struct PendingRowView: View { let currencyInfo: CurrencyInfo? let incoming: Bool + @Environment(\.sizeCategory) var sizeCategory @AppStorage("iconOnly") var iconOnly: Bool = false let inTitle0 = String(localized: "Incoming", comment: "Abbreviation ") @@ -65,7 +67,12 @@ struct PendingRowView: View { let outTitle2 = String(localized: "outgoing", comment: "Bottom of line ") func needVStack(available: CGFloat, contentWidth: CGFloat, valueWidth: CGFloat) -> Bool { - available < (contentWidth + valueWidth + 40) + if available > 20 { + let logStr = String(format: "available: %.2f sum: %.2f = content: %.2f + value: %.2f", + available, contentWidth + valueWidth, contentWidth, valueWidth) + print(logStr) + } + return available < (contentWidth + valueWidth + 20) } var body: some View { @@ -73,7 +80,7 @@ struct PendingRowView: View { SingleAxisGeometryReader { width in Group { let amountStr = amount.string(currencyInfo) - let amountWidth = amountStr.width(largeAmountFont: false) + let amountWidth = amountStr.width(largeAmountFont: false, sizeCategory) let inTitle = iconOnly ? (inTitle0, nil) : (inTitle1, inTitle2) let outTitle = iconOnly ? (outTitle0, nil) @@ -81,7 +88,7 @@ struct PendingRowView: View { let title = incoming ? inTitle : outTitle let contentWidth = PendingRowContentV.width(titles: incoming ? inTitle : outTitle, - isHorizontal: true) + isHorizontal: false, sizeCategory: sizeCategory) let needVStack = needVStack(available: width, contentWidth: contentWidth, valueWidth: amountWidth) AmountRowV(amountStr: amountStr, amountColor: pendingColor, largeAmountFont: false, diff --git a/TalerWallet1/Views/HelperViews/AmountRowV.swift b/TalerWallet1/Views/HelperViews/AmountRowV.swift index 99f1ea3..b3140e7 100644 --- a/TalerWallet1/Views/HelperViews/AmountRowV.swift +++ b/TalerWallet1/Views/HelperViews/AmountRowV.swift @@ -10,13 +10,6 @@ import taler_swift // if it fits side by side, then render HStack(content(compact), Spacer(), amountStr) // else render VStack(content(wide), HStack(Spacer(), amountStr)) -extension String { - public func width(largeAmountFont: Bool) -> CGFloat { - let uiFont = TalerFont.uiFont(largeAmountFont ? .title : .title2) - return widthOfString(usingUIFont: uiFont) - } -} - struct AmountRowV: View { let amountStr: String let amountColor: Color diff --git a/TalerWallet1/Views/Transactions/TransactionRowView.swift b/TalerWallet1/Views/Transactions/TransactionRowView.swift index a2d2c0f..fc28f09 100644 --- a/TalerWallet1/Views/Transactions/TransactionRowView.swift +++ b/TalerWallet1/Views/Transactions/TransactionRowView.swift @@ -12,21 +12,22 @@ struct TransactionRowContentV: View { let incoming: Bool let foreColor:Color - public static func width(titles: (String, String?), isHorizontal: Bool) -> CGFloat { + public static func width(titles: (String, String?), isHorizontal: Bool, + sizeCategory: ContentSizeCategory) -> CGFloat { let imageFont = TalerFont.uiFont(.largeTitle) let uiFont1 = TalerFont.uiFont(.headline) let uiFont2 = TalerFont.uiFont(.callout) let image = "++" - let imageWidth = image.widthOfString(usingUIFont: imageFont) + 8.0 // spacing: 8 + let imageWidth = image.widthOfString(usingUIFont: imageFont, sizeCategory) + 8.0 // spacing: 8 let (title1, title2) = titles - let title1Width = title1.widthOfString(usingUIFont: uiFont1) + let title1Width = title1.widthOfString(usingUIFont: uiFont1, sizeCategory) var title2Width = 0.0 var totalWidth = title1Width if let title2 { - title2Width = title2.widthOfString(usingUIFont: uiFont2) + title2Width = title2.widthOfString(usingUIFont: uiFont2, sizeCategory) let blankStr = " " - totalWidth += blankStr.widthOfString(usingUIFont: uiFont1) + title2Width + totalWidth += blankStr.widthOfString(usingUIFont: uiFont1, sizeCategory) + title2Width } // let logStr = String(format: "image: %.2f title: %.2f total: %.2f", imageWidth, max(title1Width, title2Width), totalWidth) @@ -60,6 +61,8 @@ struct TransactionRowView: View { let transaction : Transaction let currencyInfo: CurrencyInfo? + @Environment(\.sizeCategory) var sizeCategory + func needVStack(available: CGFloat, contentWidth: CGFloat, valueWidth: CGFloat) -> Bool { available < (contentWidth + valueWidth + 40) } @@ -81,10 +84,10 @@ struct TransactionRowView: View { SingleAxisGeometryReader { width in Group { let amountStr = amount.string(currencyInfo) - let amountWidth = amountStr.width(largeAmountFont: false) + let amountWidth = amountStr.width(largeAmountFont: false, sizeCategory) let contentWidth = TransactionRowContentV.width(titles: (transaction.localizedType, dateString), - isHorizontal: false) + isHorizontal: false, sizeCategory: sizeCategory) let needVStack = needVStack(available: width, contentWidth: contentWidth, valueWidth: amountWidth) AmountRowV(amountStr: amountStr, amountColor: foreColor, largeAmountFont: false, -- cgit v1.2.3