ios Swift 增加 UITextview 的字体大小,如何?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28742018/
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
Swift Increase font size of the UITextview,how?
提问by GioB
I am trying to add two buttons to my app to set the font size of a UITextview,and i found this function
我正在尝试向我的应用程序添加两个按钮来设置 UITextview 的字体大小,我发现了这个功能
textview.font.increaseSize(...) //and decreaseSize(...)
But i don't understand what I have to put inside the parentheses,i want to increase and decrease the font size by one point
但是我不明白我必须在括号内放什么,我想将字体大小增加和减少一点
Thanks for the answers
感谢您的回答
回答by Adil Soomro
I don't think there's a method named increaseSize()
. May be you've find some UIFont
or UITextView
category.
我认为没有名为increaseSize()
. 可能你已经找到了一些UIFont
或UITextView
类别。
The official UIFont
class document doesn't reveal any such method.
该官员UIFont
类文件没有显示出任何这样的方法。
Additionally you can increase the font like this:
此外,您可以像这样增加字体:
textview.font = UIFont(name: textview.font.fontName, size: 18)
The above statement will simply set the existing font size to 18, change it to whatever you want.
上面的语句将简单地将现有字体大小设置为 18,将其更改为您想要的任何值。
However if you want some method like you've posted, you can introduce your own category like this:
但是,如果你想要一些像你发布的方法,你可以像这样介绍你自己的类别:
extension UITextView {
func increaseFontSize () {
self.font = UIFont(name: self.font.fontName, size: self.font. pointSize+1)!
}
}
Swift 2 & 3:
斯威夫特 2 和 3:
import UIKit
extension UITextView {
func increaseFontSize () {
self.font = UIFont(name: (self.font?.fontName)!, size: (self.font?.pointSize)!+1)!
}
}
and simply import this to wherever you want to use like this:
并简单地将其导入到您想要使用的任何地方,如下所示:
textview.increaseFontSize()
it'll increase the font by 1 every time you call it..
每次调用它时,它都会将字体增加 1。
回答by AndyOS
For the system font, this also works:
对于系统字体,这也适用:
yourTextView.font = .systemFontOfSize(16)
回答by kuzdu
Swift 4
斯威夫特 4
yourTextView.font = .systemFont(ofSize: 16)
yourTextView.font = .systemFont(ofSize: 16)
回答by Juan Manuel Garcia Carmona
Swift 5
斯威夫特 5
textView.font = UIFont.preferredFont(forTextStyle: .body) //.title, .headline, etc..
回答by PandaDev
SWIFT 5
快速 5
You can also do
你也可以这样做
yourTextView.font = UIFont.systemFont(ofSize: CGFloat)