cjwirth / RichEditorView

RichEditorView is a simple, modular, drop-in UIView subclass for Rich Text Editing.
BSD 3-Clause "New" or "Revised" License
1.89k stars 445 forks source link

The height of Editor does not decrease when I remove newlines #249

Closed Abdelilahskali closed 3 years ago

Abdelilahskali commented 3 years ago

Firstly thank you for this API I integrated the editor in a uitableviewcell and I increase the height of the cell from the delegate function heightDidChange. But when I remove the newlines the height always stays the same, the heightDidChange is not called is there a way to know the new editor height if i delete some text (newlines) to decrease cell height ?

BarMalka commented 3 years ago

@Abdelilahskali I can see you closed this issue. Did you solve this issue? If so, can you share the solution please?

IshuRocks commented 1 year ago

@Abdelilahskali can you please share solution.

IshuRocks commented 1 year ago

This solution work for me

Update code in "RichEditorView"

private func updateHeight() {

    runJS("RE.getHtml()") { content in

        let attributedString = NSMutableAttributedString(string: content,
                                                         attributes: [
                                                             .font: UIFont.systemFont(ofSize: 16.0)
                                                         ])

        let stringToMeasure = attributedString.string.replacingOccurrences(of: "<br>", with: "\n")
        let modifiedAttributedString = NSAttributedString(string: stringToMeasure, attributes: [
            .font: UIFont.systemFont(ofSize: 16.0)
        ])

        // Calculate the bounding rect
        let maxWidth: CGFloat = UIScreen.main.bounds.width - 30
        let options: NSStringDrawingOptions = [.usesLineFragmentOrigin, .usesFontLeading]
        let boundingRect = modifiedAttributedString.boundingRect(with: CGSize(width: maxWidth, height: .greatestFiniteMagnitude), options: options, context: nil)

        // Extract the calculated height
        let height = ceil(boundingRect.height)
        self.editorHeight = Int(height)
        print(height)

    }
}