// // RoomChatScreenManager.swift // yinmeng-ios // // Created by duoban on 2024/3/23. // import UIKit import Nuke class RoomChatScreenManager: NSObject { func getSendGfitAtt(model:ChatCustomMsgModel,sendText:String)->NSMutableAttributedString?{ if let model = Deserialized.toModel(with: model.data) { let giftData = model.gift == nil ? model.giftInfo : model.gift let giftUrl = giftData?.giftUrl ?? "" let att = NSMutableAttributedString() att.append(getTextAtt(text: model.nick, color: ThemeColor(hexStr: "#FFD98C"), font: 12)) att.append(getPlaceholderAtt(width: 2)) att.append(getTextAtt(text: sendText, color: ThemeColor(hexStr: "#fffffe",alpha: 0.5), font: 12)) att.append(getPlaceholderAtt(width: 2)) att.append(getTextAtt(text: model.targetNick, color: ThemeColor(hexStr: "#FFD98C"), font: 12)) att.append(getPlaceholderAtt(width: 2)) att.append(getImageAtt(url: giftUrl)) att.append(getPlaceholderAtt(width: 2)) att.append(getTextAtt(text: "X\(model.giftNum)", color: .white, font: 12)) return att } return nil } func getSendAllGfitAtt(model:ChatCustomMsgModel,sendText:String)->NSMutableAttributedString?{ if let model = Deserialized.toModel(with: model.data) { let giftData = model.gift == nil ? model.giftInfo : model.gift let giftUrl = giftData?.giftUrl ?? "" let att = NSMutableAttributedString() att.append(getTextAtt(text: model.nick, color: ThemeColor(hexStr: "#FFD98C"), font: 12)) att.append(getPlaceholderAtt(width: 2)) att.append(getTextAtt(text: sendText, color: ThemeColor(hexStr: "#fffffe",alpha: 0.5), font: 12)) att.append(getPlaceholderAtt(width: 2)) att.append(getImageAtt(url: giftUrl)) att.append(getPlaceholderAtt(width: 2)) att.append(getTextAtt(text: "X\(model.giftNum)", color: .white, font: 12)) return att } return nil } func getPlaceholderAtt(width:CGFloat)->NSMutableAttributedString{ let placeholderView = UIView() placeholderView.backgroundColor = .clear let width = placeholderView.bounds.size.width let height = placeholderView.bounds.size.height placeholderView.bounds = CGRect(x: 0, y: 0, width: UIDevice.scaleWidth(width: width), height: UIDevice.scaleWidth(width: 10)) let att = NSMutableAttributedString.yy_attachmentString(withContent: placeholderView, contentMode: .scaleAspectFit, attachmentSize: CGSize(width: width, height: height), alignTo: UIFont.getScaleFont(ofSize: 15, weight: .regular), alignment: .center) return att } func getTextAtt(text:String,color:UIColor,font:CGFloat)->NSMutableAttributedString{ let att = NSMutableAttributedString(string: text) att.yy_font = UIFont.getScaleFont(ofSize: font, weight: .regular) att.yy_color = color att.yy_paragraphStyle = getParagraphStyle() return att } func getParagraphStyle()->NSMutableParagraphStyle{ let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = UIDevice.scaleWidth(width: 4) paragraphStyle.alignment = .left paragraphStyle.baseWritingDirection = .leftToRight return paragraphStyle } func getImageAtt(url:String)->NSMutableAttributedString{ let imageView = UIImageView() Nuke.loadImage(with: url, into: imageView, completion: nil) var bounds = CGRect(x: 0, y: 0, width:UIDevice.scaleWidth(width: 16) , height: UIDevice.scaleWidth(width: 16)) if let image = imageView.image { let scale = image.size.width / (image.size.height == 0 ? 1 : image.size.height) bounds = CGRect(x: 0, y: 0, width:UIDevice.scaleWidth(width: 16) * scale , height: UIDevice.scaleWidth(width: 16)) } imageView.bounds = bounds imageView.layer.masksToBounds = true imageView.contentMode = .scaleAspectFit let width = imageView.bounds.size.width let height = imageView.bounds.size.height let att = NSMutableAttributedString.yy_attachmentString(withContent: imageView, contentMode: .scaleAspectFit, attachmentSize: CGSize(width:width , height: height), alignTo: UIFont.getScaleFont(ofSize: 15, weight: .regular), alignment: .center) return att } }