如何设置UILabel的一部分动作?

时间:2015-08-25 03:21:29

标签: swift storyboard uilabel

我想将行动设置为"部分" UILabel不是所有的UIlabel,就像"服务条款"附图。我正在使用故事板。现在,我想在UILabel的部分覆盖UIButton。但是,通过自动布局在任何布局(iPhone 5,6和6 plus)上使用按钮调整标签部分很麻烦。如果你知道更好的方法,请告诉我。谢谢你的好意。

enter image description here

2 个答案:

答案 0 :(得分:1)

我建议UITextView使用NSAttributedString。获取您要查找的文本范围,然后将该按钮作为子视图添加到视图中。

NSRange termsOfServiceRange = [self.termsOfUseTextView.text rangeOfString:@"Terms of Service"];
self.termsOfUseTextView.selectedRange = termsOfServiceRange;
UITextRange *termsOfServiceTextRange = [self.termsOfUseTextView selectedTextRange];
CGRect termsOfServiceFrame = [self.termsOfUseTextView firstRectForRange:termsOfServiceTextRange];
CGRect convertedFrame = [self.view convertRect:termsOfServiceFrame fromView:self.termsOfUseTextView];

UIButton *termsOfServiceButton = [[UIButton alloc]initWithFrame:convertedFrame];
[termsOfServiceButton setBackgroundColor:[UIColor clearColor]];
[termsOfServiceButton addTarget:self action:@selector(termsOfServiceButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:termsOfServiceButton];
[self.view bringSubviewToFront:termsOfServiceButton];

答案 1 :(得分:1)

我用TTTAttributedLabel解决了! https://github.com/TTTAttributedLabel/TTTAttributedLabel

相关问题