There is some text in a text field. Before setting it to txtfdName, I want to capitalize the first letter.
[addrecipe.txtfdName setText:txtfield1.text];
Use autocapitalizationType for capital letter of UITextField.
txtfield1.autocapitalizationType = UITextAutocapitalizationTypeWords;
Use this code to capitalized first latter
NSString *abc = @"this is test";
abc = [NSString stringWithFormat:@"%@%@",[[abc substringToIndex:1] uppercaseString],[abc substringFromIndex:1] ];
NSLog(@"abc = %@",abc);
To capitalize the first letter of each word manually in code:
NSString* name = [addrecipe.txtfdName setText:txtfield1.text];
name = [name capitalizedString];
Use autocapitalizationType property of UITextInputTraits protocol which is conform by both UITextView
and UITextField
txtfield1.autocapitalizationType = UITextAutocapitalizationTypeWords;
txtfield1.autocapitalizationType = UITextAutocapitalizationTypeWords;
or you can also try
[addrecipe.txtfdName setText:[txtfield1.text capitalizedString]]; // the first character from each word in the receiver changed to its corresponding uppercase value
select txtfield1 and go to Attributes Inspector , there is a Capitalization with a drop down menu and select menu form Following
Word : if you select Word than first character of every word is Capital
Sentences : if you Sentence than first character of First word of every sentence is Capital
All Characters : All Characters are Capital
In XCODE 10.0, swift 3, I had to use the following:
textField.autocapitalizationType = UITextAutocapitalizationType.none
textField.autocapitalizationType = UITextAutocapitalizationType.words
textField.autocapitalizationType = UITextAutocapitalizationType.sentences
textField.autocapitalizationType = UITextAutocapitalizationType.allCharacters