text UILabel clicable(como RTLabel)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text UILabel clicable(como RTLabel)相关的知识,希望对你有一定的参考价值。

1) EL COMPONENTE DEBE SER UN TEXTVIEW
quitar el Editable del componente y el Scrolling enabled

2)
extension to NSMutableAttributedString:

import Foundation

extension NSMutableAttributedString {

   public func setAsLink(textToFind:String, linkURL:String) -> Bool {

       let foundRange = self.mutableString.rangeOfString(textToFind)
       if foundRange.location != NSNotFound {
           self.addAttribute(NSLinkAttributeName, value: linkURL, range: foundRange)
           return true
       }
       return false
   }
}

3) En la VIEW, VIEWCONTROLLER

añadir el UITextViewDelegate


let first = String.init(format: "%@ | %@\n\n%@ | %@ | %@", "cookies".localized, "privacy_policy".localized,"legal_terms".localized, "cmmedia".localized, "contact".localized);
            
let attributedString = NSMutableAttributedString(string:first)
let _ = attributedString.setAsLink(textToFind: "cookies".localized, linkURL: "cookies")
let _ = attributedString.setAsLink(textToFind: "privacy_policy".localized, linkURL: "policy")
let _ = attributedString.setAsLink(textToFind: "legal_terms".localized, linkURL: "terms")
let _ = attributedString.setAsLink(textToFind: "cmmedia".localized, linkURL: "cmmedia")
let _ = attributedString.setAsLink(textToFind: "contact".localized, linkURL: "contact")
            
self.lblClicable.attributedText = attributedString;
self.lblClicable.isUserInteractionEnabled = true;
self.lblClicable.textColor = UIColor.white;
self.lblClicable.textAlignment = .center
            
self.lblClicable.delegate = self;

4) tratamiento del click

        func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
            
            NSLog(URL.absoluteString);
            return false;
        }

以上是关于text UILabel clicable(como RTLabel)的主要内容,如果未能解决你的问题,请参考以下文章

css Darle a todos los elementos clicables el cursor pointer

IOS开发调整UILabel的行间距

无法从另一个视图控制器更改 UILabel.text,UILabel 得到 nil 值

更改 UILabel.text 的字符串

让UILabel的文字顶部对齐

让UILabel的文字顶部对齐