1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
/*
* This file is part of GNU Taler, ©2022-23 Taler Systems S.A.
* See LICENSE.md
*/
import SwiftUI
import UIKit
extension View {
@MainActor
public func announce(this: String) {
if UIAccessibility.isVoiceOverRunning {
UIAccessibility.post(notification: .screenChanged, argument: this)
}
}
}
extension View {
/// if sameSize then this searches for the longest title
/// returns true if any of the strings in 'titles' wouldn't fit in a view 1/'numViews' of the size of 'width', with 'spacing'
/// if !sameSize then all titles are added with spacing
static func fitsSideBySide(_ titles: [(String, UIFont)],
availableWidth: CGFloat, // total width available
sizeCategory: ContentSizeCategory,
spacing: CGFloat = HSPACING, // between titles
padding: CGFloat = 20,
sameSize: Bool = true,
numViews: Int = 2)
-> Bool {
// let padding = 20.0 // TODO: depend on myListStyle
let totalSpacing = spacing * CGFloat(numViews - 1)
var totalWidth = padding + totalSpacing
var maxTitleWidth = 0.0
var minTitleWidth = 1000.0
for (title, uiFont) in titles {
let titleWidth = title.widthOfString(usingUIFont: uiFont, sizeCategory)
if titleWidth > maxTitleWidth {
maxTitleWidth = titleWidth
}
if titleWidth < minTitleWidth {
minTitleWidth = titleWidth
}
// if availableWidth > 20 {
// let widthStr = String(format: "%.2f + %.2f = %.2f", totalWidth, titleWidth, totalWidth + titleWidth)
// print("❗️ \(title) \(widthStr)")
// }
totalWidth += titleWidth
}
if sameSize {
let nettoWidth = availableWidth - totalSpacing
let singleWidth = nettoWidth / CGFloat(numViews)
let neededWidth = padding + maxTitleWidth
// if availableWidth > 20 {
// let width1Str = String(format: "%.2f -spacing %.2f = %.2f / %d = %.2f", availableWidth, totalSpacing,
// nettoWidth, numViews, singleWidth)
// let width2Str = String(format: "%.2f +padding %.2f = %.2f", maxTitleWidth, padding, maxTitleWidth + padding)
// print("❗️ available: \(width1Str) needed: \(width2Str)")
// }
return neededWidth < singleWidth
} else {
// if availableWidth > 20 {
// let totalStr = String(format: "%.2f -spacing %.2f = %.2f -padding %.2f = %.2f", totalWidth, totalSpacing,
// totalWidth - totalSpacing, padding, totalWidth - totalSpacing - padding)
// print("❗️ view width: \(availableWidth) total: \(totalStr)")
// let (amount, font) = titles[1]
// print("❗️ view width: \(width) total: \(totalStr) min: \(minTitleWidth) max: \(maxTitleWidth) \(amount)")
// }
return totalWidth < availableWidth
}
}
}
|