首页 > 解决方案 > 如何在 UITextview 中通过自动换行 NSTextAttachment 换行

问题描述

我使用 UITextview 并在其上插入 NSTextAttachment 作为图像。该 Textview 只有附件图像,没有任何普通文本。如何像普通文本一样通过自动换行来换行。我设置了 textContainer 的 lineBreakMode 属性但不起作用。我将不胜感激您的任何帮助。

 func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        return true
   
        for data in arrDataModel {
              if data.textDefaul == text {
                  let attachment = NSTextAttachment()
                  if let image = data.imageData {
                    
                      let imageNew = UIImage(data: image)!
                      attachment.image =  imageNew.colorImage(with: colorDefault)
                  }else{
                    return true
                  }
                let attString = NSAttributedString(attachment: attachment)
                var attributedString :NSMutableAttributedString!
                attributedString = NSMutableAttributedString(attributedString:textView.attributedText)

                attributedString.insert(attString, at: textView.selectedRange.location)
                textView.attributedText = attributedString
                textView.selectedRange.location += 1
                return false
              }
          }

    
        return true
      }

标签: iosswifttextuitextviewnstextattachment

解决方案


推荐阅读