簡體   English   中英

如何在 swift 中做可點擊的字符串?

[英]How to do clickable string in swift ?

我想要可點擊的字符串。 喜歡 :

創建帳戶即表示您同意 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";

如果您需要不同的隱私和條款鏈接,您也可以將它們添加到本地化。

在此解決方案中,您可以以非常簡單的方式處理多語言。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM