1

How can I remove or hide HTML tags from the text so that links are not removed? I tried a lot of ways, the most effective at the bottom. but they do not fit, I now use html2AttributedString and it fits almost all the criteria except one I did not find a way to resize the photo. Who knows how to remove all tags from the text except the links? I will be very thankful for the solution.

example string:

<p class=\"entry\">\r\n\t 24-й Форум книгоиздателей Украины во Львове определил лауреатов конкурса «Лучшая книга-2017».\r\n</p>\r\n<p>\r\n\t В номинации «Город – Киев» победила книга «Бабий Яр: история и память».
<img width=\"600\" alt=\"№39---11.jpg\" src=\"http://---.com/upload/medialibrary/35f/35f50dc1765f977140baa604c30be7a.jpg\" height=\"465\" title=\"№39---11.jpg\"> \r\n</p>\r\n<p>\r\n\t
Сборник статей под редакцией Вячеслава Гриневича и Павла-Роберта Магочия стал результатом сотрудничества ученых из разных стран – Украины, Канады, Нидерландов, США, Израиля, Франции, которых объединило осознание важности сохранения памяти о Бабьем Яре как символе Холокоста и других трагедий XX столетия.\r\n</p>\r\n<p>\r\n\t Книга вышла в свет в киевском издательстве «Дух и Литера».\r\n</p> 
<br>Источник: <a href=http://---.org/page16/news58701.html>Сайт</a>`

code:

extension String {
    var html2AttributedString: NSAttributedString? {
        do {
            return try NSAttributedString(data: Data(utf8), options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)
        } catch {
            print(error)
            return nil
        }
    }
    var html2String: String {
        return html2AttributedString?.string ?? ""
    }       
}

override func viewDidLoad() {
    super.viewDidLoad()
    textView.attributedText = detail?.text?.html2AttributedString
    textView.font = UIFont(name: "roboto-light", size: 19)
}
Vasya2014
  • 203
  • 1
  • 5
  • 25
  • Do you have a sample example of the string? Because it's unclear what are images (links?) are links links with the correct tags ` – Larme Sep 19 '17 at 14:18
  • @LarmeI added an example to the question – Vasya2014 Sep 20 '17 at 07:47
  • I read more carefully your question. What you have to do after (in pseudo code) `let attributedStr = detail?.text?.html2AttributedString.mutableCopy; attributedStr.addAttribute(NSFontAttributeName, value:UIFont(name: "roboto-light", size: 19), range:wholeRange);`, then look there https://stackoverflow.com/questions/31945236/change-size-of-nstextattachment-image for enumerating the images and resizing them (it should be easily translated into Swift). – Larme Sep 20 '17 at 08:11
  • @Larme I probably incorrectly wrote a question, there is no problem with the image in the function `imageText`, I use it without actions on the HTML tag. the problem occurs with `html2AttributedString` it displays the photos too large and I can not change the size – Vasya2014 Sep 20 '17 at 08:32
  • 2
    You need to iterate the NSAttributedString (a mutable version) looking for NSTextAttachment and resize them like in the linked question. Here is a Swift Sample enumeration for images: https://stackoverflow.com/questions/29152660/extract-uiimage-from-nsattributed-string/29153172#29153172 just change the size – Larme Sep 20 '17 at 08:33
  • @Larme thanks, you really helped me – Vasya2014 Sep 20 '17 at 11:56

0 Answers0