如何在标签中添加按钮? [英] How add button in label?

查看:30
本文介绍了如何在标签中添加按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to add a button at the end of the text to trigger an alert. The text can be in several lines How can I do that?

它应该是这样的

推荐答案

基于响应 https://stackoverflow.com/a/62150640/13642906

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 中

in 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)
}
}

这篇关于如何在标签中添加按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆