如何在Swift中更改UILabel的字體大小?
您可以通過將UILabel的字體屬性設置為具有所需點大小的UIFont對象來更改Swift中UILabel的字體大小。愛掏網 - it200.com
import UIKit
class TestController: UIViewController {
private let messageLabel = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
initialSetup()
}
private func initialSetup() {
// basic setup
view.backgroundColor = .white
navigationItem.title = "UILabel"
// label customization
messageLabel.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
messageLabel.numberOfLines = 0
// adding the constraints to label
view.addSubview(messageLabel)
messageLabel.translatesAutoresizingMaskIntoConstraints = false
messageLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
messageLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 30).isActive = true
messageLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30).isActive = true
}
}
輸出
在上面的輸出中,您可以看到一個默認字體大小的標簽。愛掏網 - it200.com
這是一個改變字體大小的示例
messageLabel.font = UIFont.systemFont(ofSize: 20)
輸出
在這個例子中,messageLabel的字號被設定為20點。愛掏網 - it200.com您可以調整fontSize的值來相應地改變字號。愛掏網 - it200.com
除了改變字號之外,您還可以改變字體的加粗程度。愛掏網 - it200.com下面是一個例子:
messageLabel.font = UIFont.systemFont(ofSize: 20, weight: .semibold)
輸出結果
另一個選項是使用您的字體名稱和特定大小來創建UIFont。愛掏網 - it200.com
messageLabel.font = UIFont.init(name: "AmericanTypewriter", size: 20)
輸出結果
在上面的例子中,您已經改變了自定義字體。愛掏網 - it200.com
結論
您可以輕松地更改UILabel的字體大小。愛掏網 - it200.com使用font屬性來分配一個帶有大小的字體。愛掏網 - it200.com如果您希望,還可以指定自定義字體。愛掏網 - it200.com使用UIFont.init(name: “font_name”, size: font_size)方法來提供帶有大小的自定義字體。愛掏網 - it200.com
聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。