iOS17适配指南之UIFont.TextStyle、UITextField

2023-07-09 14:56:47 浏览数 (1)

UIFont.TextStyle

增加了新的超大字体样式extraLargeTitleextraLargeTitle2

代码语言:javascript复制
import UIKit

class ViewController: UIViewController {
    lazy var label1: UILabel = {
        let label = UILabel(frame: CGRect(x: 0, y: 100, width: UIScreen.main.bounds.width, height: 100))
        label.text = "WWDC 23 推出了 Xcode 15,发布了 iOS 17。"
        label.numberOfLines = 0
        label.font = .preferredFont(forTextStyle: .body)
        label.adjustsFontForContentSizeCategory = true
        return label
    }()
    lazy var label2: UILabel = {
        let label = UILabel(frame: CGRect(x: 0, y: 200, width: UIScreen.main.bounds.width, height: 100))
        label.text = "WWDC 23 推出了 Xcode 15,发布了 iOS 17。"
        label.numberOfLines = 0
        label.font = .preferredFont(forTextStyle: .extraLargeTitle2)
        label.adjustsFontForContentSizeCategory = true
        return label
    }()
    lazy var label3: UILabel = {
        let label = UILabel(frame: CGRect(x: 0, y: 300, width: UIScreen.main.bounds.width, height: 150))
        label.text = "WWDC 23 推出了 Xcode 15,发布了 iOS 17。"
        label.numberOfLines = 0
        label.font = .preferredFont(forTextStyle: .extraLargeTitle)
        label.adjustsFontForContentSizeCategory = true
        return label
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(label1)
        view.addSubview(label2)
        view.addSubview(label3)
    }
}

UIFont.TextStyle

UITextField

增加了birthdatecreditCard相关的 textContentType(输入时可以根据设置的类型提供自动填充的选项)。

代码语言:javascript复制
import UIKit

class ViewController: UIViewController {
    lazy var birthdayTF: UITextField = {
        let textfield = UITextField(frame: CGRect(x: 100, y: 100, width: 200, height: 34))
        textfield.borderStyle = .roundedRect
        textfield.textContentType = .birthdate
        return textfield
    }()
    lazy var cardTF: UITextField = {
        let textfield = UITextField(frame: CGRect(x: 100, y: 150, width: 200, height: 34))
        textfield.borderStyle = .roundedRect
        textfield.textContentType = .creditCardNumber
        return textfield
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(birthdayTF)
        view.addSubview(cardTF)
    }
}

0 人点赞