Swift 增加UITextview的字体大小,怎么办?
Swift Increase font size of the UITextview,how?
我想在我的应用程序中添加两个按钮来设置 UITextview 的字体大小,我发现了这个功能
textview.font.increaseSize(...) //and decreaseSize(...)
但是我不明白括号里是什么,我想把字体大小增加和减少一个点
感谢您的回答
我认为没有名为 increaseSize()
的方法。可能是您找到了一些 UIFont
或 UITextView
类别。
official UIFont
class 文档没有透露任何此类方法。
另外你可以这样增加字体:
textview.font = UIFont(name: textview.font.fontName, size: 18)
上面的语句只是将现有的字体大小设置为18,更改为任何你想要的。
但是如果你想要像你发布的那样的方法,你可以像这样介绍你自己的类别:
extension UITextView {
func increaseFontSize () {
self.font = UIFont(name: self.font.fontName, size: self.font. pointSize+1)!
}
}
Swift 2 & 3:
import UIKit
extension UITextView {
func increaseFontSize () {
self.font = UIFont(name: (self.font?.fontName)!, size: (self.font?.pointSize)!+1)!
}
}
然后只需将它导入到您想要使用的任何地方,就像这样:
textview.increaseFontSize()
每次调用它都会将字体增加1..
对于系统字体,这也有效:
yourTextView.font = .systemFontOfSize(16)
Swift 4,5
yourTextView.font = .systemFont(ofSize: 16)
Swift 5
textView.font = UIFont.preferredFont(forTextStyle: .body) //.title, .headline, etc..
SWIFT 5
你也可以
yourTextView.font = UIFont.systemFont(ofSize: CGFloat)
我想在我的应用程序中添加两个按钮来设置 UITextview 的字体大小,我发现了这个功能
textview.font.increaseSize(...) //and decreaseSize(...)
但是我不明白括号里是什么,我想把字体大小增加和减少一个点
感谢您的回答
我认为没有名为 increaseSize()
的方法。可能是您找到了一些 UIFont
或 UITextView
类别。
official UIFont
class 文档没有透露任何此类方法。
另外你可以这样增加字体:
textview.font = UIFont(name: textview.font.fontName, size: 18)
上面的语句只是将现有的字体大小设置为18,更改为任何你想要的。
但是如果你想要像你发布的那样的方法,你可以像这样介绍你自己的类别:
extension UITextView {
func increaseFontSize () {
self.font = UIFont(name: self.font.fontName, size: self.font. pointSize+1)!
}
}
Swift 2 & 3:
import UIKit
extension UITextView {
func increaseFontSize () {
self.font = UIFont(name: (self.font?.fontName)!, size: (self.font?.pointSize)!+1)!
}
}
然后只需将它导入到您想要使用的任何地方,就像这样:
textview.increaseFontSize()
每次调用它都会将字体增加1..
对于系统字体,这也有效:
yourTextView.font = .systemFontOfSize(16)
Swift 4,5
yourTextView.font = .systemFont(ofSize: 16)
Swift 5
textView.font = UIFont.preferredFont(forTextStyle: .body) //.title, .headline, etc..
SWIFT 5
你也可以
yourTextView.font = UIFont.systemFont(ofSize: CGFloat)