I put the UITextView inside a UIView. The UIView expands as the user types in the UITextView but the problem is that if the user types on the next line, it doesn't show the text being typed until the user types on the third line, then it shows the text printed on the second line. Same goes with the 3rd line and 4th line, etc.
How can I fix this?
func textViewDidChange(_ textView: UITextView) {
print(textView.text)
let size = CGSize(width: prayerRequest.frame.width, height: .infinity)
let estimatedSize = textView.sizeThatFits(size)
textView.constraints.forEach { (constraints) in
if constraints.firstAttribute == .height {
constraints.constant = estimatedSize.height
}
viewContainer.constraints.forEach({ (constraints) in
if constraints.firstAttribute == .height {
constraints.constant = estimatedSize.height
viewContainer.layoutIfNeeded()
}
})
}
}
If you're using interface builder try setting the number of lines to 0.
Or from code textView.textContainer.maximumNumberOfLines = 10
First of all, you don't need to set height for textView's Parent. Even if you do set the low priority for textView's parent.
See the image, Purple being parent and Yellow is textView.
In viewDidLoad, add the following code:
textView.textContainerInset = UIEdgeInsets.zero
textView.textContainer.lineFragmentPadding = 0
Then, implement textView Delegate method:
extension ViewController : UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
let textHeight = textView.attributedText.boundingRect(with:
CGSize.init(width: textView.frame.width, height: .infinity),
options:[.usesLineFragmentOrigin, .usesFontLeading],
context: nil).height
if previousHeight != textHeight {
previousHeight = textHeight
print("Height text: \(textHeight)")
textViewHeight.constant = textHeight
self.view.layoutIfNeeded()
textView.setContentOffset(.zero, animated: false)
}
}
}
textViewHeight is height constraint of textView.
Initialize var previousHeight = CGFloat(0) as instance variable. It will help limit calls to layoutIfNeeded to only when the height is changed.
Bottom constraint of the textView will expand parent view. So you do not need to set Parent's height as well.
Related
I have tried 2 more methods. But those are not giving me the correct solution.
If i tried this one
func textViewDidChange(_ textView: UITextView) {
self.comments.translatesAutoresizingMaskIntoConstraints = true
self.comments.sizeToFit()
self.comments.isScrollEnabled = false
}
i can only type a one letter per a line.
If i tried this one
func textViewDidChange(_ textView: UITextView) {
self.comments.translatesAutoresizingMaskIntoConstraints = true
self.comments.isScrollEnabled = false
let fixedWidth = passedOut.frame.size.width
let newSize = comments.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
comments.frame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
}
Its working.
But at the viewDidLoad stage, the width of textview is certain less than which i constraints to the textview. But i gave the leading, trailing , height as same as passedOut textfield to that textview.
If i start typing in textview, the width of textview will become exact size which i want.
So pls suggest me what mistake i made?
Assign a height constraint to the text view
Use the following method to change the height:
func textViewDidChange(_ textView: UITextView) {
let size = textView.bounds.size
let newSize = textView.sizeThatFits(
CGSize(width: size.width, height: CGFloat.greatestFiniteMagnitude)
)
if size.height != newSize.height {
self.userMessageTextViewHeightConstraint.constant = newSize.height
textView.layoutIfNeeded()
}
}
You can achieve this behavior by doing 2 things in textview
Disable scrolling in textview
Assign the following constraints to textview
a) leading
b) trailing
c) top
d) height >= 30 ,assuming 30 is the minimum height of your textview.
Now when your text will grow textview will grow with it.
I'm trying to auto resize a UITextView in a TableView Custom Cell using the below function in my TableViewController. It works perfectly: it resize my TextView heigh according to the text length BUT I cannot find any help on how to adjust the TextView width! I already set the constraints to bounds it on the width of the cell but is not working. there is any function that can help me resize the textView width as the heigh?
Thanks.
this is my output now
func adjustUITextViewHeight(arg : UITextView)
{
arg.translatesAutoresizingMaskIntoConstraints = true
arg.sizeToFit()
arg.isScrollEnabled = false
}
I solved using this function:
func adjustUITextViewHeight(arg : UITextView, viewBox : UIView)
{
arg.isScrollEnabled = false
let size = CGSize(width: viewBox.frame.width, height: .infinity)
let estimatedSize = arg.sizeThatFits(size)
arg.constraints.forEach { (constraint) in
if constraint.firstAttribute == .height {
constraint.constant = estimatedSize.height
}
}
}
Here is the link to the video-tutorial: https://www.youtube.com/watch?v=0Jb29c22xu8
I have an app which has chatting functionality where UITextview is used for entering the message. UITextview height has to be dynamic (if user enters the message, the height has to be changed according to the text length till a specific Height).
How can I achieve this?
Disable Scrolling of textView.
TO Increase Height to a specific value and then enable scrolling.
Provide a maximum height constraint then add this code to your viewController
class YourViewController: UIViewController, UITextViewDelegate
{
#IBOutlet weak var yourTextView: UITextView!
let textViewMaxHeight: CGFloat = 100
override func viewDidLoad()
{
super.viewDidLoad()
yourTextView.delegate = self
}
func textViewDidChange(textView: UITextView)
{
if textView.contentSize.height >= self.textViewMaxHeight
{
textView.scrollEnabled = true
}
else
{
textView.frame.size.height = textView.contentSize.height
textView.scrollEnabled = false
}
}
}
Add a height constraint to your textView and create an outlet so you can adjust it. Then you can use the UITexfield delegate method textViewDidChange(_ textView: UITextView) to adjust the height.
func textViewDidChange(_ textView: UITextView) {
// get the current height of your text from the content size
var height = textView.contentSize.height
// clamp your height to desired values
if height > 90 {
height = 90
} else if height < 50 {
height = 50
}
// update the constraint
textViewHeightConstraint.constant = height
self.view.layoutIfNeeded()
}
shorter version...
func textViewDidChange(_ textView: UITextView) {
let maxHeight: CGFloat = 90.0
let minHeight: CGFloat = 50.0
textViewHeightConstraint.constant = min(maxHeight, max(minHeight, textView.contentSize.height))
self.view.layoutIfNeeded()
}
In case you are subclassing UITextView this could be a solution as well:
class Message: UITextView {
var maximalSizeNotScrollable: CGFloat = 200
var minimumHeightTextView: CGFloat = 35
var textViewHeightAnchor: NSLayoutConstraint!
override var contentSize: CGSize {
didSet {
if textViewHeightAnchor != nil {
textViewHeightAnchor.isActive = false
}
textViewHeightAnchor = heightAnchor.constraint(equalToConstant: min(maximalSizeNotScrollable, max(minimumHeightTextView, contentSize.height)))
textViewHeightAnchor.priority = UILayoutPriority(rawValue: 999)
textViewHeightAnchor.isActive = true
self.layoutIfNeeded()
}
}
}
The priority needs to be smaller than 1000 otherwise you will encounter problems with _UITemporaryLayoutHeight.
I was struggling with the same problem. After reading #Anuraj's response, I dragged and dropped the height constraint of the UITextView to view controller because when textview reaches to the max height and when the scroll is enabled textview was getting smaller just to display one line, so I found the solution by giving the max height to the height constraint of textview. Textview will expand until it reaches max height, after that it will be scrollable. When you delete text, textview height will get smaller automatically.
#IBOutlet weak var txMessageHeight: NSLayoutConstraint!
let textViewMaxHeight: CGFloat = 120
func textViewDidChange(_ textView: UITextView) {
if textView.contentSize.height >= self.textViewMaxHeight{
txMessageHeight.constant = self.textViewMaxHeight
textView.isScrollEnabled = true
}else{
textView.frame.size.height = textView.contentSize.height
textView.isScrollEnabled = false
}
}
Set the Height Constraint of the TextView to <= 120(maxHeight) in StoryBoard
func textViewDidChange(_ textView: UITextView) {
if textView.contentSize.height >= maxHeight {
textView.isScrollEnabled = true
} else {
textView.isScrollEnabled = false
}
}
And if you want to reset the height of TextView when its empty, you can just write
UIView.animate(withDuration: 0.15) {
self.view.layoutIfNeeded()
}
I'm start implementing text input to a chat app and wondering that is standard behavior of a UITextView with scroll enabled absolutely does not meet expectations.
I want just it is done in chats like WhatsApp. When text reached N, 5 for example lines, scroll bar appear and text container starts scrolling. I wrote code like this, but it doesn't work.
As i think needs to count rows in text container and make content insets, or something like this.
func textViewDidChange(_ textView: UITextView) {
let fixedWidth = myTextView.frame.size.width
myTextView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
let newSize = myTextView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
var newFrame = myTextView.frame
let oldFrame = myTextView.frame
newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
myTextView.frame = newFrame
let shift = oldFrame.height - newFrame.height
textView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: shift, right: 0)
textView.scrollIndicatorInsets = textView.contentInset
textView.scrollRangeToVisible(textView.selectedRange)
}
And myTextView is specified as:
let myTextView : UITextView = {
let textView = UITextView()
textView.translatesAutoresizingMaskIntoConstraints = false
textView.isScrollEnabled = false
textView.textContainer.maximumNumberOfLines = 5
textView.textContainer.lineBreakMode = .byWordWrapping
textView.inputAccessoryView = UIView()
return textView
}()
Not based on number of lines, but on a user defined height. You'll find your answer here:
https://stackoverflow.com/a/51235517/10115072
If you want this behaviour to happen is simple:
Create a UIView having UITextView inside
Create a height constraint in UIView priority 1000 of less than or equal your MAX_HEIGHT and also greater than or equal you MIN_HEIGHT
Create a height constraint in you TextView priority 999 equal to your MIN_HEIGHT
Then add this code to your controller
Code:
class YourViewController: KUIViewController {
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
textView.isScrollEnabled = true
}
}
extension YourViewController: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
let size = CGSize(width: view.frame.width, height: .infinity)
let estimatedSize = textView.sizeThatFits(size)
textView.constraints.forEach { (constraint) in
if constraint.firstAttribute == .height {
constraint.constant = estimatedSize.height
}
}
}
}
This has the same behaviour as WhatsApp textView
I wrote following code to fit UITextView's height to its text.
The size changes but top margin relative to first line of text differ every other time when I tap enter key on keyboard to add new line.
Setting
xCode 7.3
Deployment target: iOS 9
import UIKit
class ViewController: UIViewController, UITextViewDelegate {
lazy var textView: UITextView = {
let tv = UITextView(frame: CGRectMake(20, 200, (self.view.frame.width - 40), 0) )
tv.backgroundColor = UIColor.lightGrayColor()
return tv
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview( textView )
textView.delegate = self
let height = self.height(textView)
let frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.width, height)
textView.frame = frame
}
func textViewDidChange(textView: UITextView) {
let frame = CGRect(x: textView.frame.origin.x, y: textView.frame.origin.y, width: textView.frame.width, height: height(textView) )
textView.frame = frame
}
func height(textView: UITextView) -> CGFloat {
let size = CGSizeMake(textView.frame.size.width, CGFloat.max)
let height = textView.sizeThatFits(size).height
return height
}
}
I tried few other ways to fit UITextView height but they just acted the same say.
To fix this, subclass UITextView and override setContentOffset to allow scrolling only if the content height is larger than the intrinsic content height. Something like:
override func setContentOffset(_ contentOffset: CGPoint, animated: Bool) {
let allowScrolling = (contentSize.height > intrinsicContentSize.height)
if allowScrolling {
super.setContentOffset(contentOffset, animated: animated)
}
}
For auto-growing dynamic height text view, the caret moves on starting a new line. At this moment, the size of the text view hasn't grown to the new size yet. The text view tries to make the caret visible by scrolling the text content which is unnecessary.
You may also need to override intrinsicContentSize too.