的UILabel与多个链接
问题描述:
你好我想可点击的UILabel与Facebook类似的,如文本,标签的文本将类似于的UILabel与多个链接
"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;
}
+0
后不要忘记
+1
“仅当文本视图可选但不可编辑时,文本视图中的链接才是交互式的。” - 可能对某人有用,我会浪费很多时间。 – 2016-01-28 11:30:53
这可能会帮助您:http://stackoverflow.com/questions/8839464/uilabel-string-as-text-and-links – Mrunal 2014-11-14 12:22:13
感谢@Mrunal我认为,这将导致我的解决方案 – Abhishek 2014-11-14 12:28:40
@ Kampai我认为它对于UITextView – Abhishek 2014-11-14 12:28:56