Skip to content

Commit 0fafbdd

Browse files
committed
Add new RichTextPickerValue protocol and let alignment values implement it
1 parent 3e8d8a5 commit 0fafbdd

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

Sources/RichTextKit/Alignment/RichTextAlignment.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import SwiftUI
1212
This enum defines supported rich text alignments, like left,
1313
right, center, and justified.
1414
*/
15-
public enum RichTextAlignment: String, CaseIterable, Codable, Equatable, Identifiable {
15+
public enum RichTextAlignment: String, CaseIterable, Codable, Equatable, Identifiable, RichTextLabelValue {
1616

1717
/**
1818
Initialize a rich text alignment with a native alignment.
@@ -73,6 +73,8 @@ public extension RichTextAlignment {
7373
}
7474
}
7575

76+
extension NSTextAlignment: RichTextLabelValue {}
77+
7678
public extension NSTextAlignment {
7779

7880
/// The standard icon to use for the alignment.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// RichTextLabelValue.swift
3+
// RichTextKit
4+
//
5+
// Created by Daniel Saidi on 2024-01-22.
6+
// Copyright © 2024 Daniel Saidi. All rights reserved.
7+
//
8+
9+
import SwiftUI
10+
11+
public protocol RichTextLabelValue: Hashable {
12+
13+
/// The value icon.
14+
var icon: Image { get }
15+
16+
/// The value display title.
17+
var title: String { get }
18+
}
19+
20+
public extension RichTextLabelValue {
21+
22+
/// The standard label to use for the value.
23+
var label: some View {
24+
Label(
25+
title: { Text(title) },
26+
icon: { icon }
27+
)
28+
.tag(self)
29+
.accessibilityLabel(title)
30+
}
31+
}
32+
33+
struct RichTextLabelValue_Previews: PreviewProvider {
34+
35+
struct Preview: View {
36+
37+
@State
38+
private var alignment = RichTextAlignment.left
39+
40+
var body: some View {
41+
List {
42+
Section("RichTextAlignment") {
43+
ForEach(RichTextAlignment.allCases) {
44+
$0.label
45+
}
46+
}
47+
}
48+
}
49+
}
50+
51+
static var previews: some View {
52+
Preview()
53+
}
54+
}

0 commit comments

Comments
 (0)