YBAttributeTextTapAction icon indicating copy to clipboard operation
YBAttributeTextTapAction copied to clipboard

swift4中,出现点击区域不正确问题

Open niuxinhuai opened this issue 5 years ago • 3 comments

eg: let label = UILabel() let labelStr = """ 今天是个好日子 practice; custom; tradition; convention """ label.attributedText = NSMutableAttributedString.init(string: labelStr) label.yb_addAttributeTapAction(with:["practice","custom","tradition","convention"], delegate: self) r: 在代理方法中,打印,当我点击"tradition"的时候,打印输出显示string是convention的问题,请问我应该如何解决这个问题

niuxinhuai avatar Mar 25 '19 04:03 niuxinhuai

你好,我的框架必须设置字体才有效,例如 att_str.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 17), range: NSMakeRange(0, str.characters.count))

lyb5834 avatar Mar 25 '19 07:03 lyb5834

很感谢你的意见,我使用了字体之后,点击就正常了

niuxinhuai avatar Mar 25 '19 07:03 niuxinhuai

你好 不走点击事件方法

  • (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

、、、、、、、 let label = UILabel(frame: CGRect(x: 100 , y: 200, width: 100, height: 40)) label.backgroundColor = UIColor.cyan label.text = "text123456789" label.textColor = UIColor.black label.font = UIFont.systemFont(ofSize: 14) self.view.addSubview(label)

    let title = label.text ?? ""
    let attribute = NSMutableAttributedString(string: title)
    
    if let range = title.range(of: "123456789") {
        let range1 = title.toNSRange(range)
        // 设置颜色
        attribute.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.red], range: range1)
        attribute.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12), range: range1)
    }
    label.attributedText = attribute
    
    label.yb_addAttributeTapAction(with: ["123456789"]) { (label, text, range, index) in
        

MuRanJiangXia avatar Jan 06 '20 04:01 MuRanJiangXia