简体   繁体   中英

How to use Core Text in SwiftUI

I have a basic implementation of Core Text that prints Hello World in iOS, it works perfectly with 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)
    }
}

I've tried to use this Core Text with SwiftUI instead of UIKit by applying the use of UIViewRepresentable :

import SwiftUI
import CoreText

struct CTView: UIViewRepresentable {
    func makeUIView(context: Context) -> UIView {
        return UIView()
    }
    
    func updateUIView(_ uiView: UIView, context: Context) {
        
    }
}

But I couldn't figure out how to implement (or basically, override) the draw function, since we use structs instead of classes in SwiftUI.

In addition to that, how can we do the same thing for macOS?

Just wrap your previous CTView , as in

struct WrappedCTView: UIViewRepresentable {

    func makeUIView(context: Context) -> CTView {
        let view = CTView()       // << here !!
        view.isOpaque = false
        return view
    }
    
    func updateUIView(_ uiView: CTView, context: Context) {}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM