如何在标签中添加按钮?

How add button in label?

我需要在文本末尾添加一个按钮来触发警报。文本可以分为几行 我该怎么做?

它应该是这样的

您可以使用NSTextAttachment在文本末尾添加image

let fullString = NSMutableAttributedString(string: "Text")

// Create our NSTextAttachment
let imageAttachment = NSTextAttachment()
imageAttachment.image = UIImage(named: "awesomeIcon.png")

// Wrap the attachment in its own attributed string so we can append it
let imageString = NSAttributedString(attachment: imageAttachment)

// Add the NSTextAttachment
fullString.append(imageString)

// Draw the result in a label
yourLabel.attributedText = fullString

然后添加UITapGestureRecognizer点击后触发动作。

let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(showAlert(tapGesture:)))
yourLabel.addGestureRecognizer(tapGesture)

@objc func showAlert(tapGesture: UITapGestureRecognizer){
    // Show alert
}

基于响应

let fullString = NSMutableAttributedString(string: "")
let imageAttachment = NSTextAttachment()
imageAttachment.image = UIImage(named: "awesomeIcon.png")
let imageString = NSAttributedString(attachment: imageAttachment)

fullString.append(NSAttributedString(string: " ", attributes: nil))
fullString.append(imageString)
fullString.append(NSAttributedString(string: " ", attributes: nil))

self.text.isUserInteractionEnabled = true

let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(showBlurbMessage(tapGesture:)))
self.text.addGestureRecognizer(tapGesture!)

在 showBlurbMessage 中

 @objc func showBlurbMessage(tapGesture: UITapGestureRecognizer) {
    let location = (text.text?.count ?? 0)
    if tapGesture.didTapAttributedTextInLabel(label: self.text, inRange: NSRange(location: location - 2, length: 1)) {

        let alert = UIAlertController.createOkAlert(WithTitle: "", message: assessment.blurbDescription, okTitle: "Close")
        self.present(alert, animated: true, completion: nil)
    }
}

UITapGestureRecognizer

的扩展
extension UITapGestureRecognizer {

func didTapAttributedTextInLabel(label: UILabel, inRange targetRange: NSRange) -> Bool {
    // Create instances of NSLayoutManager, NSTextContainer and NSTextStorage
    let layoutManager = NSLayoutManager()
    let textContainer = NSTextContainer(size: CGSize.zero)
    let textStorage = NSTextStorage(attributedString: label.attributedText!)

    // Configure layoutManager and textStorage
    layoutManager.addTextContainer(textContainer)
    textStorage.addLayoutManager(layoutManager)

    // Configure textContainer
    textContainer.lineFragmentPadding = 0.0 // 0.0 - left, 0.5 - center, 1 - right
    textContainer.lineBreakMode = label.lineBreakMode
    textContainer.maximumNumberOfLines = label.numberOfLines
    let labelSize = label.bounds.size
    textContainer.size = labelSize

    // Find the tapped character location and compare it to the specified range
    let locationOfTouchInLabel = self.location(in: label)
    let textBoundingBox = layoutManager.usedRect(for: textContainer)
    let textContainerOffset = CGPoint(
        x: (labelSize.width - textBoundingBox.size.width) * 0.5 - textBoundingBox.origin.x,
        y: (labelSize.height - textBoundingBox.size.height) * 0.5 - textBoundingBox.origin.y
    )
    let locationOfTouchInTextContainer = CGPoint(
        x: locationOfTouchInLabel.x - textContainerOffset.x,
        y: locationOfTouchInLabel.y - textContainerOffset.y
    )
    let indexOfCharacter = layoutManager.characterIndex(for: locationOfTouchInTextContainer, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)

    return NSLocationInRange(indexOfCharacter, targetRange)
}
}