24

Quick question, does anyone know how to programatically make the text in the textField bold or italic?

   @property(nonatomic, retain) IBOutlet UITextField *textField_TOP_01;

   [textField_TOP_01 setTextColor:[UIColor redColor]];
   [textField_TOP_01 setText:@"This text is bold"];

Much appreciated

Gary

fuzzygoat
  • 26,573
  • 48
  • 165
  • 294

5 Answers5

54

You can use the following methods to get a bold or italic system font;

UIFont* boldFont = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
UIFont* italicFont = [UIFont italicSystemFontOfSize:[UIFont systemFontSize]];

Then simply set the font that the text field uses;

[textField_TOP_01 setFont:boldFont];

If you want to see more about fonts with the iPhone, you can see a nice screen shot of all of the available fonts here: https://www.cocoanetics.com/2010/02/understanding-uifont/ or you can read about the class which shows you how you can also pull out the 'buttonFontSize' and 'labelFontSize' etc here; https://developer.apple.com/documentation/uikit/uifont

Cœur
  • 37,241
  • 25
  • 195
  • 267
John Wordsworth
  • 2,591
  • 1
  • 20
  • 24
  • how to mix between bold and italics how to save the text with this format in txt file – Ali Apr 17 '11 at 12:10
  • Depending on what you're after... 1. If you would like something read-only then you could use a UIWebView to display HTML to the user. (http://iphoneincubator.com/blog/windows-views/display-rich-text-using-a-uiwebview). This wouldn't allow for edits though. 2. If you need a 'Pages' like editable view, then I believe that the OmniGroup libraries provide an editable text area which supports formatting. I've never used it however, and I can't find any documentation easily on github. https://github.com/omnigroup/OmniGroup – John Wordsworth Apr 21 '11 at 20:37
9

Attributes like bold and italic can be set by using the appropriate font. See this answer for more details.

UIFont * font = [UIFont fontWithName:@"Helvetica-Bold"
                                size:[UIFont systemFontSize]];
[textField setFont:font];

Alternatively, if you are just looking to create bold and italic versions of the standard iPhone system font, you can use the boldSystemFontOfSize: or italicSystemFontOfSize: methods.

Community
  • 1
  • 1
e.James
  • 116,942
  • 41
  • 177
  • 214
5

If normal font is like,

[UIFont fontWithName:@"Gill Sans" size:16]]

I can make it bold by modifying the line as,

[UIFont fontWithName:@"GillSans-Bold" size:16]]
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
1

In swift 3.0 Suppose your UITextField variable name is textField.

textField.font = UIFont(name: "HelveticaNeue-Medium", size: 17)// for medium

name: "HelveticaNeue-Bold" // for bold
Dilip Jangid
  • 754
  • 1
  • 10
  • 20
0

There is a property called UIFont

@property(nonatomic, retain) UIFont *font

For creating a UIFont with a bold, I think you can use:

+ (UIFont *)fontWithName:(NSString *)fontName size:(CGFloat)fontSize

UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:[UIFont systemFontSize]];

vodkhang
  • 18,639
  • 11
  • 76
  • 110