• swift检测字符串是否在数组字符串中


    https://www.jianshu.com/p/56da83a4e0ab  

    /// 检测到敏感词标红

        private func richTextInputChange(text: NSMutableAttributedString,word: String) -> NSMutableAttributedString {

            let range = (text.string as NSString).range(of: word)

            return applyRichTextInputChange(text: text, word: word, range: range, last: range)

        }

        

        private func applyRichTextInputChange(text: NSMutableAttributedString,word: String,range: NSRange,last: NSRange) -> NSMutableAttributedString {

            if range.location != NSNotFound {

                text.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.State.fail, range: range)

                text.addAttribute(NSAttributedString.Key.font, value: 15.yp_font, range: range)

                let start = last.location + last.length

                let end = text.string.count+1 - start

                let stringRange = NSRange(location: start, length: end)

                let newString = text.string as NSString

                let newRange = newString.range(of: word, options: [], range: stringRange)

                let _ = applyRichTextInputChange(text: text, word: word, range: newRange, last: range)

            }

            return text

        }

    第二种方法

    extension YPFastRecruitHeaderView {

        /// 检查是否包含敏感词

        func contentHasSensitiveWord(textString: String){

            let words = YPFastIssueCofigWordModel.shared?.thesaurusList ?? []

            if let _ = words.first(where: {textString.contains($0)}) {

                contentHasSensitive.accept(true)

            }else{

                contentHasSensitive.accept(false)

            }

        }

        

        /// 检测到敏感词标红

        func richTextInputChange(text: NSMutableAttributedString,word: String) -> NSMutableAttributedString {

            return textRegex(pattern: word, attributeString: text, color: UIColor.State.fail)

        }

            

        // 1.匹配纯文本

        func textRegex(pattern: String,

                       attributeString: NSMutableAttributedString,

                       color: UIColor) -> NSMutableAttributedString{

            

            //富文本contentAttributeString

            let content = attributeString.string

            do {

                // 1.1.定义规则

                //let pattern = "ben"

                // 1.2.创建正则表达式对象

                let regex = try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.caseInsensitive)

                // 1.3.开始匹配

                let res = regex.matches(in: content, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, content.count))

                

                for checkingRes in res{

                    //设置字体颜色

                    attributeString.addAttribute(NSAttributedString.Key.foregroundColor, value: color,range: checkingRes.range)

                }

                return attributeString

                

            } catch {

                

                print(error)

            }

            return attributeString

        }

    }

  • 相关阅读:
    宣化上人:大佛顶首楞严经四种清净明诲浅释(4-5)(转自学佛网:http://www.xuefo.net/nr/article23/230699.html)
    宣化上人:大佛顶首楞严经四种清净明诲浅释(6-7)(转自学佛网:http://www.xuefo.net/nr/article23/230700.html)
    宣化上人: 大佛顶首楞严经四种清净明诲浅释(8-9)(转自学佛网:http://www.xuefo.net/nr/article23/230825.html)
    [我的CVE][CVE-2017-15708]Apache Synapse Remote Code Execution Vulnerability
    Ubuntu bash不记录history方法
    [我的CVE][CVE-2017-15709]Apache ActiveMQ Information Leak
    java.lang.Runtime.exec() Payload Workarounds
    CVE-2017-12149 JBOOS AS 6.X 反序列化漏洞利用
    Apache Continuum 远程命令执行漏洞
    docker 端口映射iptables: No chain/target/match by that name错误解决方法
  • 原文地址:https://www.cnblogs.com/supersr/p/15793000.html
Copyright © 2020-2023  润新知