xcode 如何以编程方式更改文本字段字体大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13225494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change textfield font size programmatically?
提问by chineseGirl
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?
我有一个创建文本字段的按钮,我也有一个按钮来更改文本字段的字体。我创建了 3 个具有不同标题 17,20,36 的按钮,我希望该按钮的功能可以更改文本字段的字体大小,我该怎么办?
回答by monjer
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 !
你可以试一试!
回答by Adriana Carelli
Swift 4is this:
斯威夫特 4是这样的:
yourTextField.font = UIFont.init(name: (Montserrat.bold.rawValue), size: 18.0)
Instead Monserrat use your font
相反,Monserrat 使用您的字体
回答by inf1783
Swift 3Version of accepted answer:
Swift 3版本已接受的答案:
let yourTextField = UITextField()
let customFont:UIFont = UIFont.init(name: (textField.font?.fontName)!, size: 14.0)!
font = customFont
yourTextField.font = customFont
回答by damithH
yes you can do it.
是的,你可以做到。
but you have to change the textField's frame also(to show the full(height) text)
但是您还必须更改 textField 的框架(以显示完整(高度)文本)
[tFild_1 setFont:[UIFont fontWithName:@"Helvetica Neue" size:17]];
instead of "Helvetica Neue" use your font name.
使用您的字体名称而不是“Helvetica Neue”。
thanx,
谢谢,