Commit e00556a3 authored by David黄金龙's avatar David黄金龙

首页 高度计算

parent 7a9071ae
...@@ -18,35 +18,13 @@ class YHHomePageViewController: YHBaseViewController { ...@@ -18,35 +18,13 @@ class YHHomePageViewController: YHBaseViewController {
}() }()
lazy var homeCollectView = { lazy var homeCollectView = {
// // 设置布局方向
// let flowLayout = UICollectionViewFlowLayout()
// let margin = 20.0
// let gap = 20.0
// let itemWidth = ceil((KScreenWidth - 2*margin - gap)/2.0)
// flowLayout.itemSize = CGSize(width: itemWidth, height: 210 + 87 + 4)
// flowLayout.minimumInteritemSpacing = 10.0
// flowLayout.minimumLineSpacing = 10.0
// flowLayout.scrollDirection = .vertical
//
// let collectinoView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
// collectinoView.backgroundColor = .white
// collectinoView.register(YHDavidTestCollectionViewCell.self, forCellWithReuseIdentifier:YHDavidTestCollectionViewCell.cellReuseIdentifier)
// collectinoView.delegate = self
// collectinoView.dataSource = self
// collectinoView.showsVerticalScrollIndicator = false
//
// collectinoView.translatesAutoresizingMaskIntoConstraints = false
// collectinoView.alwaysBounceVertical = true
//
// 设置布局方向 // 设置布局方向
let flowLayout = YHHomeCollectionLayout() let flowLayout = YHHomeCollectionLayout()
flowLayout.minimumInteritemSpacing = 7.0 flowLayout.minimumInteritemSpacing = 7.0
flowLayout.minimumColumnSpacing = 10.0 flowLayout.minimumColumnSpacing = 10.0
flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20) flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
let collectinoView = YHGestureCollectionView(frame: .zero, collectionViewLayout: flowLayout) let collectinoView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
// collectinoView.contentInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
collectinoView.backgroundColor = .white collectinoView.backgroundColor = .white
collectinoView.register(YHHomeCollectionViewCell.self, forCellWithReuseIdentifier:YHHomeCollectionViewCell.cellReuseIdentifier) collectinoView.register(YHHomeCollectionViewCell.self, forCellWithReuseIdentifier:YHHomeCollectionViewCell.cellReuseIdentifier)
collectinoView.delegate = self collectinoView.delegate = self
...@@ -82,7 +60,7 @@ class YHHomePageViewController: YHBaseViewController { ...@@ -82,7 +60,7 @@ class YHHomePageViewController: YHBaseViewController {
} }
func getData() { func getData() {
viewModel.getHomeList {[weak self] success, error in viewModel.getHomeNewsList {[weak self] success, error in
guard let self = self else { return } guard let self = self else { return }
self.homeCollectView.reloadData() self.homeCollectView.reloadData()
} }
...@@ -91,10 +69,10 @@ class YHHomePageViewController: YHBaseViewController { ...@@ -91,10 +69,10 @@ class YHHomePageViewController: YHBaseViewController {
self.homeHeaderView.homeBannerView.dataArr = self.viewModel.banners ?? [] self.homeHeaderView.homeBannerView.dataArr = self.viewModel.banners ?? []
} }
viewModel.getHomeClassify {[weak self] success, error in // viewModel.getHomeClassify {[weak self] success, error in
guard let self = self else { return } // guard let self = self else { return }
//
} // }
} }
} }
...@@ -119,10 +97,10 @@ private extension YHHomePageViewController { ...@@ -119,10 +97,10 @@ private extension YHHomePageViewController {
homeHeaderView.frame = CGRect(x: 20, y: -YHHomeHeadView.viewH, width: KScreenWidth - 40, height: YHHomeHeadView.viewH) homeHeaderView.frame = CGRect(x: 20, y: -YHHomeHeadView.viewH, width: KScreenWidth - 40, height: YHHomeHeadView.viewH)
homeCollectView.addSubview(homeHeaderView) homeCollectView.addSubview(homeHeaderView)
homeCollectView.contentInset = UIEdgeInsets(top: YHHomeHeadView.viewH, left: 0, bottom: 0, right: 0) homeCollectView.contentInset = UIEdgeInsets(top: YHHomeHeadView.viewH, left: 0, bottom: 20, right: 0)
homeCollectView.es.addInfiniteScrolling { homeCollectView.es.addInfiniteScrolling {
self.loadFakeData() // self.loadFakeData()
} }
loadFirstItem() loadFirstItem()
homeCollectView.reloadData() homeCollectView.reloadData()
...@@ -138,95 +116,72 @@ private extension YHHomePageViewController { ...@@ -138,95 +116,72 @@ private extension YHHomePageViewController {
} }
func loadFakeData() { // func getImageSize(_ url: String?) -> CGSize {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { // guard let urlStr = url, urlStr.count != 0 else {
self.items.append(AboutAdvantageItem(iconName: "about_match", title: "精准匹配", detail: "大数据精准匹配,专属方案获批率更高", url: "")) // return CGSize.zero
self.items.append(AboutAdvantageItem(iconName: "about_match", title: "精准匹配", detail: "大数据精准匹配,专属方案获批率更高", url: "")) // }
self.items.append(AboutAdvantageItem(iconName: "about_match", title: "精准匹配", detail: "大数据精准匹配,专属方案获批率更高", url: "")) // let tempUrl = URL(string: urlStr)
self.items.append(AboutAdvantageItem(iconName: "about_match", title: "精准匹配", detail: "大数据精准匹配,专属方案获批率更高", url: "")) // let imageSourceRef = CGImageSourceCreateWithURL(tempUrl! as CFURL, nil)
self.items.append(AboutAdvantageItem(iconName: "about_match", title: "精准匹配", detail: "大数据精准匹配,专属方案获批率更高", url: "")) // var width: CGFloat = 0
// var height: CGFloat = 0
self.homeCollectView.es.stopLoadingMore() // if let imageSRef = imageSourceRef {
self.homeCollectView.reloadData() // let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSRef, 0, nil)
} // if let imageP = imageProperties {
} // let imageDict = imageP as Dictionary
// width = imageDict[kCGImagePropertyPixelWidth] as! CGFloat
func getImageSize(_ url: String?) -> CGSize { // height = imageDict[kCGImagePropertyPixelHeight] as! CGFloat
guard let urlStr = url, urlStr.count != 0 else { // }
return CGSize.zero // }
} // return CGSize(width: width, height: height)
let tempUrl = URL(string: urlStr) // }
let imageSourceRef = CGImageSourceCreateWithURL(tempUrl! as CFURL, nil)
var width: CGFloat = 0
var height: CGFloat = 0
if let imageSRef = imageSourceRef {
let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSRef, 0, nil)
if let imageP = imageProperties {
let imageDict = imageP as Dictionary
width = imageDict[kCGImagePropertyPixelWidth] as! CGFloat
height = imageDict[kCGImagePropertyPixelHeight] as! CGFloat
}
}
return CGSize(width: width, height: height)
}
} }
extension YHHomePageViewController: UICollectionViewDelegate, UICollectionViewDataSource { extension YHHomePageViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.viewModel.lists?.count ?? 0 return self.viewModel.arrHomeNewsData.count
} }
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: YHHomeCollectionViewCell.cellReuseIdentifier, for: indexPath) as! YHHomeCollectionViewCell let cell = collectionView.dequeueReusableCell(withReuseIdentifier: YHHomeCollectionViewCell.cellReuseIdentifier, for: indexPath) as! YHHomeCollectionViewCell
cell.listModel = self.viewModel.lists?[indexPath.row] cell.listModel = self.viewModel.arrHomeNewsData[indexPath.row]
return cell return cell
} }
// func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
// print("index is \(indexPath.row)")
// }
} }
extension YHHomePageViewController : CollectionViewWaterfallLayoutDelegate { extension YHHomePageViewController : CollectionViewWaterfallLayoutDelegate {
func collectionView(_ collectionView: UICollectionView, layout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { func collectionView(_ collectionView: UICollectionView, layout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
guard let model = self.viewModel.lists?[indexPath.row] else { return CGSize(width: 0, height: 0) } // let model = self.viewModel.arrHomeNewsData[indexPath.row]
let text = model.title // let text = model.title
let width = (KScreenWidth - 47) / 2 // let width = (KScreenWidth - 47) / 2
let font = UIFont.PFSC_R(ofSize: 14) // 字体大小 // let font = UIFont.PFSC_R(ofSize: 14) // 字体大小
let maxWidth = width - 16 // 最大宽度限制 // let maxWidth = width - 16 // 最大宽度限制
// 创建NSAttributedString对象并设置属性 // // 创建NSAttributedString对象并设置属性
let attributes = [NSAttributedString.Key.font : font] as [NSAttributedString.Key : Any] // let attributes = [NSAttributedString.Key.font : font] as [NSAttributedString.Key : Any]
_ = NSMutableAttributedString(string: text, attributes: attributes) // _ = NSMutableAttributedString(string: text, attributes: attributes)
// 根据指定的最大宽度和字体大小计算文本的高度 // // 根据指定的最大宽度和字体大小计算文本的高度
let size = (text as NSString).boundingRect(with: CGSize(width: maxWidth, height: .greatestFiniteMagnitude), options: .usesLineFragmentOrigin, attributes: attributes, context: nil).size // let size = (text as NSString).boundingRect(with: CGSize(width: maxWidth, height: .greatestFiniteMagnitude), options: .usesLineFragmentOrigin, attributes: attributes, context: nil).size
var textHeight = size.height // var textHeight = size.height
if textHeight > 40 { // if textHeight > 40 {
textHeight = 40 // textHeight = 40
} // } else if textHeight < 20 {
let imageSize = getImageSize(model.img_url) // textHeight = 20
var imageHeight = 0.0 // }
if imageSize.width == 0 {
imageHeight = width
} else {
imageHeight = width * imageSize.height/imageSize.width
if imageHeight > 280 {
imageHeight = 280
}
}
let totalHeight = imageHeight + textHeight + 46
return CGSize(width: width, height: totalHeight)
// let margin = 20.0
// let gap = 7.0
// let itemWidth = ceil((KScreenWidth - 2*margin - gap)/2.0)
// let arrItemH : [CGFloat] = [100,150,200,250,300,350,400]
// //
// let HHH = arrItemH[Int.random(in: 0...6)] // let imageSize = CGSizeMake(CGFloat(model.img_width), CGFloat(model.img_height))
// return CGSize(width: itemWidth, height: HHH) // var imageHeight = 0.0
// if imageSize.width == 0 {
// imageHeight = width
// } else {
// imageHeight = width * imageSize.height/imageSize.width
// if imageHeight > 280 {
// imageHeight = 280
// }
// }
// let totalHeight = imageHeight + textHeight + 30
guard let model = self.viewModel.arrHomeNewsData[indexPath.row] else { return CGSize.zero }
return CGSize(width: model.img_width_use, height: model.img_height_use)
} }
} }
...@@ -16,6 +16,56 @@ class YHHomeListModel: YHBaseModel { ...@@ -16,6 +16,56 @@ class YHHomeListModel: YHBaseModel {
var view_count: Int = 0 var view_count: Int = 0
var img_url: String = "" var img_url: String = ""
var type: Int = 0 var type: Int = 0
var img_width: Int = 0
var img_height: Int = 0 //网络返回的图片宽高
var img_width: CGFloat = 0
var img_height: CGFloat = 0
//实际使用的图片宽高
var img_width_use : CGFloat = 0
var img_height_use : CGFloat = 0
//cell的宽高
var img_width_cell : CGFloat = 0
var img_height_cell : CGFloat = 0
func calHeightParam() {
let text = self.title
let width = (KScreenWidth - 47) / 2
let font = UIFont.PFSC_R(ofSize: 14) // 字体大小
let maxWidth = width - 16 // 最大宽度限制
// 创建NSAttributedString对象并设置属性
let attributes = [NSAttributedString.Key.font : font] as [NSAttributedString.Key : Any]
_ = NSMutableAttributedString(string: text, attributes: attributes)
// 根据指定的最大宽度和字体大小计算文本的高度
let size = (text as NSString).boundingRect(with: CGSize(width: maxWidth, height: .greatestFiniteMagnitude), options: .usesLineFragmentOrigin, attributes: attributes, context: nil).size
var textHeight = size.height
if textHeight > 40 {
textHeight = 40
} else if textHeight < 20 {
textHeight = 20
}
let imageSize = CGSizeMake(CGFloat(self.img_width), CGFloat(self.img_height))
var imageHeight = 0.0
if imageSize.width == 0 {
imageHeight = width
} else {
imageHeight = width * imageSize.height/imageSize.width
if imageHeight > 280 {
imageHeight = 280
}
}
let totalHeight = imageHeight + textHeight + 30
self.img_width_use = width
self.img_height_use = imageHeight
self.img_width_cell = width
self.img_height_cell = totalHeight
}
} }
...@@ -11,7 +11,7 @@ import UIKit ...@@ -11,7 +11,7 @@ import UIKit
class YHHomePageViewModel : YHBaseViewModel { class YHHomePageViewModel : YHBaseViewModel {
//数据源 //数据源
var configModel:YHHomeCofigMode? var configModel:YHHomeCofigMode?
var lists: [YHHomeListModel]? var arrHomeNewsData: [YHHomeListModel?] = []
var banners: [YHBannerModel]? var banners: [YHBannerModel]?
var classify: [YHHomeClassifyModel]? var classify: [YHHomeClassifyModel]?
} }
...@@ -38,7 +38,7 @@ extension YHHomePageViewModel { ...@@ -38,7 +38,7 @@ extension YHHomePageViewModel {
} }
} }
func getHomeList(callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) { func getHomeNewsList(callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
let params: [String : Any] = ["page": 1, let params: [String : Any] = ["page": 1,
"page_size": 10] "page_size": 10]
let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Common.consult let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Common.consult
...@@ -51,7 +51,7 @@ extension YHHomePageViewModel { ...@@ -51,7 +51,7 @@ extension YHHomePageViewModel {
callBackBlock(false,nil) callBackBlock(false,nil)
return return
} }
self.lists = result as? [YHHomeListModel] self.arrHomeNewsData = result
callBackBlock(true, nil) callBackBlock(true, nil)
} else { } else {
let error : YHErrorModel = YHErrorModel(errorCode:Int32(json.code), errorMsg: json.msg) let error : YHErrorModel = YHErrorModel(errorCode:Int32(json.code), errorMsg: json.msg)
......
...@@ -12,11 +12,11 @@ import Kingfisher ...@@ -12,11 +12,11 @@ import Kingfisher
class YHHomeCollectionViewCell: UICollectionViewCell { class YHHomeCollectionViewCell: UICollectionViewCell {
static let cellReuseIdentifier = "YHHomeCollectionViewCell" static let cellReuseIdentifier = "YHHomeCollectionViewCell"
var dataSource: AboutAdvantageItem? { // var dataSource: AboutAdvantageItem? {
didSet { // didSet {
updataAllSubViews() // updataAllSubViews()
} // }
} // }
var listModel: YHHomeListModel? { var listModel: YHHomeListModel? {
didSet { didSet {
...@@ -138,48 +138,38 @@ class YHHomeCollectionViewCell: UICollectionViewCell { ...@@ -138,48 +138,38 @@ class YHHomeCollectionViewCell: UICollectionViewCell {
} }
func updataAllSubViews() { // func updataAllSubViews() {
guard let dataSource = dataSource else { return } // guard let dataSource = listModel else { return }
let imageSize = getImageSize(dataSource.url) // let imageSize = getImageSize(dataSource.url)
var imageHeight = 0.0 // var imageHeight = 0.0
if imageSize.width == 0 { // if imageSize.width == 0 {
imageHeight = width // imageHeight = width
} else { // } else {
imageHeight = width * imageSize.height/imageSize.width // imageHeight = width * imageSize.height/imageSize.width
if imageHeight > 280 { // if imageHeight > 280 {
imageHeight = 280 // imageHeight = 280
} // }
} // }
titleImageView.snp.remakeConstraints { make in // titleImageView.snp.remakeConstraints { make in
make.left.right.top.equalToSuperview() // make.left.right.top.equalToSuperview()
make.height.equalTo(imageHeight) // make.height.equalTo(imageHeight)
} // }
if let url = URL(string: dataSource.url) { // if let url = URL(string: dataSource.url) {
titleImageView.kf.setImage(with: url) // titleImageView.kf.setImage(with: url)
} // }
descripeLable.text = dataSource.title // descripeLable.text = dataSource.title
} // }
func updataSubViews() { func updataSubViews() {
guard let listModel = listModel else { return } guard let listModel = listModel else { return }
let imageSize = getImageSize(listModel.img_url)
var imageHeight = 0.0
if imageSize.width == 0 {
imageHeight = width
} else {
imageHeight = width * imageSize.height/imageSize.width
if imageHeight > 280 {
imageHeight = 280
}
}
titleImageView.snp.remakeConstraints { make in titleImageView.snp.remakeConstraints { make in
make.left.right.top.equalToSuperview() make.left.right.top.equalToSuperview()
make.height.equalTo(imageHeight) make.height.equalTo(listModel.img_height_use)
} }
if let url = URL(string: listModel.img_url) { if let url = URL(string: listModel.img_url) {
titleImageView.kf.setImage(with: url) titleImageView.kf.setImage(with: url)
} }
descripeLable.text = listModel.title descripeLable.text = listModel.title.defaultStringIfEmpty(detaultValue: "***")//for test hjl
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment