你好,我正在尝试使用类似于 Facebook 的类似文本的可点击 UILabel,Label 的文本类似于
"You, Steve and 50 others like this."
“你”、“史蒂夫”和“另外 50 个人”应该可以分别点击。
我正在尝试 NSAttributedString 的运气,但它对我没有帮助,任何人都可以帮助我找到方法吗?
最佳答案
试试这个,但它不是用于标签,而是用于 textView。
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"firstsecond"];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(5,6)];
//[string addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:10] range:NSMakeRange(0,5)];
[string addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"http://www.google.co.in"] range:NSMakeRange(0,5)];
[string addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"http://www.yahoo.com"] range:NSMakeRange(5,6)];
self.txtView.attributedText=string;
self.txtView.scrollEnabled = NO;
self.txtView.editable = NO;
self.txtView.textContainer.lineFragmentPadding = 0;
self.txtView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
self.txtView.delegate = self;
}
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)url inRange: (NSRange)characterRange
{
return YES;
}
关于ios - 具有多个链接的 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26929892/