菜鸟教程小白 发表于 2022-12-11 17:09:54

ios - 如何将 URL+ 文本设置为 UILabel 文本


                                            <p><p>我想显示一些文本 + URL + 一些文本 + URL + 一些文本。使用文本 + URL 创建此字符串后,我想将此文本设置为 UILabel。怎么能做到这一点?
    我可以使用其他一些控件而不是 UILabel 来在 View 上显示文本,我对此很灵活。
    我已经为此搜索过,但我发现只有一种方法是使用属性文本和点击识别器来识别触摸,然后相应地处理它们。我认为这会使我的情况复杂化。我想知道有没有办法解决这个问题。</p>

<p>我想做这样的事情“如果您想创建一个帐户,请访问 <strong>Google.com</strong> 或观看 <strong>演示视频</strong>。您可以<strong>给我们发送电子邮件</strong> 调用支持<strong>444-1520-1458</strong>"</p>

<p>所有以粗体显示的文本我希望它是可点击的。并且在单击时我想执行一些操作。我还希望该粗体文本以蓝色显示。我已经为这个问题搜索了一些链接:
1) <a href="https://stackoverflow.com/questions/20541676/ios-uitextview-or-uilabel-with-clickable-links-to-actions" rel="noreferrer noopener nofollow">iOS UITextView or UILabel with clickable links to actions</a> </p>

<p>2) <a href="https://stackoverflow.com/questions/21629784/how-to-make-a-clickable-link-in-an-nsattributedstring-for-a" rel="noreferrer noopener nofollow">How to make a clickable link in an NSAttributedString for a</a> </p>

<p>在第一个链接的情况下,逻辑似乎很完美,但我输入的文本没有居中对齐。
在第二种情况下,属性字符串不可 ckickable。我已经搜索但没有成功。需要一些帮助</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>当我不得不使用超链接时,我这样做了</p>

<p>我将文本设置为 UITextView</p>

<pre><code>let link = &#34;&lt;a href=\&#34;\(link)\&#34;&gt;Link&lt;/a&gt;&#34;

let aLink = try! NSMutableAttributedString(data: link.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, options: , documentAttributes: nil)

self.textView.attributedText = aLink
</code></pre>

<p>这对我有用</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何将 URL&#43; 文本设置为 UILabel 文本,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38746901/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38746901/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何将 URL&#43; 文本设置为 UILabel 文本