UITableViewCell 具有可选择/可复制的文本,还可以检测 iPhone 上的 URL
我有一个问题。我的应用程序的一部分需要在表格中显示文本。文本需要是可选择/可复制的(但不可编辑),并且文本中的任何 URL 都需要突出显示,并且在点击时允许我获取该 URL 并打开我的嵌入式浏览器。
我见过一些解决方案可以解决其中一个问题,但不能同时解决两个问题。
第一个我尝试的解决方案是使用由 Icon Factory 实现并在 Twitterrific 中使用的 IFTweetLabel 类。
虽然此解决方案允许检测链接(或可以使用正则表达式找到的任何内容)并根据具体情况进行处理,但它不允许选择和复制。
还有一个问题是,如果 URL 足够长而可以换行,则该类覆盖在 URL 上方以使其具有交互性的按钮无法换行并从屏幕上移出,看起来非常奇怪。
解决方案 2:使用 IFTweetLabel 并手动处理复制
我尝试的第二件事是保留 IFTweetLabel 来处理链接,但使用长按手势实现复制,就像短信应用程序处理的方式一样它。这只是为了工作,但它不允许任意选择文本,复制整个文本,或者根本不复制任何文本......非常黑白。
解决方案 3:UITextView
我的第三次尝试是添加 UITextView
作为表格单元格的子视图。
唯一没有解决的问题是我无法处理检测到的 URL。文本视图使用 UIApplication
的 openURL:
方法退出我的应用程序并启动 Safari。
此外,由于表格视图可能变得相当大,作为子视图添加的 UITextView 数量会导致整个表格滚动时的性能明显下降,尤其是在 iPhone 3G 时代的设备上(因为创建、布局,每当单元格在屏幕上滚动时进行合成等)。
因此,我向所有知识渊博的人们提出的问题是:我能做什么?
UIWebView
是最好的选择吗?除了性能拖累之外,我认为 webview 可以解决上述所有问题,如果我没记错的话,早在 2.0 时代,Apple 文档实际上推荐了需要文本格式/超链接的 web 视图。
任何人都可以想出一种方法来实现这一目标而不拖累性能吗?
非常感谢所有可以提供帮助的人。
I have a problem. Part of my app requires text to be shown in a table. The text needs to be selectable/copyable (but not editable) and any URLs within the text need to be highlighted and and when tapped allow me to take that URL and open my embedded browser.
I have seen a couple of solutions that solve one of either of these problems, but not both.
Solution 1: Icon Factory's IFTweetLabel
The first solution I tried was to use the IFTweetLabel class made possible by Icon Factory and used in Twitterrific.
While this solution allows for links (or anything you can find with a regex) to be detected to be handled on a case by case basis, it doesn't allow for selecting and copying.
There is also an issue where if a URL is long enough to be wrapped, the button that the class overlays above the URL to make it interactive cannot wrap and draws off screen, looking very odd.
Solution 2: Use IFTweetLabel and handle copy manually
The second thing I tried was to keep IFTweetLabel in place to handle the links, but to implement the copying using a long-tap gesture, like how the SMS app handles it. This was just about working, but it doesn't allow for arbitrary selection of text, the whole text is copied, or none is copied at all... Pretty black and white.
Solution 3: UITextView
My third attempt was to add a UITextView
as a subview of the table cell.
The only thing that this doesn't solve is the fact that detected URLs cannot be handled by me. The text view uses UIApplication
's openURL:
method which quits my app and launched Safari.
Also, as the table view can get quite large, the number of UITextView
s added as subviews cause a noticeable performance drag on scrolling throughout the table, especially on iPhone 3G era devices (because of the creation, layout, compositing whenever a cell is scrolled on screen, etc).
So my question to all you knowledgeable folk out there is: What can I do?
Would a UIWebView
be the best option? Aside from a performance drag, I think a webview would solve all the above issues, and if I remember correctly, back in the 2.0 days, the Apple documentation actually recommended web views where text formatting / hyperlinks were required.
Can anyone think of a way to achieve this without a performance drag?
Many thanks in advance to everyone who can help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我按下提交按钮时,一个新想法突然出现。
我非常专注于让 URL 与文本内联并具有交互性,以至于我没有认为这可能不是最好的解决方案。
我确信要实现这种行为,无论性能问题如何,
UIWebView
都是最佳选择。然而,也许更好的用户体验/交互不是突出显示内联的 URL,而是在幕后将它们收集到一个数组中,并呈现一个公开按钮作为单元格的附件视图?
然后,对于选择和复制文本,我可以在关闭数据检测器的情况下使用 UITextView,而不必担心链接被发送到 safari 并关闭我的应用程序。
当点击公开按钮时,用户可以被带到文本中找到的 URL,或者如果找到多个 URL,则向用户提供一个选择器视图以选择要访问的 URL。
欢迎对此想法的任何想法/批评。
As soon as I hit the submit button, a new idea hit me.
I was so preoccupied with having URLs inline with text and interactive that I didn't consider that maybe it's not the best solution.
I'm certain that to achieve that kind of behaviour, a
UIWebView
is the best choice, regardless of the performance issues.However, maybe a better user experience / interaction is to not highlight the URLs inline, but to gather them into an array behind the scenes, and present a disclosure button as the cell's accessory view?
Then for selection and copying text, I could just use the
UITextView
with data detectors turned off and not worry about the links being sent off to safari and closing my app.When the disclosure button is tapped, the user could be whisked off to the URL found in the text, or if more than one URL is found, present the user with a picker view to choose which to go to.
Any thoughts/criticisms of this idea are welcome.
您可以通过重写 UITextField 委托方法来防止编辑文本字段,以便它们不应用任何编辑。这使得该字段可以选择和复制,但可以防止更改。
一个更好的问题是:你真的必须显示实际的 URL 本身吗?您可以只使用页面/位置名称、server.host.domain 前缀或其他一些压缩的 url 表示形式吗?我认为没有人会尝试在移动设备的受限屏幕上阅读长网址。
如果您确实需要显示整个网址,那么我认为详细视图是正确的选择。
You can prevent a textfield from being edited by overriding the UITextField Delegate methods such that they do not apply any edits. That leaves the field selectable and copyable but prevents alteration.
A better question to ask is: do you actually have to display the actual URL itself? Can you get away with just a page/location name, just the server.host.domain prefix or some other condensed representation of the url? I don't think anyone whats to try to read a long url on a mobile's restricted screen.
If you do need to display the entire url then I think that a detail view is the way to go.