ios – Breaking line in customized textual content in SwiftUI


I wish to have a protracted textual content that detects routinely the hyperlinks in it, and if that’s the case, they might be clickable and in different colour. I’ve succeed to try this. Nonetheless, I’ve an issue going again to the road.
I’ve tried including “n”, add fixedSize, layoutPriority and any of these labored.

struct RichTextView: UIViewRepresentable {
var textual content: String

func makeUIView(context: Context) -> UITextView {
    let textView = UITextView()
    textView.backgroundColor = .clear
    textView.isEditable = false
    textView.isScrollEnabled = false
    textView.font = UIFont.systemFont(ofSize: 18)
    textView.dataDetectorTypes = .all
    textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: Colors.colorPrimary()]
    textView.translatesAutoresizingMaskIntoConstraints = false
    textView.textContainer.lineBreakMode = .byWordWrapping
    textView.textContainer.widthTracksTextView = true
    return textView
}

func updateUIView(_ uiView: UITextView, context: Context) {
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineSpacing = 5  // Set line spacing
    paragraphStyle.paragraphSpacing = 10  // Set paragraph spacing
    paragraphStyle.lineBreakMode = .byWordWrapping
    paragraphStyle.lineBreakStrategy = NSParagraphStyle.LineBreakStrategy()

    let attributedString = NSAttributedString(
        string: textual content,
        attributes: [
            .font: UIFont.systemFont(ofSize: 18),
            .paragraphStyle: paragraphStyle
        ]
    )

    uiView.attributedText = attributedString
    uiView.sizeToFit()
    uiView.isScrollEnabled = false
}}

So that is my customized view that detects hyperlinks , and that is how I name it

 RichTextView(textual content: description)
        .body(width: UIScreen.screenWidth, top: viewHeight)
            .padding([.leading, .trailing])
            .padding(.backside, 16)
            .multilineTextAlignment(.main)
            .lineLimit(nil)
            .layoutPriority(1)
            .lineSpacing(8)
            .fixedSize(horizontal: false, vertical: false)
            .onAppear {
                self.viewHeight = calculateHeight(for: description, width: .infinity)
            }
            .onChange(of: description) { _ in
                self.viewHeight = calculateHeight(for: description, width: .infinity)
            }

I am unsure that calculatedHeight is related however I’ll put it in any case.

non-public func calculateHeight(for textual content: String, width: CGFloat) -> CGFloat {
let textView = UITextView()
textView.textual content = textual content
textView.font = UIFont.systemFont(ofSize: 18)  // Use default system font with measurement 18

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 5  // Set line spacing
paragraphStyle.paragraphSpacing = 10  // Set paragraph spacing

let attributedString = NSAttributedString(
    string: textual content,
    attributes: [
        .font: UIFont.systemFont(ofSize: 18),
        .paragraphStyle: paragraphStyle
    ]
)
textView.attributedText = attributedString
    textView.textContainer.lineBreakMode = .byWordWrapping
    textView.textContainer.widthTracksTextView = true
textView.translatesAutoresizingMaskIntoConstraints = false
        textView.textContainer.maximumNumberOfLines = 0
let measurement = textView.sizeThatFits(CGSize(width: width, top: CGFloat.greatestFiniteMagnitude))
return measurement.top}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles