TextView 不显示文字

Posted

技术标签:

【中文标题】TextView 不显示文字【英文标题】:TextView does not display text 【发布时间】:2017-09-26 11:59:27 【问题描述】:

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

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 型号的模拟器

【问题讨论】:

可能有用的链接:***.com/a/17744938/3883492; ***.com/a/46184150/3883492 。您确定文本不会太长,而 textView 无法呈现它吗? @dvp.petrov 在 iphone5,6,7 上全部显示 【参考方案1】:

看看这个,我这边为我工作

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)
        

【讨论】:

流量是什么意思?你可以看这里***.com/questions/43252440/… 我改变了文字的大小,一切又消失了 您找到解决方案了吗?用您当前的代码更新您的问题.. 原来你的代码和我的代码都正常工作,模拟器又失败了

以上是关于TextView 不显示文字的主要内容,如果未能解决你的问题,请参考以下文章

TextView文字描边实现

Android 文字过长TextView如何自动截断并显示成省略号

Android TextView 常见问题与使用总结

android开发使用textview调用strings.xml里面存的文字时不能显示空格和回车啊!导致排版不一样怎么办!!

Android TextView setCompoundDrawables设置图片不显示解决方案

Android TextView setCompoundDrawables设置图片不显示解决方案