|
| 1 | +// |
| 2 | +// IMUICardMessageCell.swift |
| 3 | +// RCTAuroraIMUI |
| 4 | +// |
| 5 | +// Created by Dowin on 2017/10/23. |
| 6 | +// Copyright © 2017年 HXHG. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | + |
| 10 | +import UIKit |
| 11 | + |
| 12 | +class IMUICardMessageCell: IMUIBaseMessageCell { |
| 13 | + |
| 14 | + var backView = UIView() |
| 15 | + var iconImg = MyCacheImageView() |
| 16 | + var nameLable = UILabel() |
| 17 | + var lineView = UIView() |
| 18 | + var titleLable = UILabel() |
| 19 | + let screenW = UIScreen.main.bounds.size.width |
| 20 | + |
| 21 | + override init(frame: CGRect) { |
| 22 | + super.init(frame: frame) |
| 23 | + backView.backgroundColor = UIColor.white |
| 24 | + iconImg.contentMode = UIViewContentMode.scaleAspectFill |
| 25 | + nameLable.textColor = UIColor.black |
| 26 | + nameLable.font = UIFont.systemFont(ofSize: (screenW * 18 / 375)) |
| 27 | + lineView.backgroundColor = UIColor.init(red: 230/255.0, green: 230/255.0, blue: 230/255.0, alpha: 1.0) |
| 28 | + titleLable.textColor = UIColor.gray |
| 29 | + titleLable.font = UIFont.systemFont(ofSize: (screenW * 11 / 375)) |
| 30 | + |
| 31 | + bubbleView.addSubview(backView) |
| 32 | + backView.addSubview(iconImg) |
| 33 | + backView.addSubview(nameLable) |
| 34 | + backView.addSubview(lineView) |
| 35 | + backView.addSubview(titleLable) |
| 36 | + } |
| 37 | + |
| 38 | + required init?(coder aDecoder: NSCoder) { |
| 39 | + fatalError("init(coder:) has not been implemented") |
| 40 | + } |
| 41 | + |
| 42 | + override func layoutSubviews() { |
| 43 | + super.layoutSubviews() |
| 44 | + } |
| 45 | + |
| 46 | + override func presentCell(with message: IMUIMessageModelProtocol, viewCache: IMUIReuseViewCache , delegate: IMUIMessageMessageCollectionViewDelegate?) { |
| 47 | + super.presentCell(with: message, viewCache: viewCache, delegate: delegate) |
| 48 | + let layout = message.layout |
| 49 | + |
| 50 | + self.backView.frame = CGRect(origin: CGPoint.zero, size: layout.bubbleFrame.size) |
| 51 | + let tmpDict = message.customDict |
| 52 | + let strTitle = tmpDict.object(forKey: "type") as! String |
| 53 | + let name = tmpDict.object(forKey: "name") as! String |
| 54 | + let imgPath = tmpDict.object(forKey: "imgPath") as! String |
| 55 | + titleLable.text = strTitle |
| 56 | + nameLable.text = name |
| 57 | + iconImg.setImageURL(imgPath, placeholderImage: "defaultHead") |
| 58 | + |
| 59 | + let lineY = layout.bubbleFrame.size.height * 0.78 |
| 60 | + let contentX = layout.bubbleFrame.size.width * 0.06 |
| 61 | + let contentWH = layout.bubbleFrame.size.width * 0.2 |
| 62 | + let contentY = (lineY - contentWH)*0.5 |
| 63 | + iconImg.frame = CGRect(x:contentX,y:contentY,width:contentWH,height:contentWH) |
| 64 | + let nameX = contentX*2 + contentWH |
| 65 | + let nameW = layout.bubbleFrame.size.width - nameX |
| 66 | + nameLable.frame = CGRect(x:nameX,y:contentY,width:nameW,height:contentWH) |
| 67 | + |
| 68 | + lineView.frame = CGRect(x:0,y:lineY,width:layout.bubbleFrame.size.width,height:1) |
| 69 | + |
| 70 | + let tmpSize = self.heightWithFont(font: UIFont.systemFont(ofSize: (screenW * 11 / 375)), fixedWidth: layout.bubbleFrame.size.width, text: strTitle) |
| 71 | + let titleX = contentX + (contentWH - tmpSize.width)*0.5 |
| 72 | + let titleH = layout.bubbleFrame.size.height - lineY |
| 73 | + titleLable.frame = CGRect(x:titleX,y:lineY,width:tmpSize.width,height:titleH) |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + func heightWithFont(font : UIFont, fixedWidth : CGFloat, text : String) -> CGSize { |
| 78 | + guard text.characters.count > 0 && fixedWidth > 0 else { |
| 79 | + |
| 80 | + return CGSize.zero |
| 81 | + } |
| 82 | + let size = CGSize(width:fixedWidth, height:CGFloat(MAXFLOAT)) |
| 83 | + let rect = text.boundingRect(with: size, options:.usesLineFragmentOrigin, attributes: [NSFontAttributeName : font], context:nil) |
| 84 | + |
| 85 | + return rect.size |
| 86 | + } |
| 87 | + |
| 88 | +} |
| 89 | + |
| 90 | + |
0 commit comments