正則查出范圍---根據(jù)文字字符串創(chuàng)建imge--創(chuàng)建屬性字符串(帶圖片的atachment)---屬性字符串替換原來(lái)的表情根據(jù)位置
進(jìn)入房間--離開(kāi)房間---名字變色
class func GenerateEnterOrLeaveRoom(_ isEnter : Bool, _ username : String) -> NSAttributedString {
// coderwhy 進(jìn)入房間
// 1.拼接完整的消息
let message = username + (isEnter ? " 進(jìn)入房間" : " 離開(kāi)房間")
// 2.根據(jù)String創(chuàng)建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
}
聊天消息---帶圖片
步驟:正則匹配[]-->匹配出數(shù)組--->遍歷數(shù)組-->取出圖片字符串位置--創(chuàng)建圖片---替換字符串中的位置
class func generateChatMessage(_ username : String, _ message : String, _ font : UIFont) -> NSAttributedString {
// 1.拼接完整的消息
let chatMessage = username + ":" + message
// 2.根據(jù)string創(chuàng)建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.使用正則表達(dá)式, 將表情對(duì)應(yīng)的文字匹配出來(lái)? [1379]
let pattern = "\\[.*?\\]" ? ? ?.去除特殊字符 ?* 數(shù)量不同 ??遇到第一個(gè)就截止
guard let regex = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive) else {
return chatAttrString
}
// 4.2.匹配結(jié)果
let range = NSRange(location: 0, length: chatMessage.characters.count)
let results = regex.matches(in: chatMessage, options: [], range: range)
// 4.3.遍歷所有的結(jié)果---反轉(zhuǎn)遍歷--證遍歷range就不對(duì)了--文字替換成表情字符數(shù)不同
for i in (0..<results.count).reversed() {
// 4.4.獲取result對(duì)應(yīng)的問(wèn)題
let result = results[i]
let imageName = (chatMessage as NSString).substring(with: result.range)
// 4.5.根據(jù)imageName創(chuàng)建對(duì)應(yīng)的UIImage對(duì)象
guard let image = UIImage(named: imageName) else {
continue
}
// 4.6.根據(jù)圖片創(chuàng)建NSTextAttachment
let attachment = NSTextAttachment()
attachment.image = image
attachment.bounds = CGRect(x: 0, y: -4, width: font.lineHeight, height: font.lineHeight)
// 4.7.根據(jù)NSTextAttachment創(chuàng)建NSAttributedString
let imageAttrStr = NSAttributedString(attachment: attachment)
// 4.8.將之前的文字, 替換成當(dāng)前imageAttrStr
chatAttrString.replaceCharacters(in: result.range, with: imageAttrStr)
}
return chatAttrString
}
送禮物 圖文混排
class func generateGiftMessage(_ username : String, _ giftName : String, _ giftURL : String) -> NSAttributedString {
// 1.拼接完整的字符串
let giftMessage = username + " 贈(zèng)送給主播 " + giftName
// 2.根據(jù)String創(chuàng)建NSMutableAttributedString
let giftAttrString = NSMutableAttributedString(string: giftMessage)
// 3.設(shè)置名字的顏色
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.根據(jù)giftURL創(chuàng)建UIImage對(duì)象
guard let image = KingfisherManager.shared.cache.retrieveImageInMemoryCache(forKey: giftURL) else {
return giftAttrString
}
// 6.創(chuàng)建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)
}