[英]Core Data in SwiftUI - How to use @AppStorage as a value in predicate?
[英]How to use Core Text in SwiftUI
我有一個基本的Core Text實現,它在 iOS 中打印 Hello World,它與 UIKit 完美配合。
import UIKit
import CoreText
class CTView: UIView {
override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else { return }
// Flip the coordinate system
context.textMatrix = .identity
context.translateBy(x: 0, y: bounds.size.height)
context.scaleBy(x: 1.0, y: -1.0)
let path = CGMutablePath()
path.addRect(bounds)
let attrString = NSAttributedString(string: "Hello World")
let framesetter = CTFramesetterCreateWithAttributedString(attrString as CFAttributedString)
let frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, attrString.length), path, nil)
CTFrameDraw(frame, context)
}
}
我試着通過應用使用使用這個核心文本與SwiftUI代替UIKit的UIViewRepresentable
:
import SwiftUI
import CoreText
struct CTView: UIViewRepresentable {
func makeUIView(context: Context) -> UIView {
return UIView()
}
func updateUIView(_ uiView: UIView, context: Context) {
}
}
但是我無法弄清楚如何實現(或基本上覆蓋)繪制函數,因為我們在 SwiftUI 中使用結構而不是類。
除此之外,我們如何為 macOS 做同樣的事情?
只需包裝您以前的CTView
,如
struct WrappedCTView: UIViewRepresentable {
func makeUIView(context: Context) -> CTView {
let view = CTView() // << here !!
view.isOpaque = false
return view
}
func updateUIView(_ uiView: CTView, context: Context) {}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.