简体   繁体   中英

UITextField make text bold?

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

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

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM