Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Used to Swiftui #100

Open
reinaldoriant opened this issue Sep 5, 2021 · 1 comment
Open

Used to Swiftui #100

reinaldoriant opened this issue Sep 5, 2021 · 1 comment

Comments

@reinaldoriant
Copy link

Do you have example to use in Swiftui?

@Shouheng88
Copy link

I have one, just as an example,

import Foundation
import SwiftUI
import MarkdownKit

struct GPTDescView: View {
    
    @State private var text: NSAttributedString = NSAttributedString(string: "")

    @Binding var gptInfo: GptWordVo
    
    var body: some View {
        VStack(spacing: 0) {
            DialogTitleView(title: "AI 拓展 - 单词信息", rightImage: nil)
                .padding(.vertical, 8)
            ShadowScrollView {
                Text(AttributedString(text))
                    .font(.system(size: 15))
                    .foregroundColor(Color.text_color_primary)
                    .padding(.horizontal, 15)
                    .padding(.vertical, 15)
                    .textSelection(.enabled)
            }
        }.onChange(of: gptInfo) { value in
            renderText()
        }.onAppear {
            renderText()
        }
    }
    
    private func renderText() {
        let markdownParser = MarkdownParser(
            font: UIFont.systemFont(ofSize: 16),
            color: .textColorPrimary
        )
        let explanation = GPTWordsManager.fixExplanation(
            explanation: gptInfo.explanation ?? "")
        markdownParser.enabledElements = .disabledAutomaticLink
        markdownParser.header.font = UIFont.systemFont(ofSize: 16)
        markdownParser.header.fontIncrease = 1
        self.text = markdownParser.parse(explanation)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants