简体   繁体   English

NSMutableAttributedString iOS6中的可点击URL

[英]Clickable URL in NSMutableAttributedString iOS6

I'm working on an app for fun where I receive messages from a remote web service, like Twitter. 我正在开发一个有趣的应用程序,从远程Web服务(如Twitter)接收消息。 Sometimes these messages contains URL:s, and I want to make these clickable. 有时这些消息包含URL:s,而我想使它们可单击。 I wonder which approach would be the best. 我不知道哪种方法是最好的。 I have tried NSLinkAttributeName but it doesn't work for iOS. 我试过NSLinkAttributeName,但不适用于iOS。 Is it possible to create a transparent button and position it above the right place in the textview? 是否可以创建一个透明按钮并将其放置在textview中的正确位置上方? And how could I do that? 我该怎么办呢? Or is there a better/easier way? 还是有更好/更简便的方法? The position and length of the url can vary. 网址的位置和长度可以变化。

There are two good ways of going about this: 有两种解决方法:

1. CoreText, or rather a wrapper around it like TTTAtributedLabel 1. CoreText,或者更确切地说,它周围的包装像TTTAtributedLabel

That's the most powerful solution. 那是最强大的解决方案。 It supports Datadetectortypes (though you are better off doing your own detection for performance ahead of displaying it) and it's relatively popular. 它支持Datadetectortypes(尽管最好在显示它之前对性能进行自己的检测),并且它相对流行。

It might however affect your tableview's scrolling performance depending on your usage. 但是,这可能会影响表格视图的滚动性能,具体取决于您的使用情况。

2. Dynamically placing buttons above your labels like IFTweetLabel 2.将按钮动态放置在标签上方,例如IFTweetLabel

That's the easy solution. 那是简单的解决方案。 IFTweetLabel is basically a drop-in for UILabel and it performs really well out of the box. IFTweetLabel基本上是UILabel的一个插件,并且开箱即用地表现非常好。 However, you will eventually run into its limits if you're looking to customize it too much. 但是,如果您希望对其进行过多的自定义,最终将遇到限制。 Not to mention that it doesn't behave perfectly 100% of the time. 更不用说它在100%的时间内表现不理想。 YMMV. 因人而异。

One option is by using UIDataDetectorTypeLink . 一种选择是使用UIDataDetectorTypeLink Check this link for more details. 检查此链接以获取更多详细信息。

// Create textview, centering horizontally
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 10, 320, 50)];

// Set font, background and alignment
[textView setFont:[UIFont boldSystemFontOfSize:16]];
[textView setBackgroundColor:[UIColor blackColor]];
[textView setTextAlignment:UITextAlignmentCenter];

// Unfortunately the link will show as blue even with this setting
//  [textView setTextColor:[UIColor greenColor]];

// Edit and scrolling off  
[textView setEditable:NO];
[textView setScrollEnabled:NO];

// Set data type to specify URL/link
[textView setDataDetectorTypes:UIDataDetectorTypeLink];

// Set text as URL
[textView setText:@"text.com"];  

[self.view addSubview:textView];

Some other options are UIWebview with delegates to handle touch on urls in it. UIWebview还有其他一些选项, UIWebview包含委托来处理其中的URL触摸。 And another option is fancy UILabels 另一个选择是精美的UILabels

For the other questions you asked, check this How to open a UITextView URL in UI Web View? 对于您提出的其他问题,请选中“ 如何在UI Web视图中打开UITextView URL”? , Is there a way to create custom UIDataDetectorTypes? 是否有创建自定义UIDataDetectorTypes的方法? and Change color of UITextView links with a filter? 使用过滤器更改UITextView链接的颜色?

NSLinkAttributeName is tappable with UITextView , but not with UILabel . NSLinkAttributeName可通过UITextView轻敲,但不可与UILabel轻敲。 Just note that it's for iOS 7+ , and that the UITextView must be Selectable (in Storyboard). 请注意, 它适用于iOS 7+ ,并且UITextView必须是“ Selectable (在Storyboard中)。

Swift demonstration: 迅捷演示:

let attributedText = NSMutableAttributedString(string: "Hello world")
attributedText.addAttribute(.link, value: URL(string: "https://example.com")!, range: NSRange(location: 0, length: attributedText.length))
textView.attributedText = attributedText

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM