Skip to content

Instantly share code, notes, and snippets.

@MylesCaley
Last active January 24, 2017 19:57
Show Gist options
  • Save MylesCaley/83360dd3798a5459ab8ea8313478dda3 to your computer and use it in GitHub Desktop.
Save MylesCaley/83360dd3798a5459ab8ea8313478dda3 to your computer and use it in GitHub Desktop.
When text is too small to fit in UITextView
extension UITextView {
func sizeThatWillFitAllText()->CGFloat {
return self.sizeThatFits((self.frame.size)).height
}
}
self.textViewHeightConstraintOutlet.constant = desiredTextViewHeight < CGFloat(75) ? desiredTextViewHeight : CGFloat(75)
let textViewWantsHeightObservable = self.textViewHeightConstraintOutlet
.rx.observe(CGFloat.self, "constant")
.flatMapLatest({ [weak self] height -> Observable<CGFloat> in
guard let _ = self else { return Observable.empty() }
return Observable.of(desiredTextViewHeight)
})
textViewWantsHeightObservable
.do(onNext: { height in print(height) })
.flatMapLatest({ height in
return Observable.of(height < CGFloat(75))
})
.asDriver(onErrorJustReturn: false)
.drive(readMoreLabelOutlet.rx_hidden)
.addDisposableTo(disposeBag)
let readMoreDescriptionGestureRecognizer = UITapGestureRecognizer(target: self, action: nil)
readMoreLabelOutlet.addGestureRecognizer(readMoreDescriptionGestureRecognizer)
readMoreDescriptionGestureRecognizer.rx.event
.single()
.flatMapLatest({ _ in
return textViewWantsHeightObservable
})
.single()
.subscribe (onNext : { [weak self] height in
guard let _ = self else { return }
self!.textViewHeightConstraintOutlet.constant = height
self!.readMoreLabelOutlet.isHidden = true
self!.tableView?.beginUpdates()
self!.tableView?.endUpdates()
})
.addDisposableTo(rx_disposeBag)
// just a snippet to remember the sizeThatFits bit
// self.textViewHeightConstraintOutlet
// .rx.observe(CGFloat.self, "constant")
// .subscribe(onNext: { [weak self] constant in
// let isTextLargerThanView = (self?.descriptionOutlet.sizeThatFits((self?.descriptionOutlet.frame.size)!).height)! > CGFloat(50)
// self?.moreLabelOutlet.text = constant == 50 ? "more >" : "< less"
// self?.moreLabelOutlet.underLine()
// self?.moreLabelOutlet.isHidden = isTextLargerThanView ? false : true
// detailsGestureRecognizer.isEnabled = isTextLargerThanView ? true : false
// moreGestureRecognizer.isEnabled = isTextLargerThanView ? true : false
// })
// .addDisposableTo(rx_disposeBag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment