Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 识别标签中显示的属性字符串中的图像点击_Ios_Swift_Xcode_Uilabel_Nsmutableattributedstring - Fatal编程技术网

Ios 识别标签中显示的属性字符串中的图像点击

Ios 识别标签中显示的属性字符串中的图像点击,ios,swift,xcode,uilabel,nsmutableattributedstring,Ios,Swift,Xcode,Uilabel,Nsmutableattributedstring,我有一个属性字符串,如下所示,将显示在标签中 let someText = NSMutableAttributedString(string: "This is a sample text with") let imageAttachment = NSTextAttachment() imageAttachment.image = UIImage(named: "icon") let imageString = NSAttributedString(attachment: imageAtta

我有一个属性字符串,如下所示,将显示在标签中

let someText = NSMutableAttributedString(string: "This is a sample text with")
let imageAttachment = NSTextAttachment()
imageAttachment.image = UIImage(named: "icon") 

let imageString = NSAttributedString(attachment: imageAttachment)
someText.append(imageString)
someText.append(NSAttributedString(string: "attached")
somelabel.attributedText = someText
标签显示
这是一个附有“图像”的示例文本

如何识别点击图像(而不是文本)执行操作?

  • 创建一个新的NSAttributedStringKey,用于标识图像附件
  • 然后使用图像创建一个NSTextAttachment,将其包装在一个NSMutableAttributeString中,并向其中添加自定义属性
  • 最后,将包装器添加到完整的NSAttributedString,并附加一个UITapgestureRecognitor

  • 然后,在UITapGestureRecognizer上的选择器中,只需查找该自定义标记

most位的代码:

extension NSAttributedStringKey {
static let imagePath = NSAttributedStringKey(rawValue: "imagePath")
}
何时设置文本显示

let fullString = NSMutableAttributedString()    
let imageAttachment = NSTextAttachment()
imageAttachment.image = image

let imageAttributedString: NSMutableAttributedString = NSAttributedString(attachment: imageAttachment).mutableCopy() as! NSMutableAttributedString

let customAttribute = [ NSAttributedStringKey.imagePath: imagePath ]
imageAttributedString.addAttributes(customAttribute, range: NSRange(location: 0, length: imageAttributedString.length))

fullString.append(imageAttributedString)
然后在点击操作调用的函数中:

    @objc func onImageTap(_ sender: UITapGestureRecognizer) {
      let textView = sender.view as! UITextView
      let layoutManager = textView.layoutManager

      // location of tap in textView coordinates
      var location = sender.location(in: textView)
      location.x -= textView.textContainerInset.left;
      location.y -= textView.textContainerInset.top;

      // character index at tap location
      let characterIndex = layoutManager.characterIndex(for: location, in: textView.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)

      // if index is valid 
      if characterIndex < textView.textStorage.length {

        // check if the tap location has the custom attribute
        let attributeValue = textView.attributedText.attribute(NSAttributedStringKey.imagePath, at: characterIndex, effectiveRange: nil) as? String
        if let value = attributeValue {
            print("You tapped on \(NSAttributedStringKey.imagePath) and the value is: \(value)")
        }

    }

}
@objc func onImageTap(uu发送方:UITapGestureRecognitizer){
设textView=sender.view为!UITextView
让layoutManager=textView.layoutManager
//点击在文本视图坐标中的位置
var location=sender.location(在:textView中)
location.x-=textView.textContainerSet.left;
location.y-=textView.textContainerSet.top;
//抽头位置的字符索引
让characterIndex=layoutManager.characterIndex(对于:位置,在:textView.textContainer中,插入点之间距离的分数:nil)
//如果索引有效
如果characterIndex
从那里你知道点击在图像中,你有图像框内的坐标,所以你可以使用这个组合来找出点击在图像中的位置