|
| 1 | +import SwiftUI |
| 2 | + |
| 3 | +struct DimensionLabel<Content: View>: View { |
| 4 | + @ViewBuilder var content: Content |
| 5 | + |
| 6 | + var edge: Edge? |
| 7 | + |
| 8 | + var body: some View { |
| 9 | + content |
| 10 | + .textRenderer(SubpixelRenderer()) |
| 11 | + .contentTransition(.identity) |
| 12 | + .transaction { t in |
| 13 | + t.disablesAnimations = true |
| 14 | + t.animation = nil |
| 15 | + } |
| 16 | + .font(font) |
| 17 | + .kerning(0.2) |
| 18 | + .multilineTextAlignment(.center) |
| 19 | + .foregroundStyle(.white.shadow(.drop(color: .black.opacity(0.4), radius: 0, x: 1, y: 1))) |
| 20 | + .padding(.horizontal, 2.5) |
| 21 | + .padding(.vertical, 1.3) |
| 22 | + .background(.foreground, in: LabelBackground(edge: edge)) |
| 23 | + .fixedSize() |
| 24 | + .alignmentGuide(HorizontalAlignment.dimensionLabel) { d in |
| 25 | + switch edge { |
| 26 | + case .top, .bottom, nil: d[HorizontalAlignment.center] |
| 27 | + case .leading: d[.leading] - 8 |
| 28 | + case .trailing: d[.trailing] + 8 |
| 29 | + } |
| 30 | + } |
| 31 | + .alignmentGuide(VerticalAlignment.dimensionLabel) { d in |
| 32 | + switch edge { |
| 33 | + case .leading, .trailing, nil: d[VerticalAlignment.center] |
| 34 | + case .top: d[.top] - 8 |
| 35 | + case .bottom: d[.bottom] + 8 |
| 36 | + } |
| 37 | + } |
| 38 | + .geometryGroup() |
| 39 | + } |
| 40 | + |
| 41 | + var font: Font { |
| 42 | + Font.system(size: 7.25, weight: .semibold) |
| 43 | + .monospacedDigit() |
| 44 | + .width(.init(-0.1)) |
| 45 | + } |
| 46 | + |
| 47 | + init(edge: Edge? = nil, value: CGFloat) where Content == Text { |
| 48 | + self.content = Text(value, format: .number.precision(.fractionLength(2 ... 2))) |
| 49 | + self.edge = edge |
| 50 | + } |
| 51 | + |
| 52 | + init(edge: Edge? = nil, value: CGSize) where Content == Text { |
| 53 | + self.content = Text("\(value.width, format: .number.precision(.fractionLength(2 ... 2)))×\(value.height, format: .number.precision(.fractionLength(2 ... 2)))") |
| 54 | + self.edge = edge |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +extension VerticalAlignment { |
| 59 | + struct DimensionLabel: AlignmentID { |
| 60 | + static func defaultValue(in context: ViewDimensions) -> CGFloat { |
| 61 | + context[VerticalAlignment.center] |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + static let dimensionLabel = VerticalAlignment(DimensionLabel.self) |
| 66 | +} |
| 67 | + |
| 68 | +extension HorizontalAlignment { |
| 69 | + struct DimensionLabel: AlignmentID { |
| 70 | + static func defaultValue(in context: ViewDimensions) -> CGFloat { |
| 71 | + context[HorizontalAlignment.center] |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + static let dimensionLabel = HorizontalAlignment(DimensionLabel.self) |
| 76 | +} |
| 77 | + |
| 78 | +extension Alignment { |
| 79 | + static var dimensionLabel: Alignment { |
| 80 | + .init(horizontal: .dimensionLabel, vertical: .dimensionLabel) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +private struct LabelBackground: Shape { |
| 85 | + var edge: Edge? |
| 86 | + |
| 87 | + var cornerRadius: CGFloat = 3 |
| 88 | + |
| 89 | + nonisolated func path(in rect: CGRect) -> Path { |
| 90 | + var triangle = Path() |
| 91 | + |
| 92 | + let r = cornerRadius * 2 * 1.528665 |
| 93 | + |
| 94 | + let dimension = edge == .leading || edge == .trailing ? rect.height : rect.width |
| 95 | + let w = max(2, min((dimension - r) / 2, 4)) |
| 96 | + |
| 97 | + switch edge { |
| 98 | + case nil: |
| 99 | + break |
| 100 | + case .leading: |
| 101 | + triangle.addLines([ |
| 102 | + rect[.leading].offset(y: w), |
| 103 | + rect[.leading].offset(x: -4), |
| 104 | + rect[.leading].offset(y: -w) |
| 105 | + ]) |
| 106 | + case .trailing: |
| 107 | + triangle.addLines([ |
| 108 | + rect[.trailing].offset(y: w), |
| 109 | + rect[.trailing].offset(x: 4), |
| 110 | + rect[.trailing].offset(y: -w) |
| 111 | + ]) |
| 112 | + case .top: |
| 113 | + triangle.addLines([ |
| 114 | + rect[.top].offset(x: w), |
| 115 | + rect[.top].offset(y: -4), |
| 116 | + rect[.top].offset(x: -w) |
| 117 | + ]) |
| 118 | + case .bottom: |
| 119 | + triangle.addLines([ |
| 120 | + rect[.bottom].offset(x: w), |
| 121 | + rect[.bottom].offset(y: 4), |
| 122 | + rect[.bottom].offset(x: -w) |
| 123 | + ]) |
| 124 | + } |
| 125 | + triangle.closeSubpath() |
| 126 | + |
| 127 | + return Path(roundedRect: rect, cornerRadius: cornerRadius).union(triangle) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +struct SubpixelRenderer: TextRenderer { |
| 132 | + func draw(layout: Text.Layout, in ctx: inout GraphicsContext) { |
| 133 | + ctx.translateBy(x: 0, y: -0.33333333) |
| 134 | + |
| 135 | + for line in layout { |
| 136 | + ctx.draw(line, options: .disablesSubpixelQuantization) |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +#Preview { |
| 142 | + VStack { |
| 143 | + DimensionLabel(edge: .leading, value: 1234567890) |
| 144 | + DimensionLabel(edge: .top, value: 1234567890) |
| 145 | + DimensionLabel(edge: .trailing, value: 1234567890) |
| 146 | + DimensionLabel(edge: .bottom, value: 1234567890) |
| 147 | + |
| 148 | + DimensionLabel(value: CGSize(width: 100, height: 100)) |
| 149 | + } |
| 150 | + |
| 151 | + VStack { |
| 152 | + DimensionLabel(value: 10.20) |
| 153 | + DimensionLabel(value: 10.666667) |
| 154 | + DimensionLabel(value: 10.3333333333) |
| 155 | + DimensionLabel(value: 10.99999) |
| 156 | + } |
| 157 | + .foregroundStyle(.purple) |
| 158 | +} |
0 commit comments