[英]How to implement UIKit: SF Pro Rounded?
Help pls, I searched around and can't find the solution to implement this;请帮忙,我四处搜索,找不到实现这个的解决方案; Rounded , The rounded variant of the default typeface.
Rounded ,默认字体的圆形变体。
Here's the sample code这是示例代码
//: 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()
Thanks谢谢
Well with the code from original answer ( How to use SF Rounded font in SwiftUI? ), you can use it in your code like this:使用原始答案中的代码( How to use SF Rounded font in SwiftUI? ),您可以在代码中使用它,如下所示:
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
}
But when you want to use it on multiple places, it would be better if you could reuse it... so for example I create class called FontKit... which has static function, which gives me rounded font.但是当你想在多个地方使用它时,如果你可以重复使用它会更好......例如,我创建了名为 FontKit 的 class ......它有 static ZC1C425268E68385D1AB5074C17A94F14,它给了我四舍五入的字体。 Like this:
像这样:
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
}
}
And then in my code I use it like this:然后在我的代码中我像这样使用它:
label.font = FontKit.roundedFont(ofSize: 32, weight: .semibold)
If you know you'll only be using this for the system font, you can simplify it into the following:如果您知道您只会将其用于系统字体,则可以将其简化为以下内容:
public extension UIFont {
var rounded: UIFont {
guard let desc = self.fontDescriptor.withDesign(.rounded)
else { return self }
return UIFont(descriptor: desc, size: self.pointSize)
}
}
and then call it like:然后这样称呼它:
let sysFont: UIFont = .systemFont(ofSize: UIFont.systemFontSize)
label.font = sysFont.rounded
Calling it on any other font will just return the same font.在任何其他字体上调用它只会返回相同的字体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.