Skip to content

Commit e4dae8a

Browse files
committed
Move font picker views into new RichTextFont namespace
1 parent e849315 commit e4dae8a

21 files changed

+764
-753
lines changed

RELEASE_NOTES.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,32 @@ This release starts moving types and views that relate to other types into the t
1414

1515
* `RichTextAlignment.Picker` has a new style parameter.
1616
* `RichTextCommand` is a new namespace for command-related views.
17-
* `RichTextColorPicker` now shows a quick button to reset the color.
17+
* `RichTextColor.Picker` now shows a quick button to reset the color.
1818
* `RichTextLabelValue` is a new protocol to harmonize label-compatible label values.
1919

2020
### 💡 Adjustments
2121

2222
* Many value types implement `RichTextLabelValue` to get a `label` property.
2323
* All types that implement `RichTextLabelValue` get a `label` that has improved accessibility.
2424

25-
* `RichTextColor` `.adjust` now takes an optional
25+
* `RichTextColor` `.adjust` now takes an optional color.
2626

2727
### 🐛 Bug Fixes
2828

2929
* `Image.symbol(...)` removes `palette` rendering mode to fix incorrect color scheme behavior.
30-
* `RichTextColorPicker` no longer auto-adjusts black and white to make it possible to actually set those colors.
30+
* `RichTextColor.Picker` no longer auto-adjusts black and white to make it possible to actually set those colors.
3131

3232
### 🗑️ Deprecations
3333

34-
* `RichTextActionButton` has been renamed to `RichTextAction.Button`.
35-
* `RichTextActionButtonGroup` has been renamed to `RichTextAction.ButtonGroup`.
36-
* `RichTextActionButtonStack` has been renamed to `RichTextAction.ButtonStack`.
37-
* `RichTextAlignmentPicker` has been renamed to `RichTextAlignment.Picker`.
34+
* `RichTextAction*` views have been renamed to `RichTextAction.Button*`.
35+
* `RichTextAlignment*` views have been renamed to `RichTextAlignment.*`.
3836
* `RichTextArgumentReader` deprecates the font name and size functions.
3937
* `RichTextArgumentWriter` deprecates the font name and size functions.
40-
* `RichTextColorPicker` has been renamed to `RichTextColor.Picker`.
38+
* `RichTextColor*` views have been renamed to `RichTextColor.*`.
4139
* `RichTextCommand` views are now nested within the new `RichTextCommand` type.
4240
* `RichTextComponent` deprecates the font name and size functions.
4341
* `RichTextDataFormatMenu` has been renamed to `RichTextDataFormat.Menu`.
42+
* `RichTextFont*` views have been renamed to `RichTextFont.*`.
4443

4544

4645

Sources/RichTextKit/Alignment/RichTextAlignment+Picker.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,14 @@ struct RichTextAlignment_Picker_Previews: PreviewProvider {
5858
private var alignment = RichTextAlignment.left
5959

6060
var body: some View {
61-
RichTextAlignment.Picker(
62-
selection: $alignment,
63-
values: .all
64-
)
61+
VStack(spacing: 10) {
62+
RichTextAlignment.Picker(
63+
selection: $alignment,
64+
values: .all
65+
)
66+
.withPreviewPickerStyles()
67+
}
68+
.padding()
6569
}
6670
}
6771

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
//
2+
// RichTextFont+ForEachPicker.swift
3+
// RichTextKit
4+
//
5+
// Created by Daniel Saidi on 2022-06-01.
6+
// Copyright © 2022-2023 Daniel Saidi. All rights reserved.
7+
//
8+
9+
import SwiftUI
10+
11+
public extension RichTextFont {
12+
13+
/**
14+
This view uses a plain `ForEach` to list a set of fonts,
15+
of which one can be selected.
16+
17+
Unlike ``RichTextFont/Picker`` the picker renders fonts
18+
correctly on all platforms. However, unlike the regular
19+
SwiftUI `Picker`, it must actively be added & presented.
20+
*/
21+
struct ForEachPicker: View {
22+
23+
/**
24+
Create a font picker.
25+
26+
- Parameters:
27+
- selection: The selected font name.
28+
- selectionTopmost: Whether or not to place the selected font topmost.
29+
- fonts: The fonts to display in the list, by default `all`.
30+
- fontSize: The font size to use in the list items.
31+
- dismissAfterPick: Whether or not to dismiss the picker after a font has been selected, by default `false`.
32+
*/
33+
public init(
34+
selection: Binding<FontName>,
35+
selectionTopmost: Bool = true,
36+
fonts: [Font] = .all,
37+
fontSize: CGFloat = 20,
38+
dismissAfterPick: Bool = false
39+
) {
40+
self._selection = selection
41+
self.fonts = fonts
42+
self.fontSize = fontSize
43+
self.dismissAfterPick = dismissAfterPick
44+
if selectionTopmost {
45+
self.fonts = self.fonts.moveTopmost(selection.wrappedValue)
46+
}
47+
}
48+
49+
public typealias Font = RichTextFont.PickerFont
50+
public typealias FontName = String
51+
52+
private var fonts: [Font]
53+
private let fontSize: CGFloat
54+
private let dismissAfterPick: Bool
55+
56+
@Binding
57+
private var selection: FontName
58+
59+
public var body: some View {
60+
let font = Binding(
61+
get: { Font(fontName: selection) },
62+
set: { selection = $0.fontName }
63+
)
64+
65+
RichTextKit.ForEachPicker(
66+
items: fonts,
67+
selection: font,
68+
dismissAfterPick: dismissAfterPick
69+
) { font, isSelected in
70+
RichTextFont.PickerItem(
71+
font: font,
72+
fontSize: fontSize,
73+
isSelected: isSelected
74+
)
75+
}
76+
}
77+
}
78+
}
79+
80+
struct RichTextFont_ForEachPicker_Previews: PreviewProvider {
81+
82+
struct Preview: View {
83+
84+
@State
85+
private var selection = ""
86+
87+
var body: some View {
88+
NavigationView {
89+
List {
90+
RichTextFont.ForEachPicker(
91+
selection: $selection,
92+
selectionTopmost: false
93+
)
94+
}
95+
.withTitle("Pick a font")
96+
}
97+
}
98+
}
99+
100+
static var previews: some View {
101+
Preview()
102+
}
103+
}
104+
105+
private extension View {
106+
107+
@ViewBuilder
108+
func withTitle(_ title: String) -> some View {
109+
#if iOS || os(tvOS) || os(watchOS) || os(visionOS)
110+
self.navigationBarTitle(title)
111+
#else
112+
self
113+
#endif
114+
}
115+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
//
2+
// RichTextFont+ListPicker.swift
3+
// RichTextKit
4+
//
5+
// Created by Daniel Saidi on 2022-06-01.
6+
// Copyright © 2022-2023 Daniel Saidi. All rights reserved.
7+
//
8+
9+
import SwiftUI
10+
11+
public extension RichTextFont {
12+
13+
/**
14+
This view uses a `List` to list a set of fonts of which
15+
one can be selected.
16+
17+
Unlike ``RichTextFont/Picker`` the picker renders fonts
18+
correctly on all platforms. However, unlike the regular
19+
SwiftUI `Picker`, it must actively be added & presented.
20+
*/
21+
struct ListPicker: View {
22+
23+
/**
24+
Create a font picker.
25+
26+
- Parameters:
27+
- selection: The selected font name.
28+
- selectionTopmost: Whether or not to place the selected font topmost.
29+
- fonts: The fonts to display in the list, by default `all`.
30+
- fontSize: The font size to use in the list items.
31+
- dismissAfterPick: Whether or not to dismiss the picker after a font has been selected, by default `true`.
32+
*/
33+
public init(
34+
selection: Binding<FontName>,
35+
selectionTopmost: Bool = false,
36+
fonts: [Font]? = nil,
37+
fontSize: CGFloat = 20,
38+
dismissAfterPick: Bool = true
39+
) {
40+
self._selection = selection
41+
self.fonts = fonts ?? .all
42+
self.fontSize = fontSize
43+
self.dismissAfterPick = dismissAfterPick
44+
if selectionTopmost {
45+
self.fonts = self.fonts.moveTopmost(selection.wrappedValue)
46+
}
47+
}
48+
49+
public typealias Font = RichTextFont.PickerFont
50+
public typealias FontName = String
51+
52+
private var fonts: [Font]
53+
private let fontSize: CGFloat
54+
private let dismissAfterPick: Bool
55+
56+
@Binding
57+
private var selection: FontName
58+
59+
public var body: some View {
60+
let font = Binding(
61+
get: { Font(fontName: selection) },
62+
set: { selection = $0.fontName }
63+
)
64+
65+
RichTextKit.ListPicker(
66+
items: fonts,
67+
selection: font,
68+
dismissAfterPick: dismissAfterPick
69+
) { font, isSelected in
70+
RichTextFont.PickerItem(
71+
font: font,
72+
fontSize: fontSize,
73+
isSelected: isSelected
74+
)
75+
}
76+
}
77+
}
78+
}
79+
80+
struct RichTextFont_ListPicker_Previews: PreviewProvider {
81+
82+
struct Preview: View {
83+
84+
@State private var font = ""
85+
86+
var body: some View {
87+
NavigationView {
88+
RichTextFont.ListPicker(
89+
selection: $font
90+
)
91+
.withTitle("Pick a font")
92+
}
93+
}
94+
}
95+
96+
static var previews: some View {
97+
Preview()
98+
}
99+
}
100+
101+
private extension View {
102+
103+
@ViewBuilder
104+
func withTitle(_ title: String) -> some View {
105+
#if iOS || os(tvOS) || os(watchOS) || os(visionOS)
106+
self.navigationBarTitle(title)
107+
#else
108+
self
109+
#endif
110+
}
111+
}

0 commit comments

Comments
 (0)