简体   繁体   English

如何在iOS应用程序中创建内联文本锚点

[英]How to Create Inline Text Anchors in an iOS App

I have a simple iOS app that dynamically loads content with HTML styling into a UITextView . 我有一个简单的iOS应用程序,可以将带有HTML样式的内容动态加载到UITextView

This works great as NSAttributedString handles all the HTML tags that I need to be able to support, with the exception of one case, which is for inline text anchors. 这非常有效,因为NSAttributedString处理我需要能够支持的所有HTML标记,但有一种情况除外,它用于内联文本锚。 It handles external links just fine, but doesn't appear to either recognise or jump to text anchors when tapped. 它可以很好地处理外部链接,但在点击时似乎不会识别或跳转到文本锚点。

For example, if I supply the view with the following HTML (converted to an NSAttributedString ): 例如,如果我为视图提供以下HTML(转换为NSAttributedString ):

<p><a href="#anchor1">Lorem ipsum</a> dolor sit amet, consectetur adipiscing elit. Fusce dapibus, ante et varius imperdiet, est risus tincidunt ex, non fermentum enim eros quis metus. Quisque vehicula lacus at ornare ullamcorper. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Integer scelerisque lectus quis libero rutrum, quis condimentum dolor porta.</p>

[lots more text]

<p><a name="anchor1">Sed</a> eros purus, lobortis a rhoncus id, facilisis ac orci. In hac habitasse platea dictumst. In sagittis in orci vitae volutpat. Quisque sollicitudin lectus at est ultrices, et volutpat diam lobortis.</p>

Clicking the Lorem ipsum link does nothing in this case, but if I change its href to some external URL then that triggers as expected, so the issue appears to be internal links only. 在这种情况下,单击Lorem ipsum链接不会执行任何操作,但如果我将其href更改为某个外部URL,则会按预期触发,因此问题似乎只是内部链接。

So what I'm wondering is, is it possible to natively use HTML style named anchors within UITextView at all? 所以我想知道的是,是否有可能在UITextView中本地使用HTML样式命名锚点? If not, are there any simple alternatives? 如果没有,有没有简单的替代方案?

I'm using Swift, so an answer in that language (if coding is necessary) is preferred. 我正在使用Swift,因此首选该语言的答案(如果需要编码)。

I am not aware of any HTML - JS only based solution. 我不知道任何基于HTML - JS的解决方案。

But there is something similar I did in one if my apps except that I did not scroll to a particular section of textview. 但是,除了我没有滚动到textview的特定部分之外,还有一些类似于我的应用程序。 There is a delegate of UITextView that gives you handle on which url was clicked. 有一个UITextView的委托 ,可以处理单击的URL。

You can use this to get the url. 您可以使用它来获取网址。 The next problem is to get the scroll offset of that anchor tag in the whole text. 下一个问题是在整个文本中获取该锚标记的滚动偏移量。 Of the top of my mind, one way to get that info is search the text for the anchor tag then using a NSString method like this to get the height that the string would require. 我心中的顶部,以获取信息的一种方式是搜索,然后使用NSString的方法,如锚标记文本让该字符串需要的高度。 Use that as the scroll offset. 将其用作滚动偏移量。

Clearly, doing this computation on the main thread after the user clicks the link will be a very bad user experience. 显然,在用户点击链接后在主线程上进行此计算将是一个非常糟糕的用户体验。 Hence, you can pre-compute this height if you architect your app in an appropriate way. 因此,如果您以适当的方式构建应用程序,则可以预先计算此高度。

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

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