3

I use TextView to display a photo, everything works fine, but why, on iPhone, plus the version does not display text with photos, who knows what could be the reason?

override func viewDidLoad() {
        super.viewDidLoad()

         textView.attributedText = detail?.text?.html2AttributedString
         textView.attributedText.correctimage(textView: text, SV: view)
    }

downloads photos and processes html text

extension String {
var html2AttributedString: NSAttributedString? {
    do {
        return try NSAttributedString(data: Data(utf8), options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
    } catch {
        print("error:", error)
        return nil
    }


}
var html2String: String {
    return html2AttributedString?.string ?? ""
}



}

Find photos in the text and set them the size

extension NSAttributedString {
func correctimage(textView:UITextView, SV:Any){

    textView.font = UIFont(name: "roboto-light", size: 19)

    textView.attributedText.enumerateAttribute(NSAttributedStringKey.attachment, in: NSRange(location: 0, length: textView.attributedText.length), options: [], using: {(value,range,stop) -> Void in
        if (value is NSTextAttachment) {
            let attachment: NSTextAttachment? = (value as? NSTextAttachment)

            if ((attachment?.image) != nil) {

                attachment?.bounds.origin = CGPoint(x:  (SV as AnyObject).frame.size.width - 20, y: 20)
                attachment?.bounds.size = CGSize(width: (SV as AnyObject).frame.size.width - 25, height: 250)

            }
        }
    })

}

EDIT:

my code works on all iphone models but not on the emulator with plus models

Vasya2014
  • 333
  • 3
  • 20
  • Maybe helpful links: https://stackoverflow.com/a/17744938/3883492 ; https://stackoverflow.com/a/46184150/3883492 . Are you sure the text is just not too long, and textView could not render it? – dvp.petrov Sep 26 '17 at 13:40
  • @dvp.petrov on iphone5,6,7 all displayed – Vasya2014 Sep 26 '17 at 13:51

1 Answers1

2

Check this out, My side working for me

func convertToInlineImageFormat(htmlStrr:String) -> NSMutableAttributedString {

        let htmlStringgg = htmlStrr as String
        let content = try! NSMutableAttributedString(
            data: htmlStringgg.data(using: String.Encoding.unicode, allowLossyConversion: true)!,
            options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
            documentAttributes: nil)
        //        content.addAttribute(NSForegroundColorAttributeName, value: UIColor.white, range: NSRange(location: 0, length: htmlStrr.length))

        // Resizing Inline images
        content.enumerateAttribute(NSAttachmentAttributeName, in: NSMakeRange(0, content.length), options: NSAttributedString.EnumerationOptions.init(rawValue: 0), using: { (value, range, stop) -> Void in
            if let attachement = value as? NSTextAttachment {
                let image = attachement.image(forBounds: attachement.bounds, textContainer: NSTextContainer(), characterIndex: range.location)
                let screenSize: CGRect = UIScreen.main.bounds
                if (image?.size.width)! > screenSize.width - 2 {
                    let newImage = image?.resizeImage(scale: (screenSize.width - 2)/(image?.size.width)!)
                    let newAttribut = NSTextAttachment()

                    newAttribut.image = newImage
                    content.addAttribute(NSAttachmentAttributeName, value: newAttribut, range: range)

                }
            }
            // ARTICLE DESCRIPTION FONT
            //            let replacementFont = Contants.descFont

            let fontSizeDescribtion = UserDefaults.standard.float(forKey: "FontSize")
            var fontSize :Float = 0.0
            if fontSizeDescribtion == 0{
                fontSize = 21
            }else{
                fontSize = Float(fontSizeDescribtion)
            }
            let fontDesc =  UIFont(name: Contants.abiramiFont, size: CGFloat(Float(fontSize)))

            content.addAttribute(NSFontAttributeName, value: fontDesc!, range: NSRange(location: 0, length: content.length))
            content.addAttribute(NSForegroundColorAttributeName, value: UIColor.black, range: NSRange(location: 0, length: content.length))

        }) // Content block

        return content
    }

Use like below:

 DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
            self.descriptionTextView.attributedText = self.convertToInlineImageFormat(htmlStrr: currentData["ArticleXML"] as! String)
        }
karthikeyan
  • 3,626
  • 1
  • 13
  • 22