如何实现 UIKit: SF Pro Rounded?
How to implement UIKit: SF Pro Rounded?
求助,我四处寻找,找不到实现这个的解决方案; Rounded, The rounded variant of the default typeface.
这是示例代码
//: 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()
谢谢
使用原始答案 () 中的代码,您可以像这样在代码中使用它:
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
}
但是当你想在多个地方使用它时,如果你能重用它会更好...所以例如我创建了 class 称为 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)
求助,我四处寻找,找不到实现这个的解决方案; Rounded, The rounded variant of the default typeface.
这是示例代码
//: 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()
谢谢
使用原始答案 (
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
}
但是当你想在多个地方使用它时,如果你能重用它会更好...所以例如我创建了 class 称为 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)