gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:02:51 28 4
gpt4 key购买 nike

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

创建帐户即表示您同意 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com