0

everyone. I am trying to show NSAttributedString in SwiftUI. Since SwiftUI doesn't have any controls capable of it, I am trying to use UIKit controls, wrapped in UIViewRepresentable. I've tried to use UILabel, but it doesn't handle hyperlinks. I've tried to use UITextView (isScrollEnabled = false) but in this case text isn't broken into lines (see screenshot). When isScrollEnabled is set to true, UITextView is displayed correctly, but I don't want any scroll.

I would be very glad to hear any suggestions.enter image description here

Update: I get text from the server and I need to apply different format to different (but often intersecting!) parts of text. For example: Characters [0, 10] should be bold, characters [5, 10] should be a link, etc. I'd hate to do the parsing manually and check that no part of text is repeated. NSAttributedString has a method string.addAttribute(.font, value: font, range: NSRange(location: 0, length: 10))

Update 2: We support iOS 14+

DmitryZ
  • 81
  • 1
  • 4
  • 1
    "Since SwiftUI doesn't have any controls capable of it" How about `Text`? – Sweeper Feb 21 '22 at 13:04
  • For iOS 13 and 14, you can check out [my repo](https://github.com/Orijhins/SwiftUIPlus). It was made with macOS in mind, but your implementation of my PlusTextView (tags aside) should be close – Orijhins Feb 21 '22 at 13:12
  • https://stackoverflow.com/questions/59531122/how-to-use-attributed-string-in-swiftui – ChrisR Feb 21 '22 at 13:12
  • **Found my problem solved [here](https://gonzalezreal.github.io/2020/01/31/rendering-attributed-strings-in-swiftui.html)** – DmitryZ Feb 22 '22 at 14:59

0 Answers0