Quote:
Originally Posted by DaanB
Hello,
I'm new to the SDK, and need some help!  I learned how to change a label after the user types something in a textfield. But is it possible to only show the text that a user types between two "."s
So if a user types: Hello i need .help. please
It will only display: help
Please let me know if this is possible and how.
Greets,
Daan
|
Daan,
Of course it's possible. It's simple programming.
You would need to write code that looked for two periods, extracted the text between those periods, and then assigned the results to the label.
Take a look at the methods in NSString that handle searching and breaking strings into substrings. Specifically, look at the methods rangeOfString and componentsSeparatedByString.
You could use code like this:
Code:
NSString* userText = userTextField.text;
NSArray* subStrings = [userText componentsSeparatedByString: @"."];
if ([subStrings count] == 3) //user string contains exactly 2 periods, and that text delimits strings
{
NSString* stringInsidePeriods = [subStrings objectAtIndex: 1]; //Get the second substring
if ([stringInsidePeriods length] > 0)
displayLabel.text = stringInsidePeriods;
}
If the user entered
string1.string2.string3
The code above would put "string2" into the label displayLabel.
It assumes you have 2 IBOutlets in your code, one a UITextView or UITextField called userTextField and the other a UILabel called displayLabel.
If the user entered
string1..string2
or
string1.string2.string3.string4
It would not change the label, because there are checks to make sure the string is only broken into 3 pieces with periods, and that the second piece of text is not empty.