[置顶] iOS开发进阶 - 富文本正则替换表情

标签: ios开发正则表达式
1288人阅读 评论(0) 收藏 举报
分类:

移动端访问不佳,请访问我的个人博客

最近写项目需要用到富文本解析字符串显示表情,下面是我使用正则替换实现富文本的方式,希望能帮助到大家

先上效果图和demo地址

这里写图片描述

实现过程中需要用到的知识点

  • NSRegularExpression(正则表达式)
  • NSMutableAttributedString(用来显示富文本的string)

废话不多说,直接贴代码:

import UIKit

struct WCLEmojiParse {

    //所有表情对应的字符串
    static let emotions = ["[angry]", "[beers]", "[blush]", "[bomb]", "[cool]", "[flushed]", "[grin]", "[gun]", "[heart]", "[heartseyes]", "[imp]", "[Joy]", "[kiss]", "[ok]", "[persevere]", "[pray]", "[punch]", "[scream]", "[shit]", "[skull]", "[sleeping]", "[smiley]", "[smirk]", "[sob]","[sweat]", "[thumbsup]", "[tongue]", "[unamused]", "[v]", "[weary]", "[wink]", "[yum]"]

    //String的格式
    static let textAttributes = [NSFontAttributeName: UIFont.systemFont(ofSize: 16), NSForegroundColorAttributeName: UIColor.black]
    //正则表达式的格式
    static let pattern = "\\[+[a-z]+\\]"
    //表情的bounds
    static let attachmentBounds = CGRect.init(origin: CGPoint.init(x: 0, y: -5), size: CGSize.init(width: 24, height: 24))

    //MARK: Public Methods
    static func replaceEmoji(_ str: String) -> NSAttributedString {
        //转成NSString
        let originalNSString = str as NSString
        //通过str获得NSMutableAttributedString
        let attStr = NSMutableAttributedString.init(string: str, attributes: textAttributes)
        var regex: NSRegularExpression?
        do {
            regex = try NSRegularExpression.init(pattern: pattern, options: .caseInsensitive)
        } catch let error as NSError {
            print(error.localizedDescription)
        }
        //获取到匹配正则的数据
        if let matches = regex?.matches(in: str, options: .withoutAnchoringBounds, range: NSMakeRange(0,attStr.string.characters.count)) {
            if matches.count > 0 {
                //遍历符合的数据进行解析
                for i in 0..<matches.count {
                    let result = matches[matches.count-i-1]
                    let range = result.range
                    let emojiStr = originalNSString.substring(with: range)
                    //符合的数据是否为表情
                    if emotions.contains(emojiStr) {
                        if let image = UIImage.init(named: emojiStr) {
                            //创建一个NSTextAttachment
                            let attachment    = NSTextAttachment()
                            attachment.image  = image
                            attachment.bounds = attachmentBounds
                            //通过NSTextAttachment生成一个NSAttributedString
                            let rep = NSAttributedString(attachment: attachment)
                            //把表情于之前的字符串替换
                            attStr.replaceCharacters(in: range, with: rep)
                        }
                    }
                }
            }
        }
        return attStr
    }
}

demo地址

以上是简单的富文本显示表情的方式,抛砖引玉,大家见笑了,希望大家能学到东西,谢谢大家的阅读

2
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:233334次
    • 积分:2959
    • 等级:
    • 排名:第11947名
    • 原创:67篇
    • 转载:8篇
    • 译文:0篇
    • 评论:161条
    GitHub
    博客专栏
    最新评论