OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在 UILabel 中启用超链接 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 12:11
标题: ios - 如何在 UILabel 中启用超链接

我拥有的是一个 UILabel(加载了 html),我将它放在 UIScrollView 上。

在 html 中,我有很多点击时无法“启动”的链接。

我在 UILabelUIScrollview 上都添加了 UserInteractionEnabled = true 但我无法从点击中得到任何反应超链接。

这是创建带有内容的标签的代码。

        var attr = new NSAttributedStringDocumentAttributes();
        var nsError = new NSError();
        attr.DocumentType = NSDocumentType.HTML;

        var entry = "some text and url <a href='http://www.google.com'>url</a>";
        var myHtmlData = NSData.FromString(entry, NSStringEncoding.Unicode);

        contentLabel.AttributedText = new NSAttributedString(myHtmlData, attr, ref nsError);
        contentLabel.Frame = new CGRect(0, 20, scrollView.Frame.Size.Width  , scrollView.Frame.Size.Height);
        contentLabel.LineBreakMode = UILineBreakMode.WordWrap;
        contentLabel.Lines = 0;
        contentLabel.SizeToFit();

也许这是错误的.. 将 html 内容放入标签中?但我不想使用 webview,因为它似乎不能很好地扩展。



Best Answer-推荐答案


为什么不使用 UITextView 呢?启用链接检测并禁用可编辑和 optional 。

关于ios - 如何在 UILabel 中启用超链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34022241/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4