swift - 如何在 swift 中做可点击的字符串?

标签 swift click uilabel

我想要可点击的字符串。喜欢:

创建帐户即表示您同意 ABC 服务条款隐私政策

我想点击事件服务条款、隐私政策。

我的应用程序还支持多语言。我怎样才能用多

语言有什么建议吗?

最佳答案

这个解决方案应该适用于 Swift 4

class YourClassViewController: UIViewController, UITextViewDelegate {


@IBOutlet weak var terms: UITextView!

let termsAndConditionsURL = "http://www.example.com/terms";
let privacyURL = "http://www.example.com/privacy";

override func viewDidLoad() {
    super.viewDidLoad()

    self.terms.delegate = self
    let attributedString = NSMutableAttributedString(string: "termsString".localized())
    var foundRange = attributedString.mutableString.range(of: "terms_and_conditions".localized())
    attributedString.addAttribute(NSAttributedStringKey.link, value: termsAndConditionsURL, range: foundRange)
    foundRange = attributedString.mutableString.range(of: "Privacy".localized())
    attributedString.addAttribute(NSAttributedStringKey.link, value: privacyURL, range: foundRange)
    terms.attributedText = attributedString
}


func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
    if (URL.absoluteString == termsAndConditionsURL) {
        let myAlert = UIAlertController(title: "Terms", message: nil, preferredStyle: UIAlertControllerStyle.alert)
        myAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
        self.present(myAlert, animated: true, completion: nil)
    } else if (URL.absoluteString == privacyURL) {
        let myAlert = UIAlertController(title: "Conditions", message: nil, preferredStyle: UIAlertControllerStyle.alert)
        myAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
        self.present(myAlert, animated: true, completion: nil)
    }
    return false
 }
}

看看我使用了这里的本地化扩展 https://stackoverflow.com/a/29384360/4420355

extension String {
    func localized(withComment:String = "") -> String {
        return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: withComment)
    }
}

在我的项目中,我更喜欢这个 pod https://github.com/mac-cain13/R.swift

您必须将您的文本字符串存放在可本地化的多语言中。 有关完整教程,请查看 https://medium.com/lean-localization/ios-localization-tutorial-938231f9f881

//english
"terms_and_conditions" = "Terms and Condition";
"privacyURL" = "Privacy";
"termsString" =  "By using this app you agree to our Terms and Conditions and Privacy Policy";

//german
"terms_and_conditions" = "Geschäftsbedingungen";
"privacy_url" = "Datenschutz";
"termsString" =  "Wenn du diese App verwendest, stimmst du dem Datenschutz und den Geschäftsbedigungen zu";

如果您需要不同的隐私和条款链接,您也可以将它们添加到可本地化。

在此解决方案中,您可以非常轻松地处理多语言。

关于swift - 如何在 swift 中做可点击的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49093342/

相关文章:

opengl-es - GLKMatrix4(和其他类似的)在 swift 中丢失了吗?

javascript - 提交按钮中的 jquery click()

jQuery on click $(document) - 获取被点击的元素

jquery 单击 href 链接 - 必须单击两次

ios - 自定义 UITableViewCell - UILabel 未正确对齐

ios - 如何在不使用 Webview 的情况下在我的应用程序中播放 Youtube 视频

ios - 如何在惰性变量声明中快速访问实例变量

ios - uitableview 之前 View 的动态 UILabel 大小

ios - UILabel 从垂直中心开始

ios - Swift:来自 UIViewController 扩展的 registerForKeyboardNotifications