Skip to content

Commit 9e4cbdf

Browse files
authored
Support font family (#58)
1 parent 55892a4 commit 9e4cbdf

28 files changed

+896
-117
lines changed

RichEditorDemo/RichEditorDemo.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@
305305
"INFOPLIST_KEY_UIStatusBarStyle[sdk=iphonesimulator*]" = UIStatusBarStyleDefault;
306306
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
307307
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
308-
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
308+
IPHONEOS_DEPLOYMENT_TARGET = 17.0;
309309
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
310310
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
311311
MACOSX_DEPLOYMENT_TARGET = 14.0;
@@ -344,7 +344,7 @@
344344
"INFOPLIST_KEY_UIStatusBarStyle[sdk=iphonesimulator*]" = UIStatusBarStyleDefault;
345345
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
346346
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
347-
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
347+
IPHONEOS_DEPLOYMENT_TARGET = 17.0;
348348
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
349349
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
350350
MACOSX_DEPLOYMENT_TARGET = 14.0;

RichEditorDemo/RichEditorDemo/ContentView.swift

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@
55
// Created by Divyesh Vekariya on 11/10/23.
66
//
77

8-
import SwiftUI
98
import RichEditorSwiftUI
9+
import SwiftUI
1010

1111
struct ContentView: View {
1212
@Environment(\.colorScheme) var colorScheme
1313

1414
@ObservedObject var state: RichEditorState
15+
@State private var isInspectorPresented = false
1516

1617
init(state: RichEditorState? = nil) {
1718
if let state {
1819
self.state = state
1920
} else {
20-
if let richText = readJSONFromFile(fileName: "Sample_json",
21-
type: RichText.self) {
21+
if let richText = readJSONFromFile(
22+
fileName: "Sample_json",
23+
type: RichText.self)
24+
{
2225
self.state = .init(richText: richText)
2326
} else {
2427
self.state = .init(input: "Hello World!")
@@ -29,9 +32,9 @@ struct ContentView: View {
2932
var body: some View {
3033
NavigationStack {
3134
VStack {
32-
#if os(iOS) || os(macOS) || os(visionOS)
33-
EditorToolBarView(state: state)
34-
#endif
35+
#if os(macOS)
36+
RichTextFormat.Toolbar(context: state)
37+
#endif
3538

3639
RichTextEditor(
3740
context: _state,
@@ -40,22 +43,39 @@ struct ContentView: View {
4043
}
4144
)
4245
.cornerRadius(10)
46+
47+
#if os(iOS)
48+
RichTextKeyboardToolbar(
49+
context: state,
50+
leadingButtons: { $0 },
51+
trailingButtons: { $0 },
52+
formatSheet: { $0 }
53+
)
54+
#endif
55+
}
56+
.inspector(isPresented: $isInspectorPresented) {
57+
RichTextFormat.Sidebar(context: state)
58+
#if os(macOS)
59+
.inspectorColumnWidth(min: 200, ideal: 200, max: 315)
60+
#endif
4361
}
4462
.padding(10)
4563
.toolbar {
46-
ToolbarItem() {
47-
Button(action: {
48-
print("Exported JSON == \(state.output())")
49-
}, label: {
50-
Image(systemName: "printer.inverse")
51-
.padding()
52-
})
64+
ToolbarItem(placement: .automatic) {
65+
Button(
66+
action: {
67+
print("Exported JSON == \(state.output())")
68+
},
69+
label: {
70+
Image(systemName: "printer.inverse")
71+
.padding()
72+
})
5373
}
5474
}
5575
.background(colorScheme == .dark ? .black : .gray.opacity(0.07))
5676
.navigationTitle("Rich Editor")
5777
#if os(iOS)
58-
.navigationBarTitleDisplayMode(.inline)
78+
.navigationBarTitleDisplayMode(.inline)
5979
#endif
6080
}
6181
}

Sources/RichEditorSwiftUI/Actions/RichTextAction.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public enum RichTextAction: Identifiable, Equatable {
7878
case undoLatestChange
7979

8080
/// Set HeaderStyle.
81-
case setHeaderStyle(_ style: RichTextStyle)
81+
case setHeaderStyle(_ style: RichTextSpanStyle)
8282
}
8383

8484
public extension RichTextAction {

Sources/RichEditorSwiftUI/Attributes/RichTextAttributeReader.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ public extension RichTextAttributeReader {
5656
public extension RichTextAttributeReader {
5757

5858
/// Get the text styles at a certain range.
59-
func richTextStyles(at range: NSRange) -> [RichTextStyle] {
59+
func richTextStyles(at range: NSRange) -> [RichTextSpanStyle] {
6060
let attributes = richTextAttributes(at: range)
6161
let traits = richTextFont(at: range)?.fontDescriptor.symbolicTraits
6262
var styles = traits?.enabledRichTextStyles ?? []
6363
if attributes.isStrikethrough { styles.append(.strikethrough) }
6464
if attributes.isUnderlined { styles.append(.underline) }
65-
return styles
65+
return styles.map({ $0.richTextSpanStyle })
6666
}
6767
}

Sources/RichEditorSwiftUI/Attributes/RichTextAttributeWriter+Style.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public extension NSMutableAttributedString {
2121
- range: The range to affect, by default the entire text.
2222
*/
2323
func setRichTextStyle(
24-
_ style: RichTextStyle,
24+
_ style: RichTextSpanStyle,
2525
to newValue: Bool,
2626
at range: NSRange? = nil
2727
) {
@@ -43,8 +43,8 @@ public extension NSMutableAttributedString {
4343
let shouldRemove = !newValue && styles.hasStyle(style)
4444
guard shouldAdd || shouldRemove || style.isHeaderStyle else { return }
4545
var descriptor = font.fontDescriptor
46-
if !style.isDefault && !style.isHeaderStyle {
47-
descriptor = descriptor.byTogglingStyle(style)
46+
if let richTextStyle = style.richTextStyle, !style.isDefault && !style.isHeaderStyle {
47+
descriptor = descriptor.byTogglingStyle(richTextStyle)
4848
}
4949
let newFont: FontRepresentable? = FontRepresentable(
5050
descriptor: descriptor,
@@ -56,7 +56,7 @@ public extension NSMutableAttributedString {
5656
/**
5757
This will reset font size before multiplying new size
5858
*/
59-
private func byTogglingFontSizeFor(style: TextSpanStyle, font: FontRepresentable, shouldAdd: Bool) -> CGFloat {
59+
private func byTogglingFontSizeFor(style: RichTextSpanStyle, font: FontRepresentable, shouldAdd: Bool) -> CGFloat {
6060
guard style.isHeaderStyle || style.isDefault else { return font.pointSize }
6161

6262
let cleanFont = style.getFontAfterRemovingStyle(font: font)

Sources/RichEditorSwiftUI/BaseFoundation/RichTextCoordinator+Actions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ extension RichTextCoordinator {
183183

184184
func setStyle(_ style: RichTextStyle, to newValue: Bool) {
185185
let hasStyle = textView.richTextStyles.hasStyle(style)
186-
if newValue == hasStyle { return }
186+
guard newValue != hasStyle else { return }
187187
textView.setRichTextStyle(style, to: newValue)
188188
}
189189
}

Sources/RichEditorSwiftUI/Components/RichTextViewComponent+Styles.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ public extension RichTextViewComponent {
4141
setRichTextAttribute(.underlineStyle, to: value)
4242
case .strikethrough:
4343
setRichTextAttribute(.strikethroughStyle, to: value)
44-
default:
45-
return
4644
}
4745
}
4846

Sources/RichEditorSwiftUI/UI/EditorToolBar/HeaderType.swift renamed to Sources/RichEditorSwiftUI/Data/Models/HeaderType.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public enum HeaderType: Int, CaseIterable, Codable {
3535
}
3636
}
3737

38-
func getTextSpanStyle() -> TextSpanStyle {
38+
func getTextSpanStyle() -> RichTextSpanStyle {
3939
switch self {
4040
case .default: return .default
4141
case .h1: return .h1

Sources/RichEditorSwiftUI/Data/Models/RichAttributes.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ extension RichAttributes {
146146
)
147147
}
148148

149-
public func copy(with style: TextSpanStyle, byAdding: Bool = true) -> RichAttributes {
149+
public func copy(with style: RichTextSpanStyle, byAdding: Bool = true) -> RichAttributes {
150150
return copy(with: [style], byAdding: byAdding)
151151
}
152152

153-
public func copy(with styles: [TextSpanStyle], byAdding: Bool = true) -> RichAttributes {
153+
public func copy(with styles: [RichTextSpanStyle], byAdding: Bool = true) -> RichAttributes {
154154
let att = getRichAttributesFor(styles: styles)
155155
return RichAttributes(
156156
bold: (att.bold != nil ? (byAdding ? att.bold! : nil) : self.bold),
@@ -169,8 +169,8 @@ extension RichAttributes {
169169
}
170170

171171
extension RichAttributes {
172-
public func styles() -> [TextSpanStyle] {
173-
var styles: [TextSpanStyle] = []
172+
public func styles() -> [RichTextSpanStyle] {
173+
var styles: [RichTextSpanStyle] = []
174174
if let bold = bold, bold {
175175
styles.append(.bold)
176176
}
@@ -204,8 +204,8 @@ extension RichAttributes {
204204
return styles
205205
}
206206

207-
public func stylesSet() -> Set<TextSpanStyle> {
208-
var styles: Set<TextSpanStyle> = []
207+
public func stylesSet() -> Set<RichTextSpanStyle> {
208+
var styles: Set<RichTextSpanStyle> = []
209209
if let bold = bold, bold {
210210
styles.insert(.bold)
211211
}
@@ -241,7 +241,7 @@ extension RichAttributes {
241241
}
242242

243243
extension RichAttributes {
244-
public func hasStyle(style: RichTextStyle) -> Bool {
244+
public func hasStyle(style: RichTextSpanStyle) -> Bool {
245245
switch style {
246246
case .default:
247247
return true
@@ -279,11 +279,11 @@ extension RichAttributes {
279279
}
280280
}
281281

282-
internal func getRichAttributesFor(style: RichTextStyle) -> RichAttributes {
282+
internal func getRichAttributesFor(style: RichTextSpanStyle) -> RichAttributes {
283283
return getRichAttributesFor(styles: [style])
284284
}
285285

286-
internal func getRichAttributesFor(styles: [RichTextStyle]) -> RichAttributes {
286+
internal func getRichAttributesFor(styles: [RichTextSpanStyle]) -> RichAttributes {
287287
guard !styles.isEmpty else { return RichAttributes() }
288288
var bold: Bool? = nil
289289
var italic: Bool? = nil

Sources/RichEditorSwiftUI/Format/RichTextFormat+Sheet.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ public extension RichTextFormat {
5959
public var body: some View {
6060
NavigationView {
6161
VStack(spacing: 0) {
62-
// RichTextFont.ListPicker(
63-
// selection: $context.fontName
64-
// )
65-
// Divider()
62+
RichTextFont.ListPicker(
63+
selection: $context.fontName
64+
)
65+
Divider()
6666
RichTextFormat.Toolbar(
6767
context: context
6868
)

0 commit comments

Comments
 (0)