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 2 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 @@ -17,10 +17,11 @@ import AppKit
public extension RichTextViewComponent {

/// Get the text alignment.
var richTextAlignment: RichTextAlignment? {
DominikBucher12 marked this conversation as resolved.
Show resolved Hide resolved
guard let style = richTextParagraphStyle else { return nil }
return RichTextAlignment(style.alignment)
}
// var richTextAlignment: RichTextAlignment?
// {
// guard let style = richTextParagraphStyle else { return nil }
// return RichTextAlignment(style.alignment)
// }
DominikBucher12 marked this conversation as resolved.
Show resolved Hide resolved

/// Set the text alignment.
func setRichTextAlignment(_ alignment: RichTextAlignment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,29 @@ 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 }
defer { richTextAlignment = RichTextAlignment(style.alignment) }

let range: NSRange
if multipleSelectedLines() {
range = safeRange(for: selectedRange)
} else {
range = lineRange(for: selectedRange)
}
guard range.length > 0 else {
setRichTextAttribute(.paragraphStyle, to: style)
return
}
#if os(watchOS)
setRichTextAttribute(.paragraphStyle, to: style, at: range)
#else
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 set }

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

Expand Down
6 changes: 6 additions & 0 deletions Sources/RichTextKit/RichTextView_UIKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ open class RichTextView: UITextView, RichTextViewComponent {
/// Keeps track of the data format used by the view.
private var richTextDataFormat: RichTextDataFormat = .archivedData

/// Get the text alignment.
public var richTextAlignment: RichTextAlignment?
// {
// guard let style = richTextParagraphStyle else { return nil }
// return RichTextAlignment(style.alignment)
// }
DominikBucher12 marked this conversation as resolved.
Show resolved Hide resolved
// MARK: - Overrides

/**
Expand Down
Loading