如何在我绘制文本的自定义视图中使 link、phone 数字可点击(与文本视图中的行为相同)?

How to make link, phone number clickable (Same behaviour as in textview) in a custom view where i draw text?

我制作了一个视图并在其上绘制文本,我希望如果任何文本包含 link(Hyperlink) 或 Phone Number 它将是可点击(与文本视图中的行为相同)那么如何实现呢?

我在其中绘制文本的视图代码:-

class DrawRectCellView: UIView {

    var text: NSAttributedString?

    override init(frame: CGRect) {
        super.init(frame: frame)

    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // Only override drawRect: if you perform custom drawing.

    override func draw(_ rect: CGRect)
    {
        UIColor.white.setFill()
        UIGraphicsGetCurrentContext()?.fill(rect)

        // Drawing code

        if let attributedText = text {
            attributedText.draw(in: rect)
        }
    }

}

TableCell 代码:-

class DrawRectCell: UITableViewCell {

    var cellView: DrawRectCellView?

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        // Initialization code
        cellView = DrawRectCellView(frame: self.frame)

        if let cell = cellView {
            cell.autoresizingMask = UIViewAutoresizing(rawValue: UIViewAutoresizing.RawValue(UInt8(UIViewAutoresizing.flexibleWidth.rawValue) | UInt8(UIViewAutoresizing.flexibleHeight.rawValue)))
            cell.contentMode = UIViewContentMode.redraw
        }

        self.contentView.addSubview(cellView!)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setTextString(_ text: NSAttributedString) {
        if let view = cellView {
            view.text = text
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}

我正在设置文本 = www.google.com 或任何 phone 数字它仅显示为普通文本(不像在文本视图中那样显示(它使其可点击))

您可以在DrawRectCellView 上添加UITapGestureRecognizer 并将IndexPath 设置为标签。在 UITapGestureRecognizer 的选择器方法中,您可以获取包含文本的点击的 contentView 的子视图信息。

要突出显示 TextView 中的某些文本:

假设您有 textView textView,那么您可以使用此代码来突出显示 URL、phone 数字等:

textView.dataDetectorTypes = [] // doesn't work if textfield isEditable is set to true
textView.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.blue] // the default is blue anyway

如果您只想包含某些要检测的特定数据类型,请像这样将它们添加到数组中:

textView.dataDetectorTypes = [.link]

这是类型列表:

https://developer.apple.com/documentation/uikit/uidatadetectortypes

要使整个视图可点击:

您可以向视图添加 UITapGestureRecognizer(如果不包含 phone 数字或超链接,则不添加),如下所示:

此答案可能未使用您正在使用的 Swift 版本。如果是这样,编译器会告诉你如何改变它,所以它会起作用。

如果您不喜欢使用选择器,我建议您使用库 Closures https://github.com/vhesener/Closures, specifically with this https://vhesener.github.io/Closures/Extensions/UITapGestureRecognizer.html

首先您需要检测您的文本是否包含 url 或这样的数字。

let input = "This is a test with the URL https://www.hackingwithswift.com to be detected."
let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
let matches = detector.matches(in: input, options: [], range: NSRange(location: 0, length: input.utf16.count))

for match in matches {
    guard let range = Range(match.range, in: input) else { continue }
    let url = input[range]
    print(url)
}

如果在设置为文本视图后检测到 url,则需要像这样在 UITextView 上添加 UITapGestureRecognizer

let tapGesture = UITapGestureRecognizer(target: self, action: "handleTap")  

textview.addGestureRecognizer(tapGesture)

func handleTap(sender: UITapGestureRecognizer) {

    if sender.state == .began {
      //write your code here for url tap 
    }
}