菜鸟教程小白 发表于 2022-12-11 18:33:11

ios - swift:在文本上设置超链接


                                            <p><p>我正在尝试在文本中设置超链接。文本出现,但我在文本上找不到链接。任何人都可以在这里帮忙。
我想在 getStartedDict 上设置 <strong>我的健康行动计划</strong> 的链接。但只显示整个文本,没有任何链接。</p>

<pre><code>let link1 = [&#34;My Health Action Plan&#34;]
var getStartedDict =
   [&#34;&#34;:&#34;Now that you have a glimpse of where you currently are in managing your health, it is time for you to update your Health Action Plan. Use the information in the Health Profile along with the online resources in this portal to help set realistic health goals and to help you work toward better health.&#34;]
var linkArray = []()

override func viewDidLoad(){
    super.viewDidLoad()
    linkArray.removeAll()
    linkArray.append(link1)
    CommonUtility.sharedInstance.addBackButton(self)
    loadData()
}
</code></pre>

<p>//这里我尝试设置链接</p>

<pre><code>if let getStartedText = self.getStartedDict?.htmlStringToAttributedString()
      {
            let aText = getStartedText
            let myRange = NSRange(location: 0, length: aText.length)
            aText.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(kFontSize), range: myRange)
            print(aText)
            cell.detail.setLinksForSubstrings(linkArray, withLinkHandler: handler)

      }
</code></pre>

<p><strong>更新:请参阅评论以获取答案</strong></p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>经过长时间的头痛后,我终于做到了:</p>

<pre><code>var getStartedDict =
   [&#34;Value&#34;:&#34;Now that you have a glimpse of where you currently are in managing your health, it is time for you to update your Health Action Plan. Use the information in the Health Profile along with the online resources in this portal to help set realistic health goals and to help you work toward better health.&#34;]

override func viewDidLoad(){
      super.viewDidLoad()
      CommonUtility.sharedInstance.addBackButton(self)
      loadData()
   }
</code></pre>

<p><strong>我们可以这样设置链接</strong></p>

<pre><code>if let getText = self.getStartedDict[&#34;Value&#34;]?.htmlStringToAttributedString(){
      let aText = getText
      let myRange = NSRange(location: 0, length: aText.length)
      aText.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(kFontSize), range: myRange)
      cell.detail.attributedText = aText
      cell.detail.setLinksForSubstrings([&#34;Health Action Plan&#34;], withLinkHandler: handler)
    }
    else{
      print(&#34;No text found&#34;)
    }
</code></pre>

<p>如果有更好的方法,请随时发表评论。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - swift:在文本上设置超链接,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41584753/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41584753/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - swift:在文本上设置超链接