我試圖在 SwiftUI 文本中表示分母大于 9 的分數。
我可以使用單個元素并應用偏移來實作這一點,但是隨著分數的動態變化,這會有點混亂。
有沒有辦法使用屬性文本來做到這一點?
我遇到了帶有不推薦使用的方法的 thi UIFont 擴展,并想知道是否有任何類似的東西可以與 SwiftUI 一起使用:
extension UIFont {
static func fractionFont(ofSize pointSize: CGFloat) -> UIFont {
let systemFontDesc = UIFont.systemFont(ofSize: pointSize).fontDescriptor
let fractionFontDesc = systemFontDesc.addingAttributes(
[
UIFontDescriptor.AttributeName.featureSettings: [
[
UIFontDescriptor.FeatureKey.featureIdentifier: kFractionsType,
UIFontDescriptor.FeatureKey.typeIdentifier: kDiagonalFractionsSelector,
], ]
] )
return UIFont(descriptor: fractionFontDesc, size:pointSize)
}
}
uj5u.com熱心網友回復:
UIFont與 是免費橋接的CTFont,這意味著您可以通過說將 aUIFont轉換為 a 。SwiftUI有一個初始化器,它需要一個.CTFontas CTFontFontCTFont
因此,使用fractionFont(ofSize:)您發布的方法,此操場代碼:
PlaygroundPage.current.setLiveView(
Text("The fraction 21/345 is rendered nicely.")
.font(Font(UIFont.fractionFont(ofSize: UIFont.systemFontSize) as CTFont))
.padding()
)
產生這個結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/438144.html
標籤:IOS 代码 迅捷 swiftui-文本
