parsing UItextfield for incorrect format of entry - ios

I want to get users to enter:
xyz http://www.example.org
in a textfield. But if they enter "http://www.example.org", I want to alert them immediately so they can add the "xyz" part ahead of it.
Whats the best way in Swift (2.x) to do this ? Thanks in advance.

let text = textField.text
if text.hasPrefix("xyz") {
// you're good
} else {
// give alert
}

Related

How to Send Text in an UIInputViewController

so I've got the following code to insert text:
func insert(text: String) {
(textDocumentProxy as UIKeyInput).insertText(text)
}
But how do I actually get it to send to the chat window?
If you are looking to access all the text entered in the UIInputViewController, the following code may help:
var totalText = "" // complete text entered
if let before: String = self.textDocumentProxy.documentContextBeforeInput {
totalText += before
}
if let after: String = self.textDocumentProxy.documentContextAfterInput {
totalText += after
}
Now that you have complete text entered, you can send it to chat window. There are some limitations here which I can share if this is what you're looking for. Please let me know
Edit
To send text to whatsapp, you need to press send button which is outside the custom keyboard extension. If you want to enter text into the UITextView of whatsapp, it is pretty straightforward to do so. But, the custom keyboard extension can't control the send button of whatsapp

How to stop typing characters in textfield when text is the same size as textfield?

I want to find a way where when the characters completely fill up a textfield, to let the user type but it doesn't add any new characters to the textfield (like Snapchat). I've only see this done in Objective-C and I wasn't able to translate it into Swift. Can someone please help? Thanks!
You could do like this:
#IBAction func textField_EditingChanged(sender: AnyObject) {
//Set your font and add attributes if needed.
let stringSize: CGSize = textField!.text!.sizeWithAttributes([NSFontAttributeName: textField.font!])
if (stringSize.width > textField.bounds.width - 20){
textField.deleteBackward()
}
}
I added - 20 since I wanted to be sure that no small chars is added like "i", "1" etc.

Include text with a search

I have a button action that reads off input from a textfield when the user types something, here is the code:
-(IBAction)searchnickname:(id)sender{
[theWebView highlightAllOccurencesOfString: (#"*Nick:", searchText.text)];
}
What I need this to do, is when the user hits this button.. It puts "*Nick:" then whatever the user types after that. I need it to automatically add *Nick: before the string the user enters.
Thank you
I hope this helps:
-(IBAction)searchNickname:(id)sender {
[theWebView highlightAllOccurencesOfString: [#"*Nick:" stringByAppendingString:searchText.text]];
}
Try this...
-(IBAction)searchnickname:(id)sender{
[theWebView highlightAllOccurencesOfString:[NSString stringWithFormat:#"Nick:%#",searchText.text]];
}

How to restrict Special Characters in iOS by using xamarin?

I am working with IOS by using xamarin ,I am new to technology I have one requirement ,That is I don't want to allow any special character in my text box .How can I do this any one help me
thanks
What you can do is use ShouldChangeCharacters delegate.
Using it you can decide weather or not to update the text of the UITextField.
For example lets say you have a UITextField named textField:
textField.ShouldChangeCharacters = (textField, range, replacementString) => {
if (isSpecialCharacter) {
return false;
} else {
return true;
}
};

erase textfield value on touch up inside

I have a password textfield with a default value "password". The field is marked as secure. I'm trying to erase the default value once the user touch up inside the textfield.
- (IBAction)PasswordTouchUpInside:(id)sender
{
if (Password.text == #"Password")
{
Password.text = #"";
}
}
Is the touch up inside the wrong event?
You should compare strings using the method isEqualToString in the if condition:
if ([Password.text isEqualToString:#"Password"]) {
Password.text = #"";
}
Or, you can simply use the placeHolder property so that the UITextField does this behaviour automatically without you having to write PasswordTouchUpInside.
You probably want Touch Down, not up.

Resources