欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

【Swift】获取UILabel中点击的某个功能标签文字并作出响应动作

程序员文章站 2022-07-02 15:42:21
1、需求 首先、针对UILabel中显示的多个功能标签,作出颜色标记提示。 其次、对关键字作出点击响应动作。 如图所示: 解决: 1、使用正则匹配到关键字 public static var hashtag = DetectorType.custom(try! NSRegularExpression ......

1、需求

首先、针对uilabel中显示的多个功能标签,作出颜色标记提示。

其次、对关键字作出点击响应动作。

如图所示:

【Swift】获取UILabel中点击的某个功能标签文字并作出响应动作

  解决:

1、使用正则匹配到关键字

public static var hashtag = detectortype.custom(try! nsregularexpression(pattern: "#[\\u4e00-\\u9fa5a-za-z0-9]{1,}", options: []))

扩展:获取字符串中满足正则表达式的字符串

func matches(for regex: string, in text: string) -> [string] {
        do {
            let regex = try nsregularexpression(pattern: regex)
            let results = regex.matches(in: text,
                                        range: nsrange(text.startindex..., in: text))
            return results.map {
                string(text[range($0.range, in: text)!])
            }
        } catch let error {
            print("invalid regex: \(error.localizeddescription)")
            return []
        }
    }
tags = commonuseclass._sharedmanager.matches(for: "#[\\u4e00-\\u9fa5a-za-z0-9]{1,}", in: model.text)

2、对关键字作出点击手势判断

extension uigesturerecognizer {

    func didtapattributedtextinlabel(label: uilabel, inrange targetrange: nsrange) -> bool {
        let layoutmanager = nslayoutmanager()
        let textcontainer = nstextcontainer(size: cgsize.zero)
        let textstorage = nstextstorage(attributedstring: label.attributedtext!)

        layoutmanager.addtextcontainer(textcontainer)
        textstorage.addlayoutmanager(layoutmanager)

        textcontainer.linefragmentpadding = 0.0
        textcontainer.linebreakmode = label.linebreakmode
        textcontainer.maximumnumberoflines = label.numberoflines
        let labelsize = label.bounds.size
        textcontainer.size = labelsize

        let locationoftouchinlabel = self.location(in: label)
        let textboundingbox = layoutmanager.usedrect(for: textcontainer)
        let textcontaineroffset = cgpoint(x: (labelsize.width - textboundingbox.size.width) * 0.5 - textboundingbox.origin.x, y: (labelsize.height - textboundingbox.size.height) * 0.5 - textboundingbox.origin.y)
        let locationoftouchintextcontainer = cgpoint(x: locationoftouchinlabel.x - textcontaineroffset.x, y: locationoftouchinlabel.y - textcontaineroffset.y)
        let indexofcharacter = layoutmanager.characterindex(for: locationoftouchintextcontainer, in: textcontainer, fractionofdistancebetweeninsertionpoints: nil)
        return nslocationinrange(indexofcharacter, targetrange)
    }

}

3、获取点击的字符串,并作出响应

if self.tags.count > 0 {
                var isselecttag = false
                var selecttag = ""
                for tag in self.tags {
                    let termsrange = (self.messagetext as nsstring).range(of: tag)
                    if gesture.didtapattributedtextinlabel(label: self.messagelabel, inrange: termsrange) {
                        isselecttag = true
                        selecttag = tag
                        break
                    }
                }
                
                if isselecttag {
                    delegate?.selecttag(selecttag)
                    return
                }
            }

 转自:https://www.cnblogs.com/xjf125/p/12852743.html