Skip to content

Commit 5c5f1ab

Browse files
committed
feat: add textColor
1 parent 488ff1a commit 5c5f1ab

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

Example/Example.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
7D21058C2C0EAC5D004E0E77 /* TextColorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D21058B2C0EAC5D004E0E77 /* TextColorView.swift */; };
1011
7D4E4F192BB2E5AF003B3098 /* PlaceholderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D4E4F182BB2E5AF003B3098 /* PlaceholderView.swift */; };
1112
7D4E4F5F2BB3E4A7003B3098 /* OnChangeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D4E4F5E2BB3E4A7003B3098 /* OnChangeView.swift */; };
1213
7DAB5BE42BB0ABA200B5146A /* ExampleApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DAB5BE32BB0ABA200B5146A /* ExampleApp.swift */; };
@@ -20,6 +21,7 @@
2021
/* End PBXBuildFile section */
2122

2223
/* Begin PBXFileReference section */
24+
7D21058B2C0EAC5D004E0E77 /* TextColorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextColorView.swift; sourceTree = "<group>"; };
2325
7D4E4F182BB2E5AF003B3098 /* PlaceholderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlaceholderView.swift; sourceTree = "<group>"; };
2426
7D4E4F5E2BB3E4A7003B3098 /* OnChangeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnChangeView.swift; sourceTree = "<group>"; };
2527
7DAB5BE02BB0ABA200B5146A /* Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Example.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -100,6 +102,7 @@
100102
7DC471092BB1FCAF00CE7718 /* MutableAttributedStringExampleView.swift */,
101103
7D4E4F182BB2E5AF003B3098 /* PlaceholderView.swift */,
102104
7D4E4F5E2BB3E4A7003B3098 /* OnChangeView.swift */,
105+
7D21058B2C0EAC5D004E0E77 /* TextColorView.swift */,
103106
);
104107
path = Examples;
105108
sourceTree = "<group>";
@@ -182,6 +185,7 @@
182185
7D4E4F192BB2E5AF003B3098 /* PlaceholderView.swift in Sources */,
183186
7DAB5BFA2BB16B0D00B5146A /* FontExampleView.swift in Sources */,
184187
7D4E4F5F2BB3E4A7003B3098 /* OnChangeView.swift in Sources */,
188+
7D21058C2C0EAC5D004E0E77 /* TextColorView.swift in Sources */,
185189
7DC4710A2BB1FCAF00CE7718 /* MutableAttributedStringExampleView.swift in Sources */,
186190
7DAB5BF82BB0AF9000B5146A /* ExampleView.swift in Sources */,
187191
);

Example/Example/ContentView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ struct SideBar: Identifiable, Hashable, Equatable {
2222

2323
struct ContentView: View {
2424
@State private var examples = [
25+
SideBar(name: "Text Color Test", view: AnyView(TextColorView())),
2526
SideBar(name: "Change Event Test", view: AnyView(OnChangeView())),
2627
SideBar(name: "Placeholder Example", view: AnyView(PlaceholderView())),
2728
SideBar(name: "Mutable Attributed String", view: AnyView(MutableAttributedStringExampleView())),
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// TextColorView.swift
3+
// Example
4+
//
5+
// Created by 王楚江 on 2024/6/4.
6+
//
7+
8+
import SwiftUI
9+
import TextEditorPlus
10+
11+
struct TextColorView: View {
12+
@State var text = """
13+
Hello World
14+
"""
15+
@State var color: Color = .red
16+
var body: some View {
17+
VStack(alignment: .leading, spacing: 0) {
18+
19+
ColorPicker(selection: $color, supportsOpacity: true, label: {
20+
21+
})
22+
.controlSize(.small)
23+
.labelsHidden()
24+
25+
TextEditorPlus(text: $text)
26+
.textSetting(NSColor(color), for: .textColor)
27+
}
28+
.onChange(of: text, initial: true, { old, val in
29+
print("val: \(val)")
30+
})
31+
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
32+
}
33+
}
34+
35+
#Preview {
36+
TextColorView()
37+
}

Sources/TextEditorPlus/Settings/TextSetting.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ fileprivate struct TextViewBackgroundColor: EnvironmentKey {
2828
fileprivate struct TextViewPlaceholderString: EnvironmentKey {
2929
static var defaultValue: String?
3030
}
31+
fileprivate struct TextViewTextColor: EnvironmentKey {
32+
static var defaultValue: ViewColor? = ViewColor.textColor
33+
}
3134

3235
fileprivate struct TextViewAttributedString: EnvironmentKey {
3336
static var defaultValue: (NSMutableAttributedString) -> NSMutableAttributedString? = { _ in nil }
@@ -56,6 +59,11 @@ extension EnvironmentValues {
5659
get { self[TextViewPlaceholderString.self] }
5760
set { self[TextViewPlaceholderString.self] = newValue }
5861
}
62+
/// Text Color
63+
var textViewTextColor: ViewColor? {
64+
get { self[TextViewTextColor.self] }
65+
set { self[TextViewTextColor.self] = newValue }
66+
}
5967
}
6068

6169
public enum TextViewComponent {
@@ -67,6 +75,8 @@ public enum TextViewComponent {
6775
case backgroundColor
6876
/// Set editor placeholder
6977
case placeholderString
78+
/// Set editor text color
79+
case textColor
7080
}
7181

7282
@available(iOS 13.0, macOS 10.15, *)
@@ -106,6 +116,8 @@ public extension View {
106116
environment(\.textViewInsetPadding, value as! CGFloat)
107117
case .backgroundColor:
108118
environment(\.textViewBackgroundColor, value as! ViewColor?)
119+
case .textColor:
120+
environment(\.textViewTextColor, value as! ViewColor?)
109121
case .placeholderString:
110122
environment(\.textViewPlaceholderString, value as! String?)
111123
}

Sources/TextEditorPlus/TextEditorPlus.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public struct TextEditorPlus: ViewRepresentable {
7171
@Environment(\.textViewAttributedString) private var textViewAttributedString
7272
@Environment(\.textViewBackgroundColor) private var textViewBackgroundColor
7373
@Environment(\.textViewPlaceholderString) private var placeholderString
74+
@Environment(\.textViewTextColor) private var textColor
7475
@Environment(\.colorScheme) var colorScheme
7576
@Font var font: FontHelper = .systemFont(ofSize: 14, weight: .regular)
7677

@@ -166,7 +167,7 @@ public struct TextEditorPlus: ViewRepresentable {
166167
textView.isVerticallyResizable = true
167168
textView.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
168169
textView.minSize = NSSize(width: 0, height: contentSize.height)
169-
textView.textColor = NSColor.labelColor
170+
textView.textColor = textColor
170171
textView.allowsUndo = true
171172
textView.delegate = context.coordinator // 设置代理
172173
textView.font = font
@@ -204,6 +205,7 @@ public struct TextEditorPlus: ViewRepresentable {
204205
textView.placeholderInsetPadding = insetPadding
205206
textView.textContainerInset = NSSize(width: 0, height: insetPadding)
206207
textView.textContainer?.lineFragmentPadding = insetPadding
208+
textView.textColor = textColor
207209

208210
let attributedString = NSMutableAttributedString(string: text)
209211
let nsColor = colorScheme == .dark ? NSColor.white : NSColor.black

0 commit comments

Comments
 (0)