ios – 使用swift在UILabel中单击事件的NSAttributedString

ios – 使用swift在UILabel中单击事件的NSAttributedString,第1张

概述假设我有一个AttributedString:“已经有一个帐户?登录!”. 我将此字符串放在UILabel中. 现在当用户点击“登录!”时,当前的viewController应该转到另一个viewController,或者在点击登录时调用某个函数. 任何代码或建议都应该没问题. 一些答案陈述,没有必要使用单独的手势识别器.相反,您可以将属性文本与UITextViewDelegate的textVie 假设我有一个AttributedString:“已经有一个帐户?登录!”.
我将此字符串放在UILabel中.
现在当用户点击“登录!”时,当前的vIEwController应该转到另一个vIEwController,或者在点击登录时调用某个函数.

任何代码或建议都应该没问题.

解决方法 一些答案陈述,没有必要使用单独的手势识别器.相反,您可以将属性文本与UITextVIEwDelegate的textVIEw(_:shouldIn@R_403_6704@ctWithURL:inRange)方法结合使用来实现此目的,例如:

class VIEwController: UIVIEwController,UITextVIEwDelegate {    @IBOutlet weak var textVIEw: UITextVIEw!    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        let text = NSMutableAttributedString(string: "Already have an account? ")        text.addAttribute(NSFontAttributename,value: UIFont.systemFontOfSize(12),range: NSMakeRange(0,text.length))        let selectablePart = NSMutableAttributedString(string: "Sign in!")        selectablePart.addAttribute(NSFontAttributename,selectablePart.length))        // Add an underline to indicate this portion of text is selectable (optional)        selectablePart.addAttribute(NSUnderlinestyleAttributename,value: 1,selectablePart.length))        selectablePart.addAttribute(NSUnderlinecolorAttributename,value: UIcolor.blackcolor(),selectablePart.length))        // Add an NSlinkAttributename with a value of an url or anything else        selectablePart.addAttribute(NSlinkAttributename,value: "signin",selectablePart.length))        // Combine the non-selectable string with the selectable string        text.appendAttributedString(selectablePart)        // Center the text (optional)        let paragraphStyle = NSMutableParagraphStyle()        paragraphStyle.alignment = NSTextAlignment.Center        text.addAttribute(NSParagraphStyleAttributename,value: paragraphStyle,text.length))        // To set the link text color (optional)        textVIEw.linkTextAttributes = [NSForegroundcolorAttributename:UIcolor.blackcolor(),NSFontAttributename: UIFont.systemFontOfSize(12)]        // Set the text vIEw to contain the attributed text        textVIEw.attributedText = text        // disable editing,but enable selectable so that the link can be selected        textVIEw.editable = false        textVIEw.selectable = true        // Set the delegate in order to use textVIEw(_:shouldIn@R_403_6704@ctWithURL:inRange)        textVIEw.delegate = self    }    func textVIEw(textVIEw: UITextVIEw,shouldIn@R_403_6704@ctWithURL URL: NSURL,inRange characterRange: NSRange) -> Bool {        // **Perform sign in action here**        return false    }}
总结

以上是内存溢出为你收集整理的ios – 使用swift在UILabel中单击事件的NSAttributedString全部内容,希望文章能够帮你解决ios – 使用swift在UILabel中单击事件的NSAttributedString所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1005665.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-22
下一篇2022-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存