TextView不显示文字

问题描述:

我使用TextView来显示照片,一切正常,但为什么在iPhone上加上版本不显示带照片的文字,谁知道可能是什么原因?TextView不显示文字

override func viewDidLoad() { 
     super.viewDidLoad() 

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

下载照片和处理HTML文本

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 ?? "" 
} 



} 

查找文本的照片,并将它们设置大小

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) 

      } 
     } 
    }) 

} 

编辑:

我的代码适用于所有iPhone模型,但不在带有plus模型的模拟器上小号

+0

也许有用的链接:https://stackoverflow.com/a/17744938/3883492; https://stackoverflow.com/a/46184150/3883492。你确定文本不是太长,textView无法渲染它吗? –

+0

@ dvp.petrov在iphone5,6,7全部显示 – Vasya2014

检查了这一点,我这边为我工作

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 
    } 

使用如下:

DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { 
      self.descriptionTextView.attributedText = self.convertToInlineImageFormat(htmlStrr: currentData["ArticleXML"] as! String) 
     } 
+0

你是什么意思流?你可以看看这里https://stackoverflow.com/questions/43252440/adjust-font-size-of-nsmutableattributedstring-proportional-to-uilabels-frame-he/43254261#43254261 – karthikeyan

+0

我改变了文本的大小,一切再次消失 – Vasya2014

+0

你有没有得到你的解决方案?用你当前的代码更新你的问题.. – karthikeyan