美文网首页
IOS图文混排(swift)

IOS图文混排(swift)

作者: lg大大 | 来源:发表于2016-08-03 11:04 被阅读0次
      1. 创建属性字符
    let attachment = NSTextAttachment()
    attachment.image = UIImage(contentsOfFile: emoticon.pngPath!)
    let font = textView.font!
    attachment.bounds = CGRect(x: 0, y: 0, width: font.lineHeight, height: font.lineHeight)
     let attrImageStr = NSAttributedString(attachment: attachment)
    
    • 2.创建可变属性字符串
    let attrMStr = NSMutableAttributedString(attributedString: textView.attributedText)
            // 1.获取光标所在的位置
    let range = textView.selectedRange
            
            // 2.替换属性字符串
    attrMStr.replaceCharactersInRange(range, withAttributedString: attrImageStr)
            
            // 显示属性字符串
    textView.attributedText = attrMStr
            
            // 将文字的大小重置
    textView.font = font
            
            // 将光标设置回原来位置 + 1
    textView.selectedRange = NSRange(location: range.location + 1, length: 0)
    

    相关文章

      网友评论

          本文标题:IOS图文混排(swift)

          本文链接:https://www.haomeiwen.com/subject/egyqsttx.html