[英]How to do clickable string in swift ?
I want to clickable string.我想要可点击的字符串。 like :
喜欢 :
By creating an account, you agree to the ABC Terms of Service and Privacy Policy .创建帐户即表示您同意 ABC服务条款和隐私政策。
I want to click event Terms of Service , Privacy Policy.我想点击活动服务条款,隐私政策。
My app support also multi language.我的应用程序也支持多语言。 How can I do this with multi
我怎么能用多做到这一点
language any advice please ?语言有什么建议吗?
This solution should work for Swift 4这个解决方案应该适用于 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
}
}
Have a look that I use the localized extension from here https://stackoverflow.com/a/29384360/4420355看看我使用这里的本地化扩展https://stackoverflow.com/a/29384360/4420355
extension String {
func localized(withComment:String = "") -> String {
return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: withComment)
}
}
In my projects I prefer this pod https://github.com/mac-cain13/R.swift在我的项目中,我更喜欢这个 pod https://github.com/mac-cain13/R.swift
You have to deposit your text strings in your localizable for multi language.您必须将文本字符串存放在可本地化的多语言中。 For a complete tutorial have a look at https://medium.com/lean-localization/ios-localization-tutorial-938231f9f881
有关完整教程, 请查看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";
If you need different links for the privacy and the terms you can add them to localizable too.如果您需要不同的隐私和条款链接,您也可以将它们添加到本地化。
In this solution you handle multi language in a very easy way.在此解决方案中,您可以以非常简单的方式处理多语言。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.