13

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];
0

7 Answers 7

36

Use autocapitalizationType for capital letter of UITextField.

txtfield1.autocapitalizationType = UITextAutocapitalizationTypeWords;
3
  • 6
    It will not work if its simulator or Auto-Capitalisation is turned off from Settings>General>Keyboard>Auto-Capitalisation on iOS/iDevice
    – NaXir
    Commented Jun 13, 2013 at 8:14
  • Be aware that this solution doesn't work if auto correction is disabled for text field.
    – alexey
    Commented Apr 17, 2014 at 9:12
  • 1
    If you want to capitalize the first letter you should use UITextAutocapitalizationTypeSentences. Because UITextAutocapitalizationTypeWords will capitalize first letter of every word in the text field, not just the first one.
    – Werewolf
    Commented Apr 8, 2016 at 8:19
14

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);
8

To capitalize the first letter of each word manually in code:

NSString* name = [addrecipe.txtfdName setText:txtfield1.text];
name = [name capitalizedString];
5

Use autocapitalizationType property of UITextInputTraits protocol which is conform by both UITextView and UITextField

txtfield1.autocapitalizationType = UITextAutocapitalizationTypeWords;
4
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
1

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.