Skip to content

Commit

Permalink
Fixed:
Browse files Browse the repository at this point in the history
1. Using non-default textContainerInset value, initial text is misplaced #19
2. Top and bottom insets sometimes appear to be incorrect. #18
  • Loading branch information
KennethTsang committed Nov 1, 2017
1 parent 723a11b commit 994aa53
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
2 changes: 1 addition & 1 deletion GrowingTextView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = "GrowingTextView"
s.version = "0.5.2"
s.version = "0.5.3"
s.summary = "UITextView on Swift 3 and Swift 4. Support auto growing, placeholder and length limit."

# This description is used to generate tags and improve search results.
Expand Down
36 changes: 14 additions & 22 deletions Pod/Classes/GrowingTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ open class GrowingTextView: UITextView {
return CGSize(width: UIViewNoIntrinsicMetric, height: 30)
}

func associateConstraints() {
private func associateConstraints() {
// iterate through all text view's constraints and identify
// height,from: https://github.com/legranddamien/MBAutoGrowingTextView
for constraint in constraints {
Expand All @@ -94,7 +94,7 @@ open class GrowingTextView: UITextView {
layoutIfNeeded()
}

private var isGrowing = false
private var shouldScrollAfterHeightChanged = false
override open func layoutSubviews() {
super.layoutSubviews()

Expand All @@ -118,26 +118,21 @@ open class GrowingTextView: UITextView {
}

// Update height constraint if needed
let originHeight = heightConstraint?.constant ?? 0
if height != originHeight {
if height > originHeight { isGrowing = true }
if height != heightConstraint!.constant {
shouldScrollAfterHeightChanged = true
heightConstraint!.constant = height
scrollToCorrectPosition()
if let delegate = delegate as? GrowingTextViewDelegate {
delegate.textViewDidChangeHeight?(self, height: height)
}
} else if shouldScrollAfterHeightChanged {
shouldScrollAfterHeightChanged = false
scrollToCorrectPosition()
}
}

private func scrollToCorrectPosition() {
if self.isFirstResponder {
if self.isGrowing {
// Workaround to for incorrect scroll position on Swift4
self.heightConstraint!.constant += 0.0000001
self.isGrowing = false
} else {
self.scrollRangeToVisible(NSMakeRange(-1, 0)) // Scroll to bottom
}
self.scrollRangeToVisible(NSMakeRange(-1, 0)) // Scroll to bottom
} else {
self.scrollRangeToVisible(NSMakeRange(0, 0)) // Scroll to top
}
Expand Down Expand Up @@ -188,16 +183,13 @@ open class GrowingTextView: UITextView {

// Limit the length of text
@objc func textDidChange(notification: Notification) {
if let notificationObject = notification.object as? GrowingTextView {
if notificationObject === self {
if maxLength > 0 && text.count > maxLength {

let endIndex = text.index(text.startIndex, offsetBy: maxLength)
text = String(text[..<endIndex])
undoManager?.removeAllActions()
}
setNeedsDisplay()
if let sender = notification.object as? GrowingTextView, sender == self {
if maxLength > 0 && text.count > maxLength {
let endIndex = text.index(text.startIndex, offsetBy: maxLength)
text = String(text[..<endIndex])
undoManager?.removeAllActions()
}
setNeedsDisplay()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ it, simply add the following line to your Podfile:
Swift 4<br>

```ruby
pod 'GrowingTextView', '~> 0.5.0'
pod 'GrowingTextView', '~> 0.5.3'
```

Swift 3<br>
Expand Down

0 comments on commit 994aa53

Please sign in to comment.