UILabel有多个链接

时间:2014-11-14 12:19:41

标签: ios objective-c uilabel nsattributedstring

您好我试图像Facebook上的UILabel类似文本,Label的文字类似于

"You, Steve and 50 others like this."

“你”,“史蒂夫”和“其他50人”应该单独点击。

我正在尝试使用NSAttributedString,但它没有帮助我,任何人都可以帮助我找到方法吗?

1 个答案:

答案 0 :(得分:1)

尝试这个,但它不适用于标签,但它适用于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;
  }