I'm working on app that will take user input from UITextFields for data such as users' first and last names. Later on, I want to display this data in bold.
I've read NSAttributedString change style to bold without changing pointSize? and Any way to bold part of a NSString?, but these seem to indicate that you have to know the range of characters that you want to set to bold.
In my case, I don't actually know the length of the strings until after the user inputs their data...how would I know what the length of the string to set to bold until this happens?
Below are my properties for the UITextFields I'm using, which I'll want to display in bold later.
@property (strong, nonatomic) IBOutlet UITextField *firstName;
@property (strong, nonatomic) IBOutlet UITextField *lastName;
And below is how I'm currently creating strings to store the info from the text fields, all of which I want to display in bold:
NSString *firstName = [_firstName text];
NSString *lastName = [_lastName text];
You may not know the length of the text entered into your text box at the time you write your code, but you will know at runtime.
Try something like this:
NSMutableAttributedString *attribFirstNameString = [[NSMutableAttributedString alloc] initWithString: [firstName text]];
NSRange firstNameRange = NSMakeRange(0, [[firstName text] length]);
[attribFirstNameString addAttribute: NSFontAttributeName value: [UIFont boldSystemFontOfSize: 20.0f] range: firstNameRange];
[firstName setAttributedText: attribFirstNameString];