Nic*_*rbo 1 nsattributedstring uilabel ios uitapgesturerecognizer swift
我需要在文本的某些部分添加一些tapGestures,即UILabel中的那些部分.它似乎能够创建一个超链接 - 所以我想它也可以做一个函数调用,但我只是不知道该怎么做.
继承我的代码:
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
Run Code Online (Sandbox Code Playgroud)
我希望turqoise部分能够将用户带到条款和条件,或私人政策.任何人都可以帮我解决这个问题吗?:))
我曾经做过同样的事情,但是使用textView代替UILabel,你应该创建自己的属性,并将它们添加到属性字符串,然后处理tap,这里有一些代码
你的代码与我的修改
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:))))
Run Code Online (Sandbox Code Playgroud)
和func处理水龙头
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新:
这是我写的使用Label的类
https://github.com/barbados88/TapableLabel/blob/master/TapableLabel.swift
| 归档时间: |
|
| 查看次数: |
1151 次 |
| 最近记录: |