mirror of
https://github.com/zhigang1992/GitHawk.git
synced 2026-05-28 15:23:54 +08:00
Reaction cell size is actually calculated (#1362)
* reaction size actually calculated * remove interitem spacing * simplify spacing
This commit is contained in:
@@ -29,6 +29,7 @@ UICollectionViewDelegateFlowLayout {
|
||||
let layout = UICollectionViewFlowLayout()
|
||||
layout.scrollDirection = .horizontal
|
||||
layout.minimumLineSpacing = 0
|
||||
layout.minimumInteritemSpacing = 0
|
||||
let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
|
||||
view.register(IssueReactionCell.self, forCellWithReuseIdentifier: IssueCommentReactionCell.reuse)
|
||||
return view
|
||||
@@ -233,8 +234,9 @@ UICollectionViewDelegateFlowLayout {
|
||||
layout collectionViewLayout: UICollectionViewLayout,
|
||||
sizeForItemAt indexPath: IndexPath
|
||||
) -> CGSize {
|
||||
let modifier = CGFloat(reactions[indexPath.item].count.description.count - 1)
|
||||
return CGSize(width: 50 + modifier * 5, height: collectionView.bounds.height)
|
||||
let reaction = reactions[indexPath.item]
|
||||
let width = IssueReactionCell.width(emoji: reaction.content.emoji, count: reaction.count)
|
||||
return CGSize(width: width, height: collectionView.bounds.height)
|
||||
}
|
||||
|
||||
// MARK: UICollectionViewDelegate
|
||||
|
||||
@@ -11,6 +11,24 @@ import SnapKit
|
||||
|
||||
final class IssueReactionCell: UICollectionViewCell {
|
||||
|
||||
private static var cache = [String: CGFloat]()
|
||||
private static let spacing: CGFloat = 4
|
||||
private static var emojiFont: UIFont { return UIFont.systemFont(ofSize: Styles.Sizes.Text.body + 2) }
|
||||
private static var countFont: UIFont { return Styles.Fonts.body }
|
||||
|
||||
static func width(emoji: String, count: Int) -> CGFloat {
|
||||
let key = "\(emoji)\(count)"
|
||||
if let cached = cache[key] {
|
||||
return cached
|
||||
}
|
||||
|
||||
let emojiWidth = (emoji as NSString).size(withAttributes: [.font: emojiFont]).width
|
||||
let countWidth = ("\(count)" as NSString).size(withAttributes: [.font: countFont]).width
|
||||
let width = emojiWidth + countWidth + 3 * spacing
|
||||
cache[key] = width
|
||||
return width
|
||||
}
|
||||
|
||||
private let emojiLabel = UILabel()
|
||||
private let countLabel = ShowMoreDetailsLabel()
|
||||
private var detailText = ""
|
||||
@@ -20,8 +38,6 @@ final class IssueReactionCell: UICollectionViewCell {
|
||||
accessibilityTraits |= UIAccessibilityTraitButton
|
||||
isAccessibilityElement = true
|
||||
|
||||
let offset: CGFloat = 6
|
||||
|
||||
emojiLabel.textAlignment = .center
|
||||
emojiLabel.backgroundColor = .clear
|
||||
// hint bigger emoji than labels
|
||||
@@ -29,7 +45,7 @@ final class IssueReactionCell: UICollectionViewCell {
|
||||
contentView.addSubview(emojiLabel)
|
||||
emojiLabel.snp.makeConstraints { make in
|
||||
make.centerY.equalTo(contentView)
|
||||
make.centerX.equalTo(contentView).offset(-offset)
|
||||
make.left.equalTo(contentView).offset(IssueReactionCell.spacing)
|
||||
}
|
||||
|
||||
countLabel.textAlignment = .center
|
||||
@@ -39,7 +55,7 @@ final class IssueReactionCell: UICollectionViewCell {
|
||||
contentView.addSubview(countLabel)
|
||||
countLabel.snp.makeConstraints { make in
|
||||
make.centerY.equalTo(emojiLabel)
|
||||
make.left.equalTo(emojiLabel.snp.right).offset(offset)
|
||||
make.left.equalTo(emojiLabel.snp.right).offset(IssueReactionCell.spacing)
|
||||
}
|
||||
|
||||
let longPress = UILongPressGestureRecognizer(
|
||||
|
||||
Reference in New Issue
Block a user