How start to UITextView from paragraph in Swift - swift

How to start a new line in UITextView with a little space, called paragraph I believe?

What you need to do is make the controller confront to UITextFieldDelegate
for example:
class ViewController: UIViewController,UITextFieldDelegate
{
//link the textfield
#IBOutlet weak var textField: UITextField!
override func viewDidLoad()
{
super.viewDidLoad()
// link the delegate to the textfield
textField.delegate = self
}
//this function detects the return button when it's pressed
func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
//since we always want to start a new line with space we override the default function
textField.text += "\n "
return false
}

Related

How to trigger textFieldDidChangeSelection only for a specific UITextField?

In a view with multiple UITextField elements, textFieldDidChangeSelection will trigger for any editing done in any UITextField.
Can we perform some action inside this function only when a certain UITextField is edited?
class MyViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var text1: UITextField!
#IBOutlet weak var text2: UITextField!
#IBOutlet weak var text2: UITextField!
//..........
func textFieldDidChangeSelection(_ textField: UITextField) {
print(textField.text) // this code should run only for text1 for example
}
}
You just need to compare the textField to the one you want:
func textFieldDidChangeSelection(_ textField: UITextField) {
guard textField === text1 else { return }
print(textField.text)
}
Note that you should use the identity operator === not the equality operator ==.
You can do something like this:
func textFieldDidChangeSelection(_ textField: UITextField) {
if textField === text1 {
print(textField.text) // this code should run only for text1 for example
}
}

Swift How to disable a TextField while another UITextField is highlighted or has value

I have three text fields: tempTextField, airPressureTextField, and airDensityTextField. I want to create a check between Textfields to get the following logic:
If tempTextField or airPressureTextField is in focus where user trying to enter a value or user enters value and returns, I want to disable airDensityTextField. I tried this code. It disables airDensityTextField correctly. But If I remove the focus from tempTextField or airPressureTextField and empty both those two textfields, the airDensityTextField is still disabled.
func textFieldDidEndEditing(_ textField: UITextField) {
if let t = tempTextField.text, let ap = airPressureTextField.text {
airDensityTextField.isEnabled = false
} else {
airDensityTextField.isEnabled = true
}
}
I tried the code in both textFieldDidEndEditing and textFieldDidBeginEditing, the result is the same.
How can I make it work?
You can see which textfield is editing inside textFieldDidBeginEditing and disable airDensityTextField. Then, when they finish editing, you need to check if the text is "" (blank) in addition to whether it's nil or not.
class TextFieldVC: UIViewController, UITextFieldDelegate {
#IBOutlet weak var tempTextField: UITextField!
#IBOutlet weak var airPressureTextField: UITextField!
#IBOutlet weak var airDensityTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
tempTextField.delegate = self
airPressureTextField.delegate = self
airDensityTextField.delegate = self
}
func textFieldDidBeginEditing(_ textField: UITextField) {
switch textField {
case tempTextField:
airDensityTextField.isEnabled = false
case airPressureTextField:
airDensityTextField.isEnabled = false
default:
break
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
if
let tempText = tempTextField.text,
let apText = airPressureTextField.text,
tempText != "" || apText != "" /// check if at least one of them has text
{
airDensityTextField.isEnabled = false
} else {
airDensityTextField.isEnabled = true
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return false
}
}

textfield not empty show save button in Swift

My save button is hidden with the function saveBtnHidden() in the code below. However, the save button won't reappear when text is typed into the text field. I have tried multiple solutions similar to this. Every time that I type into the text field, the save button just won't show up.
import UIKit
class TableViewController: UITableViewController, UITextFieldDelegate {
#IBOutlet weak var saveBtn: UIButton!
#IBOutlet var nicknameField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
saveBtnHidden()
}
func saveBtnHidden() {
if (nicknameField.text?.isEmpty ?? true) {
// is empty
saveBtn.isHidden = true
} else {
saveBtn.isHidden = false
}
}
#IBAction func saveBtnPressed(_ sender: Any) {
performSegue(withIdentifier: "nextPage", sender: nil)
}
}
You are getting this error because your function saveBtnHidden() is only called once in viewDidLoad(). It does not get called again when the text in your text field changes. To detect when text changes, you will need to add a target to your text field that calls a function when it changes (.editingChanged) like this:
nicknameField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
Then in the textFieldDidChange call your saveBtnHidden() function:
func textFieldDidChange(_ textField: UITextField) {
saveBtnHidden()
}
Code adapted from: How do I check when a UITextField changes?
Use delegate to be notify of any change. Delegates is a key part of iOS development and Apple's framework.
class TableViewController: UITableViewController, UITextFieldDelegate {
#IBOutlet weak var saveBtn: UIButton!
#IBOutlet var nicknameField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
saveBtnHidden()
nicknameField.delegate = self
}
func textFieldDidChange(_ textField: UITextField) {
saveBtnHidden()
}
// More of your source code below...

Converting UITextField to String in Swift

I'm trying to convert a UITextField object to a String so that I can pass it to a function in my model file. But it is printing a blank even after I type in some characters in the name input text field and then click outside of it. Why is nothing stored there?
In view controller file:
#IBOutlet weak var nameInputText: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
nameInputText.delegate = self
let text: String = nameInputText.text!
print(text) //this prints a blank
model.validateName(nametext: text)
}
It should be like below
#IBOutlet weak var usernameInputText : UITextField!
override func viewDidLoad() {
super.viewDidLoad()
usernameInputText.delegate = self
}
func ClickOnButton()
{
let text: String = usernameInputText.text!
print(text) //this prints My text
model.validateName(nametext: text)
}
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String)
-> Bool
{
let text: String = usernameInputText.text!
print(text) //this prints My text
model.validateName(nametext: text)
}
viewDidLoad gets called before your ViewController would be visible to the user, so any text field can only contain text if you have set that text before (in InterfaceBuilder or code). If you want to access user input from that text field, you should either do that in textFieldDidEndEditing or in any other function which you know can only be called after the user interaction happened.
#IBOutlet weak var nameInputText: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
nameInputText.delegate = self
}
extension ViewController: UITextFieldDelegate {
func textFieldDidEndEditing(_ textField: UITextField) {
model.validateName(nametext: textField.text)
}
}

Unable to delegate textfield with UipickerView in SWIFT

i'm currently facing a problem in swift.
I implemented a method to make textfield next responder on returnkey tap, and o make it fonctional i have to make my textfields delegate (everything work on my other class).
The problem is, i also implemented a UIPickerView , which appear when "textfieldcategorie" is tapped, this pickerview need to be delegate, to put the choosen value in the textfield.
by the way, the method to make textfield nextresponder not working anymore, at the moment where i put the textfield in delegate , it become inactive.
this is the code is used :
declaration of my variable :
#IBOutlet var textfieldcategorie: UITextField!
#IBOutlet var keyword: UITextField!
#IBOutlet var codepostal: UITextField!
#IBOutlet var prix: UITextField!
#IBOutlet var pickercategorie: UIPickerView! = UIPickerView()
The method to show uipickerview on tap :
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
pickercategorie.hidden = false
return false
}
The method to make textfield nextresponder :
func textFieldShouldReturn(textField: UITextField) -> Bool {
//delegate method
if textField == keyword {
textfieldcategorie.becomeFirstResponder()
}
return true
}
}
I don't know if it's important, but i also use theses methods :
to move up the textfield when the keyboard appears :
func textFieldDidBeginEditing(textField: UITextField) {
animateViewMoving(true, moveValue: 25)
}
func textFieldDidEndEditing(textField: UITextField) {
animateViewMoving(false, moveValue: 25)
}
func animateViewMoving (up:Bool, moveValue :CGFloat){
var movementDuration:NSTimeInterval = 0.3
var movement:CGFloat = ( up ? -moveValue : moveValue)
UIView.beginAnimations( "animateView", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration )
self.view.frame = CGRectOffset(self.view.frame, 0, movement)
UIView.commitAnimations()
}
//Fin Monter la vue lorsque le clavier apparait
And to dismiss the keyboard on tap anywhere in the view:
func DismissKeyboard(){
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}
Thank's
I feel you must change your code as follows:
func textFieldShouldReturn(textField: UITextField) -> Bool {
//delegate method
if textField === keyword {
textfieldcategorie.becomeFirstResponder()
}
return true
}
The reason being == operator checks equality of two objects but === operator check identity of two objects i.e. wether these two refer to the same object.
Also pleas change:
#IBOutlet var pickercategorie: UIPickerView!