How to move downward the textView cursor? [duplicate] - ios

This question already has an answer here:
iOS: Programmatically move cursor up, down, left and right in UITextView
(1 answer)
Closed 3 years ago.
Can someone show me how to move the textView cursor downward cause I don't know how to do it,
Also where should I put it
in the textViewDidBeginEditing(_ textView: UITextView) or in the textViewDidEndEditing(_ textView: UITextView)
Thanks in advance

This will move coursor to the end of text:
func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
DispatchQueue.main.async {
textView.selectedRange = NSRange(location: textView.text.count, length: 0)
}
return true
}
If you want to begin text from a new line, add this, before return true:
textView.text = textView.text + "\n"
If you want to skip two lines, make it "\n\n", and so on.

Related

Check If UItextView Text Is Empty Once

I want to call an animation when my UITextView is empty and again when it is not empty.
I get the UITextView changes in the delegate method below;
func textViewDidChange(_ textView: UITextView) {
}
However, this checks if the UITextView is empty every time it changes. I only want to call this once.
Is there any method for this?
This delegate is handled by UIKit, therefore is not possible to select when it is called. However, there are these other options according to Apple docs:
func textViewDidBeginEditing(_ textView: UITextView)
func textViewDidEndEditing(_ textView: UITextView)

How to add text bubble styles when I separate the email address by comma as show in the screen shot attached

Hi I'm new to iOS app development. I am implemented an UITextview to add multiple address separated by comma I need to apply a style bubble when user separates the email by pressing comma or space or done button (in the keyboard.) reference screen shot from android
I am executing an email array same as below
func executeEmailArray(){
self.emailString = self.emailTF.text ?? ""
self.emailArray = emailString.components(separatedBy: ",")
print("emailArray \(self.emailArray)")
}
I formatted the uiTextView as below by using an extension
extension NewShoppingListVC: UITextViewDelegate {
func textViewDidChangeSelection(_ textView: UITextView) {
// Moves cursor to start when tapped on textView with placeholder
if emailTF.text == placeholder {
emailTF.selectedRange = start
}
}
func textViewDidChange(_ textView: UITextView) {
// Manages state of text when changed
if emailTF.text.isEmpty {
emailTF.text = placeholder
emailTF.textColor = .lightGray
} else if emailTF.text != placeholder {
emailTF.textColor = .black
}
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
// Called when you're trying to enter a character (to replace the placeholder)
if emailTF.text == placeholder {
emailTF.text = ""
}
return true
}
}
Please add some codes to explain me how to apply the textbubbles as in the image

UITextView dynamic height scrolling doesn't work when long text is pasted

I've implemented changing the height of UITextView dynamically when height reaches a certain value by following this solution https://stackoverflow.com/a/38454252/12006517
This works fine however text view freezes when I paste a large chunk of text in it first time. After pasting large chunk of text it doesn't go to the end of text content and cursor disappears while text view freezes. I've to hit delete key and start entering then it starts to work fine.
Subsequent paste of large chunk of text works. So problem happens only pasting first time.
How do I fix this issue?
class MyViewController: UIViewController {
let messageTextViewMaxHeight: CGFloat = 200
}
extension MyViewController: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
if textView.contentSize.height >= self.messageTextViewMaxHeight {
textView.isScrollEnabled = true
} else {
textView.frame.size.height = textView.contentSize.height
}
}
}
You can try below code. It working fine.
func textViewDidChange(_ textView: UITextView) {
let sizeThatShouldFitTheContent = textView.sizeThatFits(textView.frame.size)
if sizeThatShouldFitTheContent.height > 120 {
textView.isScrollEnabled = true
}else{
textView.isScrollEnabled = false
}
}

Issue with updating Edited Textview in swift

I have a textView and i need to update my textView content and save the edited text. In my case everything is working but in the UI edited text is updated.self.taskName data get it from the previous ViewController
In viewDidLoad i was written like this
self.taskNameTextView.text = self.taskName
self.taskNameTextView.delegate = self
In textViewDelegate,
func textViewDidChange(_ textView: UITextView) {
self.taskName = textView.text
}
and button action i have to passed the edited textString and it return successful but not changed updated text in UI.Can you please anyone help me to figure out this problem.
Rather than textViewDidChange please write your code inside
func textViewDidEndEditing(_ textView: UITextView) {
// your code here
}
For more delegate functions of TextView please refer to this post;
How to detect text view begin editing and end editing in swift 3

Change 'Return' button function to 'Done' in swift in UITextView

I would like to get rid of the "return" function of the keyboard while the user is typing, so there are no new lines, so instead I would like the 'return' key to function as 'Done' so it would hide the keyboard.
I am using a UITextView, that is editable, so the user is able to type their post, and post it to the main timeline, but since I have fixed cells, I don't want the user to be able to press 'return' and their post would be out of range of the timeline.
I found this that works with UITextField, but not with UITextView:
func textFieldShouldReturn(textField: UITextField!) -> Bool {
textField.resignFirstResponder() //if desired
return true
}
So I just wanted to know if there is a way to do that in a UITextView, or at least to be able to hide the keyboard if pressed return, instead of creating a new line.
You can set the return key type of the text field:
textField.returnKeyType = UIReturnKeyType.done
Update
You can definitely use the same approach to set the return key to "Done", as mentioned above. However, UITextView doesn't provide a callback when user hits the return key. As a workaround, you can try to handle the textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) delegate call, and dismiss the keyboard when you detect the input of a new line character:
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if (text == "\n") {
textView.resignFirstResponder()
}
return true
}
I have tried many codes and finally this worked for me in Swift 3.0 Latest [April 2019] this achieved using UITextFields
The "ViewController" class should be inherited the "UITextFieldDelegate" for making this code working.
class ViewController: UIViewController,UITextFieldDelegate
Add the Text field with the Proper Tag number and this tag number is used to take the control to appropriate text field based on incremental tag number assigned to it.
override func viewDidLoad() {
userNameTextField.delegate = self
userNameTextField.tag = 0
userNameTextField.returnKeyType = .next
passwordTextField.delegate = self
passwordTextField.tag = 1
passwordTextField.returnKeyType = .go
}
In the above code, the "returnKeyType = UIReturnKeyType.next" where will make the Key pad return key to display as "Next" you also have other options as "Join/Go" etc, based on your application change the values.
This "textFieldShouldReturn" is a method of UITextFieldDelegate controlled and here we have next field selection based on the Tag value incrementation.
func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
if let nextField = textField.superview?.viewWithTag(textField.tag + 1) as? UITextField {
nextField.becomeFirstResponder()
} else {
textField.resignFirstResponder()
return true;
}
return false
}
If you're working with a storyboard or xib, you can change the UITextView's Return button to 'Done' (or various other options) within Interface Builder, without the need for any setup code. Just look for this option in the Attributes inspector:
From there, you just pair it up with the UITextViewDelegate code that others have already provided here.
Swift v5:
extension ExampleViewController: UITextViewDelegate {
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if (text == "\n") {
textView.resignFirstResponder()
}
return true
}
}
And then, in your viewDidLoad() method:
exampleTextView.delegate = self
Working in Swift 4
Add this in viewDidLoad().
textField.returnKeyType = UIReturnKeyType.Done
Add this anywhere you like.
extension UITextView: UITextViewDelegate {
public func textViewDidChange(_ textView: UITextView) {
if text.last == "\n" { //Check if last char is newline
text.removeLast() //Remove newline
textView.resignFirstResponder() //Dismiss keyboard
}
}
}

Resources