How to change textfield font size programmatically?

心不动则不痛 提交于 2019-12-01 20:08:54

问题


i have a button that create textfield, also i have button to change the textfield font. I create 3 button with different title 17,20,36 i want the functions of that button to change the font size of the textfield what will i do?


回答1:


If you want to simply change your textfield's font size , and not change it's font style at the same time , code below may be work : `

UITextField *yourTextField = [[UITextField alloc]init];
CGFloat yourSelectedFontSize = 14.0 ;
UIFont *yourNewSameStyleFont = [yourTextField.font fontWithSize:yourSelectedFontSize];
yourTextField.font = yourNewSameStyleFont ;

One thing you have to note is that : when you change your textfield's font size , you should always pay attention to your textfield view's height , keep your textfiled's height taller than your font height !

You can have a try !




回答2:


Swift 3 Version of accepted answer:

let yourTextField = UITextField()
let customFont:UIFont = UIFont.init(name: (textField.font?.fontName)!, size: 14.0)!
font = customFont
yourTextField.font = customFont



回答3:


yes you can do it.

but you have to change the textField's frame also(to show the full(height) text)

[tFild_1 setFont:[UIFont fontWithName:@"Helvetica Neue" size:17]];

instead of "Helvetica Neue" use your font name.

thanx,




回答4:


Swift 4 is this:

yourTextField.font =  UIFont.init(name: (Montserrat.bold.rawValue), size: 18.0)

Instead Monserrat use your font



来源:https://stackoverflow.com/questions/13225494/how-to-change-textfield-font-size-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!