2017-06-06 31 views
0

我有以下UIButton呈现复选框和标签; iOS UIButton rendering as checkboxUIButton标签检测点击特定文本

要创建此,我用一个UIButton控制与NSMutableAttributedString给“条款&条件”不同的风格文本的其余部分。

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

但是,如果用户点击“条款&条件”我希望执行操作(例如加载术语URL)。

这在Android中很容易实现,因为您可以使用checkbox.setMovementMethod(LinkMovementMethod.getInstance());,但在iOS中,我非常努力地找到一种方法来基本创建一个包含链接标签的复选框。

有没有人曾经这样做过,你是如何处理的?

+0

此链接可帮助您。 https://stackoverflow.com/questions/20541676/ios-uitextview-or-uilabel-with-clickable-links-to-actions –

+0

你想点击标签或当开关改变? – Gregga17

+0

就像它可以在网页上工作 - 点击“标签”也会标记复选框 - 除非用户点击了可以打开链接/操作的“条款和条件”。 –

回答

0

最后我做了以下内;

为我的实际复选框创建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的非链接文本都会打开和关闭复选框。 “条款&条款”文本将链接到网站或在其他地方继续。

希望这可以帮助别人。

1

您可以使用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); 
} 
+0

如果它是一个标签,那么是的,我可以使用TTTAttributedLabel - 尽管我更可能使用UITextView - NSAttributedString作为定义点击链接和UITextViewDelegate以捕获链接的链接的方式。 https://stackoverflow.com/a/36604425/263534原因是我认为扩展UIButton会更有意义,因为它主要是一个复选框。 –

+0

好的感谢您的反馈 –