How to Add Followed Text to Uitextfield

How to add text to an active UITextField

To write text in last active UITextField you have to make your UIViewController a delegate of UITextField

ex:

class ViewController: UIViewController, UITextFieldDelegate

declare a activeField variable

ex:

var activeField: UITextField?

then implements textFieldDidBeginEditing

ex:

func textFieldDidBeginEditing(textField: UITextField)
{
activeField = textField
}

So your button function will be something like

@IBAction func buttonEen(sender: UIButton) {
if activeField
{
activeField.text = "static text"
}

}

How can I append text to the active UITextField - Swift

I was able to fix it after some research with changes to the textFieldDidBeginEditing and didTapButton functions: Here is the full code if anybody wants to choose one textField at a time with a custom keyboard:


import UIKit

class HomeVC: UIViewController, ButtonTapDelegate, UITextFieldDelegate {

@IBOutlet var textField1: UITextField!
@IBOutlet var textField2: UITextField!
@IBOutlet var keyboardView: UIView!

var activeField: UITextField?
var delegate: ButtonTapDelegate!

override func viewDidLoad() {
addKeyboard(view: keyboardView)
textField1.inputView = UIView()
textField2.inputView = UIView()
textField1.becomeFirstResponder()
activeField?.delegate = self
}

func textFieldDidBeginEditing(_ textField: UITextField) {
self.activeField = textField
}

func addKeyboard(view: UIView) {
let keyboard = KeyboardVC(nibName: "KeyboardVC", bundle: nil)
keyboard.delegate = self
view.addSubview(keyboard.view)
addChild(keyboard)
}

func didTapButton(sender: UIButton) {
if textField1 == self.activeField {
if sender.tag == 8 {
textField1.text?.append(contentsOf: " ")
} else if sender.tag == 9 {
textField1.text?.removeAll()
} else {
let val = sender.titleLabel?.text?
textField1.text?.append(contentsOf: val!)
}
return;
}

if textField2 == self.activeField {
if sender.tag == 8 {
textField2.text?.append(contentsOf: " ")
} else if sender.tag == 9 {
textField2.text?.removeAll()
} else {
let val = sender.titleLabel?.text?
textField2.text?.append(contentsOf: val!)
}
return;
}
}
}

How to insert text into UITextField?

You can replace the text as you have done previously and restore the position of your caret. Note that this can only be done with UITextView rather than UITextField (that would require a lot more unjustified work, unless you absolutely have to use UITextField):

NSRange selectionRange = myTextView.selectedRange;
myTextView.text = myNewText;
myTextView.selectedRange = selectionRange;

You have to note that it's a good idea to check the validity of selectionRange variable for new text - make sure that cursor position isn't greater than the new text length.

Append text with UITextField text

import <UIKit/UiKit.h>
@interface MyViewController : UIViewController {
    UIButton *btnToAppend;
UITextField *textfield;
}

- (IBAction)buttonClick:(id)sender;

@end

import "MyViewController.h"

@implementation MyViewController

-(void)init{
btnToAppend = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btbToAppend.frame = CGRectMake(10, 10, 100, 30);
[btnToAppend setText:@"Append Text" forState:UIControlStateNormal];
[btnToAppend addTarget:self action:@select(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubView:btnToAppend];
textfield = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 50)];
textfield.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubView:textfield];
}

-(IBAction)buttonClick:(id)sender {
NSString *string = [textfield.text stringByAppendingString:@"Welcome "];
    textfield.text = string;
}

- (void)dealloc {
    [super dealloc];
}

@end

Output will be like :

Welcome Welcome Welcome ...

How can I append text from a UITextField to an array?

A UITextField can never return nil for its text property, so it is safe to force unwrap it. You can simply call .append on the array to append the text:

textfieldArray.append(someTextField.text!)

How to set up UITextFieldDelegate for multiple text fields?

If your goal is to have all three text fields follow the same rule, set the delegate for all three. The shouldChangeCharactersIn only needs to check the “current” text field into which the user is currently typing.

A minor observation, but I also would avoid recreating the CharacterSet of allowed characters repeatedly. You can simply make that a property.

That reduces it down to something like:

private let allowedCharacters = CharacterSet(charactersIn: "ABCDEFGĞHIİJKLMNOÖPRSŞTUÜVYZ")

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if (textField.text?.count ?? 0) - range.length + string.count > 1 {
return false
}

return allowedCharacters.isSuperset(of: CharacterSet(charactersIn: string.localizedUppercase))
}

If you want them only entering uppercase characters, I would:

  • Change the keyboard capitalization to “All Characters”, so they are more likely to not enter lowercase letters:

    Sample Image

  • Optionally change the capitalization by adding an “editing changed” action for your text field to:

    @IBAction func editingChanged(_ textField: UITextField) {
    textField.text = textField.text?.localizedUppercase
    }

    You might have to experiment whether you want to use localizedUppercase or uppercased() or uppercased(with:).

    Note, this “uppercase the whole string” logic is a little sloppy. If you were allowing multi-character strings in your input, you really would want to capture where the cursor was and restore it. (Otherwise it could be an annoying UX where if the user is changing the first character of a multi-character string, the cursor would jump to the end.) E.g., a simple rendition might be:

    @IBAction func editingChanged(_ textField: UITextField) {
    let range = textField.selectedTextRange
    textField.text = textField.text?.localizedUppercase
    textField.selectedTextRange = range
    }

    But for your single character input, the simpler example, above, should be sufficient.

How to add target to UITextField in a class other than ViewController

I think it could be because you are not persisting with your ListenerModule object.

I believe you are doing this ListenerModule().textWatcher(textField: password, view: self) in some function so the scope of the object created is limited to that function.

You could do the following:

// Globally in your UIViewController subclass
var listenerModule: ListenerModule?

// Some set up function after text field is initialized
private func setup()
{
listenerModule = ListenerModule()

// Then call the text watcher, not sure why you pass the view,
// doesn't seem like you use it
listenerModule?.textWatcher(textField: password, view: self)
}

Give this a try and see if this solves your issue



Related Topics



Leave a reply



Submit