Skip to content

Commit

Permalink
Merge pull request #131 from danielsaidi/bugfix/paragraphs
Browse files Browse the repository at this point in the history
Fix rich text alignment bugs
  • Loading branch information
danielsaidi authored Jan 24, 2024
2 parents b410667 + 9a8b7ac commit 2f2acee
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,23 @@ public extension RichTextAttributeWriter {
_ alignment: RichTextAlignment,
at range: NSRange
) {
let text = richText.string

// Text view has selected text
if range.length > 0 {
return setAlignment(alignment, at: range)
guard range.length == 0 else {
setAlignment(alignment, at: range)
return
}

let text = richText.string

// The cursor is at the beginning of the text
if range.location == 0 {
return setAlignment(alignment, atIndex: 0)
}

// The cursor is immediately after a newline
if let char = text.character(at: range.location - 1), char.isNewLineSeparator {
return setAlignment(alignment, atIndex: range.location)
// We dont want to set attributed string to previous line,
// isNewLineSeparator is unfortunately omitted by API.
return
}

// The cursor is immediately before a newline
Expand All @@ -55,6 +57,7 @@ public extension RichTextAttributeWriter {
let location = UInt(range.location)
let index = text.findIndexOfCurrentParagraph(from: location)
return setAlignment(alignment, atIndex: index)

}
}

Expand Down Expand Up @@ -85,7 +88,7 @@ private extension RichTextAttributeWriter {
_ alignment: RichTextAlignment,
atIndex index: Int
) {
guard let text = mutableRichText else { return }
guard let text = mutableRichText, !text.string.isEmpty else { return }
let range = NSRange(location: index, length: 1)
let safeRange = safeRange(for: range, isAttributeOperation: true)
var attributes = text.attributes(at: safeRange.location, effectiveRange: nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ public extension RichTextViewComponent {
_ alignment: RichTextAlignment
) {
if richTextAlignment == alignment { return }
if !hasTrimmedText {
if !hasTrimmedText || !hasSelectedRange {
let style = NSMutableParagraphStyle()
style.alignment = alignment.nativeAlignment
var attributes = richTextAttributes
attributes[.paragraphStyle] = style
typingAttributes = attributes
setRichTextAlignment(alignment, at: selectedRange)
} else {
setRichTextAlignment(alignment, at: selectedRange)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/RichTextKit/Extensions/String+Paragraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public extension String {
after the newline char, not the newline itself.
*/

func findIndexOfNextParagraphOrEndofCurrent(from location: UInt) -> UInt {
func findIndexOfNextParagraphOrEndOfCurrent(from location: UInt) -> UInt {
var index = location
repeat {
guard let char = character(at: index) else { break }
Expand All @@ -80,7 +80,7 @@ public extension String {
func findLengthOfCurrentParagraph(from location: UInt) -> Int {
if isEmpty { return 0 }
let startIndex = findIndexOfCurrentParagraph(from: location)
let endIndex = findIndexOfNextParagraphOrEndofCurrent(from: location)
let endIndex = findIndexOfNextParagraphOrEndOfCurrent(from: location)
return Int(endIndex)-Int(startIndex)
}

Expand Down

0 comments on commit 2f2acee

Please sign in to comment.