如何实现UIKit。SF Pro Rounded?

问题描述 投票:0回答:1

请帮助我,我找了一圈也没找到实现这个的方法。四舍五入,默认字体的圆角变体。

下面是示例代码

//: A UIKit based Playground for presenting user interface

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white

        let label = UILabel()
        label.frame = CGRect(x: 150, y: 200, width: 200, height: 32)
        label.text = "Hello World!"
        label.textColor = .black
        label.font = .systemFont(ofSize: 32, weight: .semibold)
        label.font.fontDescriptor.withDesign(.rounded) // I'm guessing this is wrong

        view.addSubview(label)
        self.view = view
    }
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

谢谢你

ios swift fonts storyboard uikit
1个回答
1
投票

好了,用原答案的代码(如何在SwiftUI中使用SF圆角字体?),你可以在你的代码中像这样使用它,但是当你想在多个地方使用它的时候,如果你能重复使用它就更好了。

    override func loadView() {
    let view = UIView()
    view.backgroundColor = .white

    let label = UILabel()
    label.frame = CGRect(x: 150, y: 200, width: 200, height: 32)
    label.text = "Hello World!"
    label.textColor = .black

    // set rounded font
    let fontSize: CGFloat = 32
    let systemFont = UIFont.systemFont(ofSize: fontSize, weight: .semibold)
    let roundedFont: UIFont
    if let descriptor = systemFont.fontDescriptor.withDesign(.rounded) {
        roundedFont = UIFont(descriptor: descriptor, size: fontSize)
    } else {
        roundedFont = systemFont
    }

    label.font = roundedFont

    view.addSubview(label)
    self.view = view
}

但是当你想在多个地方使用它的时候,如果你能重复使用它就更好了... 例如我创建了一个叫FontKit的类... 它有一个静态函数,可以给我圆角字体。就像这样。

class FontKit {

 static func roundedFont(ofSize fontSize: CGFloat, weight: UIFont.Weight) -> UIFont {
    let systemFont = UIFont.systemFont(ofSize: fontSize, weight: weight)
    let font: UIFont

    if #available(iOS 13.0, *) {
        if let descriptor = systemFont.fontDescriptor.withDesign(.rounded) {
            font = UIFont(descriptor: descriptor, size: fontSize)
        } else {
            font = systemFont
        }
    } else {
        font = systemFont
    }

    return font
}
}

然后在我的代码中,我这样使用它。

label.font = FontKit.roundedFont(ofSize: 32, weight: .semibold)
© www.soinside.com 2019 - 2024. All rights reserved.