测试已测试 | ✗ |
Lang语言 | SwiftSwift |
许可证 | MIT |
发布最后发布 | 2017年7月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Roman Sorochak 维护。
具有不同图片和文本尺寸的自定义UICollectionView布局。
PinterestVC | 自定义 Cell |
---|---|
![]() | ![]() |
import UIKit
import PinterestLayout
class MyPinterestVC: PinterestVC {
override public func viewDidLoad() {
super.viewDidLoad()
let text = "Some text. Some text. Some text. Some text."
items = [
PinterestItem(image: UIImage(named: "new_york"), text: text),
PinterestItem(image: UIImage(named: "bigben_river"), text: text),
PinterestItem(image: UIImage(named: "dubai"), text: text),
PinterestItem(image: UIImage(named: "4"), text: text),
PinterestItem(image: UIImage(named: "tiger"), text: text)
]
}
}
0 - 导入 PinterestLayout
import PinterestLayout
1 - 将 PinterestLayout 设置到您的 collection view。
let layout = PinterestLayout()
collectionView.collectionViewLayout = layout
2 - 设置布局
layout.delegate = self
layout.cellPadding = 5
layout.numberOfColumns = 2
3 - 实现 PinterestLayoutDelegate 的方法
/**
Height for image view in cell.
@param collectionView - collectionView
@param indexPath - index path for cell
Returns height of image view.
*/
func collectionView(collectionView: UICollectionView,
heightForImageAtIndexPath indexPath: IndexPath,
withWidth: CGFloat) -> CGFloat
/**
Height for annotation view (label) in cell.
@param collectionView - collectionView
@param indexPath - index path for cell
Returns height of annotation view.
*/
func collectionView(collectionView: UICollectionView,
heightForAnnotationAtIndexPath indexPath: IndexPath,
withWidth: CGFloat) -> CGFloat
public extension UIImage {
/**
Calculates the best height of the image for available width.
*/
public func height(forWidth width: CGFloat) -> CGFloat
//...
public extension String {
/**
Calculates the best height of the text for available width and font used.
*/
public func heightForWidth(width: CGFloat, font: UIFont) -> CGFloat
extension CustomCollectionVC: PinterestLayoutDelegate {
func collectionView(collectionView: UICollectionView,
heightForImageAtIndexPath indexPath: IndexPath,
withWidth: CGFloat) -> CGFloat {
let image = images[indexPath.item]
return image.height(forWidth: withWidth)
}
func collectionView(collectionView: UICollectionView,
heightForAnnotationAtIndexPath indexPath: IndexPath,
withWidth: CGFloat) -> CGFloat {
let textFont = UIFont(name: "Arial-ItalicMT", size: 11)!
return "Some text".heightForWidth(width: withWidth, font: textFont)
}
}
4 - 创建自定义 cell 并应用 PinterestLayoutAttributes
class CollectionViewCell: UICollectionViewCell {
//...
override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
super.apply(layoutAttributes)
if let attributes = layoutAttributes as? PinterestLayoutAttributes {
//change image view height by changing its constraint
imageViewHeightLayoutConstraint.constant = attributes.imageHeight
}
}
}
{
"src": "/upload/resize_cache/iblock/8e7/204_265_2/8e7f1f04d5e835f596ef33da74946847.jpg",
"width": 204,
"height": 265
}
collectionView.collectionViewLayout.invalidateLayout()
collectionView.reloadData()
通过电子邮件联系我们的团队 - [email protected]
可重用性遵循 MIT 许可证发布。有关详细信息,请参阅 LICENSE。