美文网首页
富文本实现图文混排和文本点击事件

富文本实现图文混排和文本点击事件

作者: ImmortalSummer | 来源:发表于2020-08-31 11:02 被阅读0次

图文混排和文本点击事件代码和效果图如下:

import UIKit

class ViewController: UIViewController {

    var textView = UITextView.init()
    var isSelected = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 富文本实现图文混排
        let label = UILabel.init(frame: CGRect.init(x: 0, y: 100, width: 0, height: 60))
        self.view.addSubview(label)
        label.textColor = UIColor.red
        label.backgroundColor = UIColor.green
        
        let textAttachment = NSTextAttachment.init()
        textAttachment.image = UIImage.init(named: "weixiao")
        textAttachment.bounds = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: 24, height: 24))
        let imageStr = NSAttributedString.init(attachment: textAttachment)
        let abs = NSMutableAttributedString.init(string: "你好")
        abs.append(imageStr)
        abs.append(NSMutableAttributedString.init(string: "!!!"))
        label.attributedText = abs
        
        let w = label.sizeThatFits(CGSize.init(width: Double(MAXFLOAT), height: 60)).width
        let x = (UIScreen.main.bounds.width-w) * 0.5
        
        label.frame = CGRect.init(x: x, y: 100, width: w, height: 60)
        
        // 富文本实现跳转
        textView = UITextView.init(frame: CGRect.init(x: 40, y: 200, width: UIScreen.main.bounds.width-80, height: 100))
        textView.delegate = self
        textView.textColor = UIColor.black
        self.view.addSubview(textView)
        
        textView.attributedText = self.getProtocalText(isSelected: isSelected)
        textView.isEditable = false
        textView.isScrollEnabled = false
        
    }
    
    func getProtocalText(isSelected:Bool) -> NSMutableAttributedString {
        let tip = "我已阅读《支付宝协议》和《微信协议》, 并遵守这些协议以继续使用本app"
        let tipAttr = NSMutableAttributedString.init(string: tip)
        tipAttr.addAttribute(.link, value: "zhifubao://", range: NSRange.init(location: 4, length: 7))
        tipAttr.addAttribute(.link, value: "weixin://", range: NSRange.init(location: 12, length: 6))
        
        let image = isSelected ? UIImage.init(named: "选择") : UIImage.init(named: "未选择")
        let textAttachment = NSTextAttachment.init()
        textAttachment.image = image
        textAttachment.bounds = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: 16, height: 16))
        let imageAttributedStr = NSMutableAttributedString.init(attachment: textAttachment)
        imageAttributedStr.addAttribute(.link, value: "selectbox://", range: NSRange.init(location: 0, length: imageAttributedStr.length))
        
        tipAttr.insert(imageAttributedStr, at: 0)
        
        return tipAttr
    }

}

extension ViewController : UITextViewDelegate{
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        
        //print(URL.scheme)
        if URL.scheme == "zhifubao" {
            let alert = UIAlertController.init(title: "提示", message: "支付宝协议如下:巴拉巴拉巴拉...", preferredStyle: .alert)
            alert.addAction(UIAlertAction.init(title: "确定", style: .cancel, handler: nil))
            self.present(alert, animated: true, completion: nil)
            return false
        } else if URL.scheme == "weixin" {
            let alert = UIAlertController.init(title: "提示", message: "微信协议如下:巴拉巴拉巴拉...", preferredStyle: .alert)
            alert.addAction(UIAlertAction.init(title: "确定", style: .cancel, handler: nil))
            self.present(alert, animated: true, completion: nil)
            return false
        } else if URL.scheme == "selectbox" {
            isSelected = !isSelected
            textView.attributedText = self.getProtocalText(isSelected: isSelected)
            return false
        }
        
        return true
    }
}


效果.png

相关文章

网友评论

      本文标题:富文本实现图文混排和文本点击事件

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