Skip to content

Commit

Permalink
Fixed issue where cursor moved incorrectly if next to the bullet/numb…
Browse files Browse the repository at this point in the history
…er or at the end of the textview when bullets/numbers were toggled off (#11)

* Fixed issue where cursor moved incorrectly if next to the bullet/number or at the end of the textview when bullets/numbers were toggled off

* Moved the let next to the other

* Removed bad check from if
Improved guard

* Fixed more selection and cursor placement issues when toggling numbered/bulleted lists

* Fixed logic of ranges
  • Loading branch information
brandenr authored and SerenadeX committed May 16, 2016
1 parent b2116e9 commit f0ea1a1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
2 changes: 1 addition & 1 deletion RichTextVC-iOS.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = "RichTextVC-iOS"
s.version = "1.2.1"
s.version = "1.2.2"
s.summary = "A Rich Text ViewController for iOS."

# This description is used to generate tags and improve search results.
Expand Down
8 changes: 4 additions & 4 deletions src/Classes/NSRange+Extras.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ extension NSRange {
}

func containsEntireRange(range: NSRange) -> Bool {
return (location < range.location) && ((range.location - location) + range.length <= length)
return containsBeginningOfRange(range) && containsEndOfRange(range)
}

func containedInRange(range: NSRange) -> Bool {
return range.containsEntireRange(self)
}

func containsEndOfRange(range: NSRange) -> Bool {
return location < range.endLocation && range.endLocation < endLocation
return length > 0 && location <= range.endLocation && range.endLocation < endLocation
}

func containsBeginningOfRange(range: NSRange) -> Bool {
return range.location < endLocation && endLocation < range.endLocation
return length > 0 && location <= range.location && endLocation >= range.location
}

func comesBeforeRange(range: NSRange) -> Bool {
return endLocation < range.location
return endLocation <= range.location
}

func comesAfterRange(range: NSRange) -> Bool {
Expand Down
32 changes: 13 additions & 19 deletions src/Classes/RichTextViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,19 @@ public class RichTextViewController: UIViewController {
/// - parameter toTextView: The `UITextView` to remove the text from.
private func removeTextFromRange(range: NSRange, fromTextView textView: UITextView) {
let substringLength = (textView.text as NSString).substringWithRange(range).length

let initialRange = textView.selectedRange

textView.textStorage.beginEditing()
textView.textStorage.replaceCharactersInRange(range, withAttributedString: NSAttributedString(string: ""))
textView.textStorage.endEditing()

if range.comesBeforeRange(textView.selectedRange) {
textView.selectedRange.location -= substringLength
textView.selectedRange.location -= (substringLength - (initialRange.location - textView.selectedRange.location))
textView.selectedRange.length = initialRange.length
} else if range.containedInRange(textView.selectedRange) {
textView.selectedRange.length -= substringLength
} else if range.containsBeginningOfRange(textView.selectedRange) {
let inSelectionRemoved = textView.selectedRange.location - range.location
let outSelectionRemoved = range.length - inSelectionRemoved
textView.selectedRange.location -= outSelectionRemoved
textView.selectedRange.length -= inSelectionRemoved
} else if range.containsEndOfRange(textView.selectedRange) {
textView.selectedRange.length -= textView.selectedRange.endLocation - range.location
textView.selectedRange.length -= (substringLength - (initialRange.length - textView.selectedRange.length))
} else if range.location == textView.selectedRange.location && range.length == textView.selectedRange.length {
textView.selectedRange.length = 0
}

textViewDidChangeSelection(textView)
Expand Down Expand Up @@ -174,8 +171,8 @@ public class RichTextViewController: UIViewController {
var index = textView.selectedRange.location

while index < textView.text.length {
guard let newLineIndex = textView.text.nextIndexOfSubstring("\n", fromIndex: index) where
newLineIndex < textView.selectedRange.endLocation else { break }
guard let newLineIndex = textView.text.nextIndexOfSubstring("\n", fromIndex: index) where newLineIndex < textView.selectedRange.endLocation else { break }

addText("\(newNumber)\(RichTextViewController.numberedListTrailer)", toTextView: textView, atIndex: newLineIndex + 1)
newNumber += 1
index = newLineIndex + 1
Expand Down Expand Up @@ -212,7 +209,6 @@ public class RichTextViewController: UIViewController {
guard let numberedTrailerIndex = string.nextIndexOfSubstring(RichTextViewController.numberedListTrailer, fromIndex: index) else { return nil }

var newLineIndex = string.previousIndexOfSubstring("\n", fromIndex: numberedTrailerIndex) ?? -1

if newLineIndex >= -1 {
newLineIndex += 1
}
Expand Down Expand Up @@ -496,12 +492,10 @@ public class RichTextViewController: UIViewController {
}

private func removeFormattingFromListLeadsInRange(range: NSRange) {
guard let listHeadRegex = try? NSRegularExpression(pattern: "^(([0-9]+\\.\\u00A0)|(\\u2022\\u00A0)).*$", options: .AnchorsMatchLines),
regularFont = regularFont where
range.length > 0
else {
print("Failed to remove formatting")
return
guard let regularFont = regularFont else { return }
guard range.length > 0, let listHeadRegex = try? NSRegularExpression(pattern: "^(([0-9]+\\.\\u00A0)|(\\u2022\\u00A0)).*$", options: .AnchorsMatchLines) else {
print("Failed to remove formatting")
return
}

listHeadRegex.matchesInString(textView.text, options: [], range: range).forEach { match in
Expand Down

0 comments on commit f0ea1a1

Please sign in to comment.