简体   繁体   中英

How to add custom actions to attributed text in UITextView?

I have an attributed string. I'd like to add custom actions for the highlighted attributed text. Which is dataModel.user & dataModel.object How would I go about doing this?

My Attributed Text is;

var title: NSAttributedString {
    let highlighted: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16, weight: .semibold),
                                                   NSAttributedString.Key.foregroundColor: UIColor.label]
    let regular: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16, weight: .regular),
                                                   NSAttributedString.Key.foregroundColor: UIColor.gray]
    let title = NSMutableAttributedString(string: dataModel.user, attributes: highlighted)
    let added = NSMutableAttributedString(string: " added ", attributes: regular)
    let object = NSMutableAttributedString(string: dataModel.object, attributes: highlighted)
    title.append(added)
    title.append(object)
    return title
}

You can simply set your view controller as the delegate of your UITextView, add link attributes to the user and object strings with a path/name using an url and switch that url last path component inside UITextView shouldInteractWith method.


struct DataModel {
    let user: String
    let object: String
}

import UIKit
class ViewController: UIViewController, UITextViewDelegate {
    var dataModel: DataModel!
    @IBOutlet weak var textView: UITextView!
    override func viewDidLoad() {
        super.viewDidLoad()
        textView.delegate = self
        dataModel = .init(user: "Steve", object: "Jobs")
        textView.attributedText = { 
            let title = NSMutableAttributedString(string: dataModel.user, attributes: [
                .font: UIFont.systemFont(ofSize: 16, weight: .semibold),
                .foregroundColor: UIColor.label,
                .link: URL(fileURLWithPath: "user")])
            let added = NSMutableAttributedString(string: " added ", attributes: [
                .font: UIFont.systemFont(ofSize: 16, weight: .regular),
                .foregroundColor: UIColor.gray])
            let object = NSMutableAttributedString(string: dataModel.object, attributes: [
                .font: UIFont.systemFont(ofSize: 16, weight: .semibold),
                .foregroundColor: UIColor.label,
                .link: URL(fileURLWithPath: "object")])
            title.append(added)
            title.append(object)
            return title
        }()
    }
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        switch URL.lastPathComponent {
        case "user":    print("user action")
        case "object":  print("object action")
        default: break
        }
        return true
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM