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

ios - UITextContainerView 隐藏 UITextView 的可点击链接


                                            <p><p>我使用 NSMutableAttributeString 在 UITextView 中创建了一个可点击的链接。
它所改变的只是文本被突出显示</p>

<p> <a href="/image/VxbHc.png" rel="noreferrer noopener nofollow"><img src="/image/VxbHc.png" alt="The link is on the telephone number"/></a> </p>

<p> <a href="/image/Bwj01.png" rel="noreferrer noopener nofollow"><img src="/image/Bwj01.png" alt="DEBUGGER"/></a>
正如我们所看到的:漂浮在我的 UITextView 上的是一个 UIContainerView(我真的不知道是不是因为这个......我正在尝试)</p>

<p>这是我的 UIView 代码:
    类信息框:UIView {</p>

<pre><code>let Heading: UITextView = {
    let textView = UITextView(frame: CGRect(x: 15, y: 0, width: 200, height: 35))
    textView.font = UIFont.systemFont(ofSize: 20)
    textView.textColor = UIColor.white
    textView.isScrollEnabled = false
    textView.backgroundColor = UIColor.clear
    textView.isEditable = false
    textView.isSelectable = true
    return textView
}()

let TextContent: UITextView = {
    let textView = UITextView(frame: CGRect(x: 15, y: 27, width: UIScreen.main.bounds.width, height: 30))
    textView.font = UIFont.systemFont(ofSize: 17)
    textView.textColor = UIColor.white
    textView.isScrollEnabled = false
    textView.backgroundColor = UIColor.clear
    textView.isEditable = false
    textView.isSelectable = true
    return textView
}()}
</code></pre>

<p>NSAttributedString 代码:</p>

<pre><code>      func transformText(text: String, underlined: Bool, linkURL: String) -&gt; NSAttributedString {
    let textRange = NSMakeRange(0, text.characters.count)
    let attributedText = NSMutableAttributedString(string: text)
    if underlined{
      attributedText.addAttribute(NSUnderlineStyleAttributeName , value: NSUnderlineStyle.styleSingle.rawValue, range: textRange)
      attributedText.addAttribute(NSUnderlineColorAttributeName , value: UIColor.lightGray, range: textRange)
    }
    attributedText.addAttribute(NSFontAttributeName , value: UIFont(name: &#34;Helvetica-Light&#34;, size: 17)!, range: textRange)
    attributedText.addAttribute(NSForegroundColorAttributeName , value: UIColor.lightGray, range: textRange)
    if(linkURL != &#34;&#34;)
    {
      let attrib =

      attributedText.addAttributes(attrib, range: textRange)
    }
    return attributedText
}
</code></pre>

<p>这就是它的名称:</p>

<pre><code>    self.TelBox.TextContent.attributedText = transformText(text: self.TelBox.TextContent.text, underlined: true, linkURL: &#34;https://www.google.fr&#34;)
</code></pre>

<p>第二个问题:是否可以在 UITextView 中为电话号码创建可点击链接,以便在点击时调用该号码?用 UIButton 做的。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我不确定您的 <code>UIContainerView</code> 有什么问题,据我所知没有任何问题。</p>

<p>这是使链接调用数字的方法:</p>

<pre><code>func transformText(text: String, underlined: Bool, phoneNumber: String) -&gt; NSAttributedString {
      let textRange = NSMakeRange(0, text.characters.count)
      let attributedText = NSMutableAttributedString(string: text)
      if underlined{
            attributedText.addAttribute(NSAttributedStringKey.underlineStyle , value: NSUnderlineStyle.styleSingle.rawValue, range: textRange)
            attributedText.addAttribute(NSAttributedStringKey.underlineColor , value: UIColor.lightGray, range: textRange)
      }
      attributedText.addAttribute(NSAttributedStringKey.font , value: UIFont(name: &#34;Helvetica-Light&#34;, size: 17)!, range: textRange)
      attributedText.addAttribute(NSAttributedStringKey.foregroundColor , value: UIColor.lightGray, range: textRange)
      if(phoneNumber != &#34;&#34;)
      {
            let attrib = [
                NSAttributedStringKey.link: URL(string: &#34;tel://&#34; + phoneNumber.replacingOccurrences(of: &#34; &#34;, with: &#34;&#34;))]
            attributedText.addAttributes(attrib, range: textRange)
      }
      return attributedText
    }
</code></pre>

<p>你可以这样使用它:<code>TextContent.attributedText = transformText(text: "12345", underlined: true, phoneNumber: "12345")</code></p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UITextContainerView 隐藏 UITextView 的可点击链接,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/46565704/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/46565704/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UITextContainerView 隐藏 UITextView 的可点击链接