Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
niuxinhuai opened this issue Mar 25, 2019 · 3 comments
Open

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

niuxinhuai opened this issue Mar 25, 2019 · 3 comments

Comments

@niuxinhuai
Copy link

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的问题,请问我应该如何解决这个问题

@lyb5834
Copy link
Owner

lyb5834 commented Mar 25, 2019

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

@niuxinhuai
Copy link
Author

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

@MuRanJiangXia
Copy link

MuRanJiangXia commented Jan 6, 2020

你好 不走点击事件方法

  • (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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants