danielsaidi / RichTextKit

RichTextKit is a Swift SDK that helps you use rich text in Swift and SwiftUI.
MIT License
814 stars 97 forks source link

Font sizing doesn't work #171

Open kovs705 opened 2 months ago

kovs705 commented 2 months ago
  1. Minimum deployment target: iOS 17.0
  2. Physical device and simulator: iOS 17.2
  3. SwiftUI

I'm trying to make a font bigger but I receive strange text in console:

UITextView 0x102839600 is switching to TextKit 1 compatibility mode because its layoutManager was accessed. 
Break on void _UITextViewEnablingCompatibilityMode(UITextView *__strong, BOOL) to debug.

my code:


import SwiftUI
import SwiftData
import RichTextKit

struct NoteView: View {

    @State private var context = RichTextContext()
    @State private var attributedNoteText: NSAttributedString
    var note: Note

    init(note: Note) {
        self.note = note
        self.attributedNoteText = NSAttributedString(string: note.text)

        RichTextEditor.standardRichTextFontSize = 50 // <- I've tried this
        self.context.fontSize = 50  // <- I've tried this
        self.context.isEditable = false // <- I've tried this and editor is still editable (maybe I'm wrong)
    }

    var body: some View {
        RichTextEditor(text: $attributedNoteText, context: context) {
            $0.setRichTextFontSize(50) // <- I've tried this
        }
            .focusedValue(\.richTextContext, context)
            .frame(maxWidth: .infinity)
            .frame(minHeight: 100)
            .richTextEditorStyle(.init(font: .preferredFont(forTextStyle: .extraLargeTitle))) // <- I've tried this
    }

result: <img src="https://github.com/danielsaidi/RichTextKit/assets/56929597/5e9f0404-8928-4e9f-ac18-b225507cee9f" width="356" height="665">

danielsaidi commented 2 months ago

Hi @kovs705

First of all, the context must be a @StateObject, not @State.

Can you fix that and see if it helps?

kovs705 commented 2 months ago
import SwiftUI
import RichTextKit

struct MyView: View {

    @StateObject var context = RichTextContext()
    @State var attributedNoteText: NSAttributedString = NSAttributedString(string: "Test text")

    var body: some View {
        RichTextEditor(text: $attributedNoteText, context: context) {
            $0.setRichTextFontSize(50)
        }
        .focusedValue(\.richTextContext, context)
        .frame(maxWidth: .infinity)
        .frame(minHeight: 100)
        .richTextEditorStyle(.init(font: .preferredFont(forTextStyle: .extraLargeTitle)))
    }
}

#Preview {
    MyView()
}

still small.. <img src="https://github.com/danielsaidi/RichTextKit/assets/56929597/db2c0f14-6507-4d5e-8f85-c36d7fe6eb30" width="356" height="665">