【问题标题】:UIButton label detect tap on specific textUIButton 标签检测点击特定文本
【发布时间】:2017-06-06 12:34:10
【问题描述】:

我有以下 UIButton 来呈现复选框和标签;

为了创建这个,我使用了一个带有NSMutableAttributedString 的 UIButton 控件,为“条款和条件”的其余部分提供了不同的样式。

就像网页上的复选框一样,我希望用户能够点击灰色文本或图像本身来打开或关闭复选框。

但如果用户点击“条款和条件”,我希望它执行一项操作(例如加载条款 URL)。

这在 Android 中很容易做到,因为您可以使用 checkbox.setMovementMethod(LinkMovementMethod.getInstance());,但在 iOS 中,我真的很难找到一种方法来创建一个带有包含链接的标签的复选框。

以前有没有人这样做过,你是怎么做的?

【问题讨论】:

  • 这个链接可以帮助你。 stackoverflow.com/questions/20541676/…
  • 要点击标签还是切换开关时?
  • 就像在网页上工作一样——点击“标签”也会标记复选框——除非用户点击“条款和条件”,这将打开一个链接/操作。
  • 你可以在标签上做一个手势,当标签被点击时你可以做一些事情。因此,当点击标签时,复选框会被标记。 @J.C
  • 此实例中的“标签”是按钮的标签,而不是 UILabel。有没有办法只用 UIButton 来做?

标签: ios swift user-interface swift3 uibutton


【解决方案1】:

我最终做了以下事情;

为我的实际复选框创建了一个 UIButton(空文本标签),并为我相邻的复选框标签文本创建了一个 UITextView。

然后我像这样使用NSMutableAttributedString

class MyViewController: UIViewController, UITextViewDelegate {

    override func viewDidLoad() {
        let attributedTermsTitle = NSMutableAttributedString()
        attributedTermsTitle.append(NSAttributedString(string: "I have read and accept the ", attributes: [NSFontAttributeName:UIFont(name: "My-Font", size: 14)!]))
        attributedTermsTitle.append(NSAttributedString(string: "Terms & Conditions", attributes: [NSFontAttributeName:UIFont(name: "My-Font", size: 14)!, NSLinkAttributeName: "terms"]))

        termsText.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.blue]
        termsText.attributedText = attributedTermsTitle

        termsText.delegate = self
        termsText.isSelectable = true
        termsText.isUserInteractionEnabled = true
        termsText.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(MyViewController.toggleCheckbox)))
    }

    func toggleCheckbox(sender: UITapGestureRecognizer) {    
        termsButton.isChecked = !termsButton.isChecked
    }

    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {

        let link = URL.absoluteString
        if(link == "terms") {
             // link to the Terms web page / screen
        }

        return false
    }

}

对于UIButton,给类;

class CheckBox: UIButton {
    let checkedImage = UIImage(named: "checkbox_on")! as UIImage
    let uncheckedImage = UIImage(named: "checkbox_off")! as UIImage

    var isChecked: Bool = false {
        didSet{
            if isChecked == true {
                self.setImage(checkedImage, for: UIControlState.normal)
            } else {
                self.setImage(uncheckedImage, for: UIControlState.normal)
            }
        }
    }

    override func awakeFromNib() {
        self.addTarget(self, action:#selector(buttonClicked(sender:)), for: UIControlEvents.touchUpInside)
        self.isChecked = false
    }

    func buttonClicked(sender: UIButton) {
        if sender == self {
            isChecked = !isChecked
        }
    }
} 

最终结果是 UIButton 和 UITextView 的非链接文本都会打开和关闭复选框。 “条款和条件”文本将链接到网站或转至其他地方。

希望这对其他人有所帮助。

【讨论】:

    【解决方案2】:

    您可以使用TTTAttributedLabel 使 UILabel 中的特定部分或单词可点击。

    例如

    label.text = @"Fork me on GitHub!"; // Repository URL will be automatically detected and linked
    NSRange range = [label.text rangeOfString:@"me"];
    [label addLinkToURL:[NSURL URLWithString:@"meUrl"] withRange:range];
    

    在这里我变成了可点击的,点击这个单词委托方法 didSelectLinkWithURL 被调用,你可以检查那个函数内的链接

    (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
        NSLog(@"link %@", [url absoluteString]);
       if ([url absoluteString] == @"meUrl") {
       //implement your logic here
       }
        NSLog(@"whole label %@", label);
    }
    

    【讨论】:

    • 如果它是一个标签,那么是的,我可以使用 TTTAttributedLabel - 尽管我更可能使用 UITextView - NSAttributedString 作为定义要点击的链接和 UITextViewDelegate 来捕捉链接的按下的方式例如stackoverflow.com/a/36604425/263534 原因是我认为扩展 UIButton 会更有意义,因为它主要是一个复选框。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    • 1970-01-01
    相关资源
    最近更新 更多