0

Part of the App I'm building is a chat screen. I'm displaying chat messages as UITableViewCells that consist of simple UILables (holding the text) and a surrounding UIView (to display text within a little speech bubble). All pretty straight forward.

Chat message text I'm getting from the backend is html and can also include links. I would basically like to achieve 2 things:

  1. html formatting - <ul>, <li>, etc.
  2. I would like to make included hyperlinks clickable

I've found some solutions that either achieve one or the other - mostly via NSAttributedStrings and NSMutableAttributedStrings. As the problem doesn't seem to be very exotic, I thought I'd ask if anyone out there has found or established a way to get both done in one go - i.e. a function that takes a string and spits out an attribted string with clickable links and html attritubes.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Bernhard Engl
  • 243
  • 1
  • 2
  • 12
  • It sounds like a web view. But yes, you can convert html to an attributed string and use a text view instead. – matt Mar 27 '20 at 11:10
  • Does this answer your question? [Convert HTML to NSAttributedString in iOS](https://stackoverflow.com/questions/4217820/convert-html-to-nsattributedstring-in-ios) – Willeke Mar 27 '20 at 11:10
  • @Willeke: Thanks for your input but that only solves part 1 of the issue again. Was looking for something that solves both problems. – Bernhard Engl Mar 27 '20 at 13:41
  • @matt: Thanks, but wouldn't that only solve 1 and still leave me with 2 unsolved? – Bernhard Engl Mar 27 '20 at 13:43
  • No, links can be clickable in a text view. – matt Mar 27 '20 at 13:54
  • @matt: I see. So attributed text held by a UILabel would not be clickable? That's the problem I'm having right now. Just trying to understand if that's what causing it and me going for a UITextView would solve it. – Bernhard Engl Mar 27 '20 at 14:19
  • Yes, but what I'm telling you is that you can make a text view that looks and behaves _exactly like a label_ except for the fact that the link is clickable. – matt Mar 27 '20 at 14:22
  • @matt: OK, thanks, that wasn't clear to me. I've tried that but end up with some display issues as a result. Realise I need to share a bit more information for anyone to delve into this, so I've created this: https://stackoverflow.com/questions/60890289/layout-problems-after-replacing-uilabel-with-uitextview-in-a-uitableviewcell As you seem to have some expertise here specifically, I was hoping you might take a look. Thanks in advance!!! – Bernhard Engl Mar 27 '20 at 16:37
  • At 2:32 https://developer.apple.com/videos/play/wwdc2018/221/?time=152 prefers the UITextView since you have interaction. – Larme Mar 28 '20 at 10:22

0 Answers0