正则查出范围---根据文字字符串创建imge--创建属性字符串(带图片的atachment)---属性字符串替换原来的表情根据位置
进入房间--离开房间---名字变色
class func GenerateEnterOrLeaveRoom(_ isEnter : Bool, _ username : String) -> NSAttributedString {
// coderwhy 进入房间
// 1.拼接完整的消息
let message = username + (isEnter ? " 进入房间" : " 离开房间")
// 2.根据String创建NSMutableAttributedString
let attrMessage = NSMutableAttributedString(string: message)
// 3.修改名字的颜色
let nameRange = NSRange(location: 0, length: username.lengthOfBytes(using: .utf8))
attrMessage.setAttributes([NSForegroundColorAttributeName : UIColor.orange], range: nameRange)
return attrMessage
}
聊天消息---带图片
步骤:正则匹配[]-->匹配出数组--->遍历数组-->取出图片字符串位置--创建图片---替换字符串中的位置
class func generateChatMessage(_ username : String, _ message : String, _ font : UIFont) -> NSAttributedString {
// 1.拼接完整的消息
let chatMessage = username + ":" + message
// 2.根据string创建NSMutableAttributedString
let chatAttrString = NSMutableAttributedString(string: chatMessage)
// 3.将名字改成特殊的颜色
let nameRange = NSRange(location: 0, length: username.lengthOfBytes(using: .utf8))
chatAttrString.setAttributes([NSForegroundColorAttributeName : UIColor.orange], range: nameRange)
// 4.I [哈哈] my [嘿嘿]is [嘻嘻]why[呵呵]
// [哈哈] --> 图片名称 --> UIImage --> NSTextAttachment -> NSAttributedString
// 4.1.使用正则表达式, 将表情对应的文字匹配出来 [1379]
let pattern = "\\[.*?\\]" .去除特殊字符 * 数量不同 ?遇到第一个就截止
guard let regex = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive) else {
return chatAttrString
}
// 4.2.匹配结果
let range = NSRange(location: 0, length: chatMessage.characters.count)
let results = regex.matches(in: chatMessage, options: [], range: range)
// 4.3.遍历所有的结果---反转遍历--证遍历range就不对了--文字替换成表情字符数不同
for i in (0..<results.count).reversed() {
// 4.4.获取result对应的问题
let result = results[i]
let imageName = (chatMessage as NSString).substring(with: result.range)
// 4.5.根据imageName创建对应的UIImage对象
guard let image = UIImage(named: imageName) else {
continue
}
// 4.6.根据图片创建NSTextAttachment
let attachment = NSTextAttachment()
attachment.image = image
attachment.bounds = CGRect(x: 0, y: -4, width: font.lineHeight, height: font.lineHeight)
// 4.7.根据NSTextAttachment创建NSAttributedString
let imageAttrStr = NSAttributedString(attachment: attachment)
// 4.8.将之前的文字, 替换成当前imageAttrStr
chatAttrString.replaceCharacters(in: result.range, with: imageAttrStr)
}
return chatAttrString
}
送礼物 图文混排
class func generateGiftMessage(_ username : String, _ giftName : String, _ giftURL : String) -> NSAttributedString {
// 1.拼接完整的字符串
let giftMessage = username + " 赠送给主播 " + giftName
// 2.根据String创建NSMutableAttributedString
let giftAttrString = NSMutableAttributedString(string: giftMessage)
// 3.设置名字的颜色
let nameRange = NSRange(location: 0, length: username.lengthOfBytes(using: .utf8))
giftAttrString.setAttributes([NSForegroundColorAttributeName : UIColor.orange], range: nameRange)
// 4.将礼物的名称修改成特殊颜色
let giftNameRange = (giftMessage as NSString).range(of: giftName)
giftAttrString.setAttributes([NSForegroundColorAttributeName : UIColor.red], range: giftNameRange)
// 5.根据giftURL创建UIImage对象
guard let image = KingfisherManager.shared.cache.retrieveImageInMemoryCache(forKey: giftURL) else {
return giftAttrString
}
// 6.创建NSTextAttachment
let giftImageAttr = AttributeStrGenerator.getAttrStringWithImage(image, UIFont.systemFont(ofSize: 17.0))
// 7.将giftImageAttr拼接到giftAttrString
giftAttrString.append(giftImageAttr)
return giftAttrString
}
private class func getAttrStringWithImage(_ image : UIImage, _ font : UIFont) -> NSAttributedString {
let attachment = NSTextAttachment()
attachment.image = image
attachment.bounds = CGRect(x: 0, y: -4, width: font.lineHeight, height: font.lineHeight)
return NSAttributedString(attachment: attachment)
}