//
|
// CommentContentView.swift
|
// OKProject
|
//
|
// Created by 无故事王国 on 2022/5/11.
|
// Copyright © 2022 yangwang. All rights reserved.
|
//
|
|
import UIKit
|
import RxSwift
|
import QMUIKit
|
|
class CommentContentView: UITableViewHeaderFooterView {
|
|
private var profileImg:UIImageView!
|
private var userNameL:UILabel!
|
private var contentL:UILabel!
|
private var timeL:UILabel!
|
private var replyBtn:UIButton!
|
private var showAllBtn:UIButton!
|
private var showMoreView:UIView!
|
private var showMoreBtn:QMUIButton!
|
private var commentModel:CommentModel?
|
private let lineView = UIView()
|
var section:Int!
|
|
var showAllDelegate = Delegate<Void,Void>()
|
var replyDelegate = Delegate<Void,Void>()
|
var showMoreDelegate = Delegate<Int,Void>()
|
var cellDisposeBag = DisposeBag()
|
|
override init(reuseIdentifier: String?) {
|
super.init(reuseIdentifier: reuseIdentifier)
|
setUI()
|
}
|
|
override func awakeFromNib() {
|
super.awakeFromNib()
|
setUI()
|
|
}
|
|
required init?(coder: NSCoder) {
|
super.init(coder: coder)
|
setUI()
|
}
|
|
private func setUI(){
|
backgroundColor = .white
|
contentView.backgroundColor = .white
|
profileImg = UIImageView()
|
profileImg.contentMode = .scaleToFill
|
profileImg.backgroundColor = .gray
|
profileImg.cornerRadius = 19
|
profileImg.maskToBounds = true
|
addSubview(profileImg)
|
profileImg.snp.makeConstraints { make in
|
make.left.equalTo(15)
|
make.top.equalTo(20)
|
make.width.height.equalTo(38)
|
}
|
|
userNameL = UILabel()
|
userNameL.font = UIFont.systemFont(ofSize: 14, weight: .semibold)
|
userNameL.textColor = UIColor.black.withAlphaComponent(0.8)
|
addSubview(userNameL)
|
userNameL.snp.makeConstraints { make in
|
make.top.equalTo(profileImg)
|
make.left.equalTo(profileImg.snp.right).offset(9)
|
make.height.equalTo(20)
|
}
|
|
contentL = UILabel()
|
contentL.font = UIFont.systemFont(ofSize: 11)
|
contentL.numberOfLines = 0
|
contentL.textColor = UIColor.black.withAlphaComponent(0.8)
|
addSubview(contentL)
|
contentL.snp.makeConstraints { make in
|
make.top.equalTo(userNameL.snp.bottom).offset(1)
|
make.left.equalTo(userNameL)
|
make.right.equalTo(-13)
|
make.height.greaterThanOrEqualTo(0)
|
}
|
|
showAllBtn = UIButton(type: .custom)
|
showAllBtn.backgroundColor = .white
|
showAllBtn.contentHorizontalAlignment = .right
|
showAllBtn.setTitle("全文", for: .normal)
|
showAllBtn.addTarget(self, action: #selector(showAllAction), for: .touchUpInside)
|
showAllBtn.setTitleColor(UIColor(hexString: "#00BF30"), for: .normal)
|
showAllBtn.titleLabel?.font = UIFont.systemFont(ofSize: 12,weight: .medium)
|
addSubview(showAllBtn)
|
showAllBtn.snp.makeConstraints { make in
|
make.right.equalTo(contentL.snp.right).offset(-7)
|
make.bottom.equalTo(contentL.snp.bottom).offset(2.5)
|
make.height.equalTo(17)
|
}
|
|
timeL = UILabel()
|
timeL.font = UIFont.systemFont(ofSize: 12)
|
timeL.textColor = UIColor(hexString: "#666666")
|
addSubview(timeL)
|
timeL.snp.makeConstraints { make in
|
make.left.equalTo(userNameL)
|
make.top.equalTo(contentL.snp.bottom).offset(10)
|
make.height.equalTo(17)
|
}
|
|
replyBtn = UIButton(type: .custom)
|
replyBtn.setTitle("回复", for: .normal)
|
replyBtn.setTitleColor(UIColor(hexString: "#666666"), for: .normal)
|
replyBtn.titleLabel?.font = UIFont.systemFont(ofSize: 12)
|
replyBtn.addTarget(self, action: #selector(replyAction), for: .touchUpInside)
|
addSubview(replyBtn)
|
replyBtn.snp.makeConstraints { make in
|
make.centerY.equalTo(timeL)
|
make.left.equalTo(timeL.snp.right).offset(11)
|
}
|
|
showMoreView = UIView()
|
showMoreView.backgroundColor = .white
|
addSubview(showMoreView)
|
showMoreView.snp.makeConstraints { make in
|
make.left.equalTo(userNameL)
|
make.top.equalTo(timeL.snp.bottom).offset(8)
|
make.height.equalTo(17)
|
make.right.equalTo(contentL.snp.right)
|
}
|
|
lineView.backgroundColor = .black.withAlphaComponent(0.1)
|
showMoreView.addSubview(lineView)
|
lineView.snp.makeConstraints { make in
|
make.left.equalToSuperview()
|
make.centerY.equalToSuperview()
|
make.height.equalTo(0.6)
|
make.width.equalTo(25)
|
}
|
|
showMoreBtn = QMUIButton(type: .custom)
|
showMoreBtn.setTitleColor(UIColor(hexString: "#666666"), for: .normal)
|
showMoreBtn.titleLabel?.font = UIFont.systemFont(ofSize: 12)
|
showMoreBtn.setImage(UIImage(named: "icon_arrow_down"), for: .normal)
|
showMoreBtn.addTarget(self, action: #selector(showMoreAction), for: .touchUpInside)
|
showMoreView.addSubview(showMoreBtn)
|
showMoreBtn.snp.makeConstraints { make in
|
make.centerY.equalTo(lineView)
|
make.left.equalTo(lineView.snp.right).offset(8)
|
}
|
}
|
|
func setCommentModel(_ model:CommentModel){
|
commentModel = model
|
contentL.text = model.content
|
contentL.sizeToFit()
|
profileImg.load(url: model.userAvatar,placeHolder: UIImage(named: "logo")!)
|
if model.userName.isEmpty{
|
userNameL.text = "佚名"
|
}else{
|
userNameL.text = model.userName
|
}
|
|
//74184 [招聘详情]评论时间显示与需求不符
|
let createTime = DateClass.timeStringToDate(model.createTime)
|
let diffday = DateClass.dateDifference(Date(), from: createTime)
|
if diffday > 1.0{
|
timeL.text = String(format: "%@", DateClass.dateToDateString(createTime, dateFormat: "yyyy/MM/dd"))
|
}else{
|
let time = DateClass.timeStringToDate(model.createTime).timeIntervalSince1970 * 1000
|
timeL.text = String(format: "%@", DateClass.compareCurrentTime(str: "\(time)"))
|
}
|
|
showMoreBtn.isHidden = !(model.replyCommentList.count > 0)
|
lineView.isHidden = !(model.replyCommentList.count > 0)
|
showMoreBtn.setTitle("展开\(model.replyCommentList.count)条回复", for: .normal)
|
contentL.numberOfLines = model.showAll ? 0:3
|
|
if model.replyCommentList.count == 0 || model.showOther{
|
showMoreView.isHidden = true
|
showMoreView.snp.remakeConstraints { make in
|
make.left.equalTo(userNameL)
|
make.top.equalTo(timeL.snp.bottom).offset(8)
|
make.height.equalTo(0)
|
make.right.equalTo(contentL.snp.right)
|
}
|
}else{
|
showMoreView.isHidden = false
|
showMoreView.snp.remakeConstraints { make in
|
make.left.equalTo(userNameL)
|
make.top.equalTo(timeL.snp.bottom).offset(8)
|
make.height.equalTo(17)
|
make.right.equalTo(contentL.snp.right)
|
}
|
}
|
|
|
let attribute = AttributedStringbuilder.build()
|
contentL.attributedText = attribute.add(string: model.content, withFont: UIFont.systemFont(ofSize: 12), withColor: .black.withAlphaComponent(0.8), lineSpace: 5).mutableAttributedString
|
|
let hei = attribute.mutableAttributedString.heightOfAttributedString(ScreenWidth - 75)
|
model.contentHeight = hei
|
|
//大于3行,被截断
|
let line = ceil(hei / 18.0)
|
if line >= 3 && !model.showAll{
|
showAllBtn.isHidden = false
|
}else{
|
showAllBtn.isHidden = true
|
}
|
contentL.lineBreakMode = .byTruncatingTail
|
}
|
|
@objc func showAllAction(){
|
commentModel?.showAll = true
|
contentL.numberOfLines = 0
|
showAllBtn.isHidden = true
|
showAllDelegate.call()
|
}
|
|
@objc func showMoreAction(){
|
commentModel?.showOther = true
|
showMoreDelegate.call(section)
|
}
|
|
@objc func replyAction(){
|
//一级回复
|
let commentView = PublishCommentView.show({[weak self]text in
|
guard let weakSelf = self else { return }
|
|
let id = weakSelf.commentModel!.id
|
let type = weakSelf.commentModel!.type.rawValue
|
let orderId = weakSelf.commentModel!.orderId
|
let replyUserId = weakSelf.commentModel!.userId
|
|
APIManager.shared.provider.rx.request(.insertComment(commentId: id, content: text, orderId: orderId, type: type, replyUserId: replyUserId)).map(YYModel<Nothing>.self).validate().subscribe(onSuccess: {data in
|
weakSelf.replyDelegate.call()
|
}) { error in
|
alert(text: "回复失败")
|
}.disposed(by: weakSelf.cellDisposeBag)
|
})
|
|
commentView.textView.placeholder = "回复\(commentModel?.userName ?? "")"
|
}
|
}
|
|
extension UILabel{
|
|
/// 获取行数
|
func getRealLabelLines()->Int{
|
guard let labelText = text else{
|
return 0
|
}
|
|
let rect = CGSize(width: self.bounds.width, height: CGFloat.greatestFiniteMagnitude)
|
let labelTextSize = (labelText as NSString).boundingRect(with: rect, options: .usesFontLeading, attributes: [NSAttributedString.Key.font:self.font!], context: nil)
|
let labelTextLines = Int(ceil(CGFloat(labelTextSize.height) / self.font.lineHeight))
|
return labelTextLines
|
}
|
|
var isTruncated:Bool{
|
guard let labelText = text else { return false }
|
|
let rect = CGSize(width: self.bounds.width, height: CGFloat.greatestFiniteMagnitude)
|
let labelTextSize = (labelText as NSString).boundingRect(with: rect, options: .usesFontLeading, attributes: [NSAttributedString.Key.font:self.font!], context: nil)
|
let labelTextLines = Int(ceil(CGFloat(labelTextSize.height) / self.font.lineHeight))
|
|
var labelShowLines = Int(floor(CGFloat(bounds.size.height) / self.font.lineHeight))
|
if self.numberOfLines != 0{
|
labelShowLines = min(labelShowLines,self.numberOfLines)
|
}
|
|
return labelTextLines > labelShowLines
|
}
|
}
|