gpt4 book ai didi

fonts - 是否可以在 Interface Builder 中使用 SF Rounded

转载 作者:行者123 更新时间:2023-12-04 04:23:56 25 4
gpt4 key购买 nike

我只是想仔细检查我没有遗漏一些明显的东西:

是否可以在 Interface Builder 中使用 San Francisco 的新 Rounded 变体?

最佳答案

我目前的锻炼方式是:

import UIKit

@IBDesignable

class SFRoundedLabel: UILabel {

required init?(coder: NSCoder) {
super.init(coder: coder)

setupFont()
}

override func prepareForInterfaceBuilder() {
setupFont()
}

func setupFont() {
self.font = self.font.asSFRounded()
}
}

//https://stackoverflow.com/a/53818276/1343140
extension UIFont {
var weight: UIFont.Weight {
guard let weightNumber = traits[.weight] as? NSNumber else { return .regular }
let weightRawValue = CGFloat(weightNumber.doubleValue)
let weight = UIFont.Weight(rawValue: weightRawValue)
return weight
}

private var traits: [UIFontDescriptor.TraitKey: Any] {
return fontDescriptor.object(forKey: .traits) as? [UIFontDescriptor.TraitKey: Any]
?? [:]
}

func asSFRounded() -> UIFont {
if #available(iOS 13.0, *) {
let fontSize = self.pointSize
let systemFont = UIFont.systemFont(ofSize: fontSize, weight: self.weight)

let font: UIFont

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

return font
} else {
return self
}
}
}

extension NSMutableAttributedString {
func setFontFace(font: UIFont, color: UIColor? = nil) {
beginEditing()
self.enumerateAttribute(
.font,
in: NSRange(location: 0, length: self.length)
) { (value, range, stop) in

if let f = value as? UIFont,
let newFontDescriptor = f.fontDescriptor
.withFamily(font.familyName)
.withSymbolicTraits(f.fontDescriptor.symbolicTraits) {

let newFont = UIFont(
descriptor: newFontDescriptor,
size: font.pointSize
)
removeAttribute(.font, range: range)
addAttribute(.font, value: newFont, range: range)
if let color = color {
removeAttribute(
.foregroundColor,
range: range
)
addAttribute(
.foregroundColor,
value: color,
range: range
)
}
}
}
endEditing()
}
}

关于fonts - 是否可以在 Interface Builder 中使用 SF Rounded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58326955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com