ios - 具有多个链接的 UILabel

标签 ios objective-c uilabel nsattributedstring

你好,我正在尝试使用类似于 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/

相关文章:

ios - 使用 use_native_modules react Native Pod 问题

objective-c - NSView 设置框架时出现问题

iphone - 在 iOS 5.1 中创建具有不同样式文本的 UILabel,而不使用 Storyboard

iphone - 使用 NSAttributedString 为 UILabel 设置突出显示的文本颜色。?

ios - 多行 UILabel 底部在滚动后被截断,UITableViewCell -> UIStackView -> UILabel

swift - 使用 UITextField 字符串更改 UIView 颜色

iphone - 你如何清除 UIWebView 的缓存?

ios - 如何根据 ADFS 3.0 识别通过 ADAL 身份验证的用户?

javascript - 如何知道一个耗时的 CSS 操作(渲染)已经完成

objective-c - NSInteger = NSInteger -1?