gpt4 book ai didi

ios - 如何将函数调用(不是超链接)添加到 UILabel 中的 NSAttributedString 的一部分?

转载 作者:搜寻专家 更新时间:2023-11-01 05:50:03 24 4
gpt4 key购买 nike

我需要在 UILabel 中的文本的某些部分添加一些 tapGestures。它似乎能够创建一个超链接 - 所以我想它也可以进行函数调用,但我只是不确定该怎么做。

这是我的代码:

    let regularFont = UIFont(name: Constants.robotoRegular, size: 13)!
let att1 = [NSFontAttributeName: regularFont]

let turqoiseFont = UIFont(name: Constants.robotoBold, size: 13)!
let att2 = [NSFontAttributeName: turqoiseFont]

let attString1 = NSMutableAttributedString(string: "By creating a profile, I accept ", attributes: att1)
attString1.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkGrayColor(), range: NSMakeRange(0, attString1.length))

let attString2 = NSMutableAttributedString(string: "Terms and Conditions ", attributes: att2)
attString2.addAttribute(NSForegroundColorAttributeName, value: Colors.loginButtonColor, range: NSMakeRange(0, attString2.length))

let attString3 = NSMutableAttributedString(string: "and ", attributes: att1)
attString3.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkGrayColor(), range: NSMakeRange(0, attString3.length))

let attString4 = NSMutableAttributedString(string: "Private Policy.", attributes: att2)
attString4.addAttribute(NSForegroundColorAttributeName, value: Colors.loginButtonColor, range: NSMakeRange(0, attString4.length))

let index = "\(attString1.string.endIndex)"

attString1.insertAttributedString(attString4, atIndex: Int(index)!)
attString1.insertAttributedString(attString3, atIndex: Int(index)!)
attString1.insertAttributedString(attString2, atIndex: Int(index)!)

termsAndConditionLabel.attributedText = attString1

我希望绿松石部分能够将用户带到条款和条件或隐私政策。谁能帮我解决这个问题? :))

最佳答案

我曾经做过同样的事情,但是使用 textView 而不是 UILabel,你应该创建自己的属性,并将它们添加到属性字符串中,然后处理点击,这里是一些代码

你的代码和我的修改

   let regularFont = UIFont(name: "HelveticaNeue", size: 13)!
let att1 = [NSFontAttributeName: regularFont]

let turqoiseFont = UIFont(name: "HelveticaNeue", size: 13)!
let att2 = [NSFontAttributeName: turqoiseFont]
let mattString : NSMutableAttributedString = NSMutableAttributedString()
let attString1 = NSMutableAttributedString(string: "By creating a profile, I accept ", attributes: att1)
attString1.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkGray, range: NSMakeRange(0, attString1.length))
let attString2 = NSMutableAttributedString(string: "Terms and Conditions ", attributes: att2)
attString2.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSMakeRange(0, attString2.length))
let attString3 = NSMutableAttributedString(string: "and ", attributes: att1)
attString3.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkGray, range: NSMakeRange(0, attString3.length))
let attString4 = NSMutableAttributedString(string: "Private Policy.", attributes: att2)
attString4.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSMakeRange(0, attString4.length))
mattString.append(attString1)
mattString.append(attString2)
mattString.append(attString3)
mattString.append(attString4)
let attributes = ["link" : "termsLink"]
let attributes2 = ["link" : "policyLink"]
let str : NSString = mattString.string as NSString
mattString.addAttributes(attributes, range: str.range(of: "Terms and Conditions"))
mattString.addAttributes(attributes2, range: str.range(of: "Private Policy"))
_textView.attributedText = mattString
_textView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.singleTap(tap:))))

和处理点击的函数

func singleTap(tap : UITapGestureRecognizer) {
let layoutManager = _textView.layoutManager
let location = tap.location(in: _textView)
let index = layoutManager.characterIndex(for: location, in: _textView.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
if index > _textView.textStorage.length {return}
var range : NSRange = NSRange()
if let type = _textView.attributedText.attribute("link", at: index, effectiveRange: &range) as? String {
if type == "termsLink" {
//.. do smth
} else {
//.. do smth
}
}
}

更新:

这是我写的使用Label的类

https://github.com/barbados88/TapableLabel/blob/master/TapableLabel.swift

关于ios - 如何将函数调用(不是超链接)添加到 UILabel 中的 NSAttributedString 的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42433508/

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