Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Fix alignment 💯 #147

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ import AppKit
#endif

public extension RichTextViewComponent {

/// Get the text alignment.
var richTextAlignment: RichTextAlignment? {
DominikBucher12 marked this conversation as resolved.
Show resolved Hide resolved
public 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 @@ -19,8 +19,11 @@ import AppKit
public extension RichTextViewComponent {

/// Get the paragraph style.
var richTextParagraphStyle: NSMutableParagraphStyle? {
richTextAttribute(.paragraphStyle)
/// We need to get paragraph style from text storage because attributed string
/// doesn't inherit those attributes from storage. It simply doesnt mirror.
/// Appears like a bug on Apples side.
public var richTextParagraphStyle: NSMutableParagraphStyle? {
textStorageWrapper?.richTextParagraphStyle(at: selectedRange)
}

/// Set the paragraph style.
Expand All @@ -29,12 +32,25 @@ public extension RichTextViewComponent {
/// selected paragraphs. If many paragraphs are selected,
/// it will only affect the first one.
func setRichTextParagraphStyle(_ style: NSParagraphStyle) {
let range = lineRange(for: selectedRange)
guard range.length > 0 else { return }
let range: NSRange
if multipleSelectedLines() {
range = safeRange(for: selectedRange)
} else {
range = lineRange(for: selectedRange)
}

#if os(watchOS)
setRichTextAttribute(.paragraphStyle, to: style, at: range)
#else
setRichTextAttribute(.paragraphStyle, to: style)
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
}
}
3 changes: 3 additions & 0 deletions Sources/RichTextKit/Component/RichTextViewComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public protocol RichTextViewComponent: AnyObject,
/// The text view's mutable attributed string, if any.
var mutableAttributedString: NSMutableAttributedString? { get }

/// Get the text alignment.
var richTextAlignment: RichTextAlignment? { get }

/// The spacing between the text view's edge and its text.
var textContentInset: CGSize { get set }

Expand Down
15 changes: 14 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,18 @@ extension RichTextCoordinator {
textView.setRichTextAttributes(attributes)
#endif
}

private func fetchAttributeAlignment() -> RichTextAlignment? {
#if macOS
guard let style = textView.richTextParagraphStyle else { return nil }
return RichTextAlignment(style.alignment)
#else
// For iOS, this is 100% behavior.
if let typingAttributes = textView.typingAttributes[.paragraphStyle] as? NSParagraphStyle {
return RichTextAlignment(typingAttributes.alignment)
}
return nil
#endif
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,12 @@ final class RichTextCoordinatorIntegrationTests: XCTestCase {

textView.simulateTyping(of: firstTypingPart)

let attributes = textView.richTextAttributes

textContext.selectRange(NSRange(location: Self.initialAttributedString.length, length: firstTypingPart.count))
XCTAssertEqual(attributes[.font] as? FontRepresentable, FontRepresentable.systemFont(ofSize: 16))
XCTAssertEqual(attributes[.foregroundColor] as? ColorRepresentable, ColorRepresentable.red)
XCTAssertEqual(textView.richTextAttributes[.font] as? FontRepresentable, FontRepresentable.systemFont(ofSize: 16))
XCTAssertEqual(textView.richTextAttributes[.foregroundColor] as? ColorRepresentable, ColorRepresentable.red)

textView.setRichTextStyle(.strikethrough, to: true)
XCTAssertEqual(attributes[.strikethroughStyle] as? Int, 1)
XCTAssertEqual(textView.richTextAttributes[.strikethroughStyle] as? Int, 1)
}
}
#endif
Loading