Swift3.0仿微信的評論回復(fù)功能

最近項(xiàng)目的社區(qū)模塊,有需要用到類似于微信的評論回復(fù)列表,可是以前沒有做過,怎么辦? 還能怎么辦當(dāng)然是找度娘了,所幸的是還真被我找到了,在這里貼上 gitHub 地址 : [ https://github.com/suqiang/commentDemo ],不過我現(xiàn)在用的是 Swift3.0編寫代碼,所以我在 commentDemo的思路下,自己用 Swift實(shí)現(xiàn)了該功能,具體思路如下:

首先,我們將接口請求到的數(shù)據(jù),用循環(huán)的方式,遍歷所有的字典數(shù)據(jù), 創(chuàng)建一個(gè) ViewModel[ TDCommentCellModel ],該類用于設(shè)計(jì) Cell布局,同時(shí)設(shè)置評論 / 回復(fù) 功能

class TDCommentCellModel: NSObject { ? ?

var tdMessageModel: MessageCommentModel?///一條評論數(shù)據(jù)的模型 ? ? ? ?

var commentCellModels: [TDCommentListCellModel]?///存儲每一個(gè) cell 的 TDCommentCellModel ? ? ? ?

///對 cell 各個(gè)子 view 元素布局屬性 ? ?

var iconFrame: CGRect? ? ? ? ?

var nameLabelFrame: CGRect? ? ? ?

?var timeFrame: CGRect? ? ? ? ?

var commentBtnFrame: CGRect? ? ? ?

var contentFrame: CGRect? ? ? ? ?

var tableviewFrame: CGRect? ? ? ? ?

///緩存Cell 的高度 ? ?

var cellHeight: CGFloat = 0 ? ? ?

func initWithCommentCellModel(_ model:MessageCommentModel){ ? ? ?

//賦值模型 ? ? ?

self.tdMessageModel = model ? ? ? ?

//邊緣距離 ? ? ? ?

let margin:CGFloat = 15 ? ? ? ?

//評論框的寬度 ? ? ?

let contentW = ScreenWidth-2*margin ? ? ? ?// ? ? ? ?

let iconX:CGFloat = 10 ? ? ? ?

let iconY:CGFloat = margin ? ? ? ?

let iconSize:CGFloat = 37 ? ? ? ? ? ? ? ?

self.iconFrame = CGRect(x:iconX, y:iconY, width:iconSize, height:iconSize) ? ? ? ? ? ? ?

let nameLabelX:CGFloat = (self.iconFrame?.maxX)!+margin ? ? ? ?

let nameLabelW:CGFloat = ScreenWidth-57 ? ? ?

let nameLabelH:CGFloat = 14 ? ? ? ? ? ? ? ?

let timeLabelX = nameLabelX ? ? ? ?

let timeLabelW:CGFloat = ScreenWidth-57 ? ? ? ?

let timeLabelH:CGFloat = 12 ? ? ? ? ? ? ?

let commentBtnX:CGFloat = ScreenWidth-47 ? ? ?

let commentBtnY:CGFloat = iconY ? ? ? ?

let commentBtnSize:CGFloat = 37 ? ? ? ? ? ? ? ?

self.commentBtnFrame = CGRect(x:commentBtnX, y:commentBtnY, width:commentBtnSize, height:commentBtnSize) ? ? ? ? ? ? ?

let commentLabelX:CGFloat = timeLabelX ? ? ? ?

let commentLabelW:CGFloat = contentW-margin-iconSize ? ? ? ?

var commentLabelH:CGFloat = 0 ? ? ? ?

///計(jì)算評論內(nèi)容的高度 ? ? ? ?

commentLabelH = getTitleSize(model.content!, titleFont: 14, titleSize: CGSize(width:commentLabelW, height:CGFloat(MAXFLOAT))).height ? ? ? ? ? ? ? ? ? ? ?

let tableViewX:CGFloat = commentLabelX ? ? ?

let tableViewW:CGFloat = ScreenWidth-margin-tableViewX ? ? ? ?

var tableViewH:CGFloat = 0 ? ? ? ? ? ? ?

let commentTitle = model.list!//所有的評論回復(fù)數(shù)據(jù) ? ? ? ? ? ? ?

var mutableListTitle = [TDCommentListCellModel]() ? ? ? ? ? ? ?

if commentTitle.count > 0 {// ? ? ? ? ? ?print("111111") ? ? ? ? ?

for index in 0..<commentTitle.count {

self.cellHeight += margin

self.nameLabelFrame = CGRect(x:nameLabelX, y:self.cellHeight, width:nameLabelW, height:nameLabelH)

self.cellHeight += nameLabelH

}

if timeLabelH > 0 {

self.cellHeight += margin*0.5

self.timeFrame = CGRect(x:timeLabelX, y:self.cellHeight, width:timeLabelW, height:timeLabelH)

self.cellHeight += timeLabelH

}

if commentLabelH > 0 {

self.cellHeight += margin*0.5

self.contentFrame = CGRect(x:commentLabelX, y:self.cellHeight, width:commentLabelW, height:commentLabelH)

self.cellHeight += commentLabelH

}

// ? ? ? ?print("出來 for循環(huán)之后tableViewH的高度 ?:\(tableViewH)")

if tableViewH > 0 {

self.cellHeight += margin*0.5

self.tableviewFrame = CGRect(x:tableViewX, y:self.cellHeight, width:tableViewW, height:tableViewH)

self.cellHeight += tableViewH

}else {

self.tableviewFrame = CGRect(x:0, y:0, width:0, height:0)

}

self.cellHeight += margin*0.5

}

}

而其中的 TDCommentListCellModel類,目的在于設(shè)置評論回復(fù)功能的一些屬性,

class TDCommentListCellModel: NSObject {

var maxWidth: CGFloat?

var commentAttributedString: NSMutableAttributedString?

var commentLabelFrame: CGRect?

var cellHeight: CGFloat?

var commentModel: CommentListModel? {

didSet{

let margin:CGFloat = 5

let contentLabelX:CGFloat = margin

let contentLabelY:CGFloat = 4

let contentLabelW:CGFloat = self.maxWidth!-2*margin

var contentLabelH:CGFloat = 0

這一部分的判斷依據(jù)項(xiàng)目的實(shí)際情況和接口返回的數(shù)據(jù)結(jié)構(gòu)來決定

var contentState: String = ""

if (((commentModel?.replay_name!)! as NSString).length > 0 && ((commentModel?.comment_name!)! != (commentModel?.replay_name!)!)) {

contentState = "\((commentModel?.comment_name!)!)回復(fù)\((commentModel?.replay_name!)!): \((commentModel?.content!)!)"

}else {

contentState = "\((commentModel?.comment_name!)!): \((commentModel?.content!)!)"

}

let paragraphStyle = NSMutableParagraphStyle()

paragraphStyle.lineSpacing = 4

let attr = [NSParagraphStyleAttributeName:paragraphStyle, NSFontAttributeName:UIFont.systemFont(ofSize: 14), NSForegroundColorAttributeName:UIColor(red:135/255.0, green:135/255.0, blue:135/255.0, alpha:1.0)]

self.commentAttributedString = NSMutableAttributedString(string:contentState, attributes:attr)

contentLabelH = (self.commentAttributedString?.boundingRect(with: CGSize(width:contentLabelW, height:CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.usesLineFragmentOrigin, context: nil).size.height)!

self.commentLabelFrame = CGRect(x:contentLabelX, y:contentLabelY, width:contentLabelW, height:contentLabelH)

self.cellHeight = (self.commentLabelFrame?.maxY)!+2

}

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容