Skip to content

Commit

Permalink
Set correct alignment to context
Browse files Browse the repository at this point in the history
  • Loading branch information
DominikBucher12 committed Feb 21, 2024
1 parent c7d0e15 commit 8aef3e2
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ import AppKit

public extension RichTextViewComponent {

/// Get the text alignment.
// var richTextAlignment: RichTextAlignment?
// {
// guard let style = richTextParagraphStyle else { return nil }
// return RichTextAlignment(style.alignment)
// }

/// Set the text alignment.
func setRichTextAlignment(_ alignment: RichTextAlignment) {
if richTextAlignment == alignment { return }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public extension RichTextViewComponent {
/// selected paragraphs. If many paragraphs are selected,
/// it will only affect the first one.
func setRichTextParagraphStyle(_ style: NSParagraphStyle) {
defer { richTextAlignment = RichTextAlignment(style.alignment) }
defer { richTextAlignment = RichTextAlignment(style.alignment) }

let range: NSRange
if multipleSelectedLines() {
range = safeRange(for: selectedRange)
Expand All @@ -47,11 +47,11 @@ public extension RichTextViewComponent {
textStorageWrapper?.addAttribute(.paragraphStyle, value: style, range: range)
#endif
}

private func multipleSelectedLines() -> Bool {
guard let selectedText = textStorageWrapper?.attributedSubstring(from: selectedRange) else { return false }
let selectedLines = selectedText.string.components(separatedBy: .newlines)

return selectedLines.count > 1
}
}
7 changes: 6 additions & 1 deletion Sources/RichTextKit/Coordinator/RichTextCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ extension RichTextCoordinator {
sync(&context.fontSize, with: font.pointSize)
sync(&context.isEditingText, with: textView.isFirstResponder)
// sync(&context.lineSpacing, with: textView.richTextLineSpacing ?? 10.0) TODO: Not done yet
sync(&context.textAlignment, with: textView.richTextAlignment ?? .left)
sync(&context.textAlignment, with: fetchAttributeAlignment() ?? .left)

RichTextColor.allCases.forEach {
if let color = textView.richTextColor($0) {
Expand Down Expand Up @@ -232,5 +232,10 @@ extension RichTextCoordinator {
textView.setRichTextAttributes(attributes)
#endif
}

private func fetchAttributeAlignment() -> RichTextAlignment? {
guard let style = textView.richTextParagraphStyle else { return nil }
return RichTextAlignment(style.alignment)
}
}
#endif
2 changes: 1 addition & 1 deletion Sources/RichTextKit/RichTextView_AppKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ open class RichTextView: NSTextView, RichTextViewComponent {

/// Get the text alignment.
public var richTextAlignment: RichTextAlignment?

// MARK: - Overrides

/// Paste the current pasteboard content into the view.
Expand Down
5 changes: 1 addition & 4 deletions Sources/RichTextKit/RichTextView_UIKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ open class RichTextView: UITextView, RichTextViewComponent {

/// Get the text alignment.
public var richTextAlignment: RichTextAlignment?
// {
// guard let style = richTextParagraphStyle else { return nil }
// return RichTextAlignment(style.alignment)
// }

// MARK: - Overrides

/**
Expand Down

0 comments on commit 8aef3e2

Please sign in to comment.