Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JXSegmentedTitleDataSource,textWidth,itemWidth,itemWidthIncrement 生效,但是标题漏出,导致覆盖问题 #199

Open
nice2m opened this issue Feb 9, 2022 · 0 comments
Labels
help wanted Extra attention is needed

Comments

@nice2m
Copy link

nice2m commented Feb 9, 2022

lib_addr: https://github.com/pujiaxin33/JXSegmentedView
lib_version: 1.3.0,1.2.5
iOS 15.2 iPhone7

itemWidth,itemWidthIncrement 生效或定制化问题,UILabel标题漏出,导致覆盖

解决参考:

class MyCustomTabTitleDataSource: JXSegmentedTitleDataSource{
    open override func registerCellClass(in segmentedView: JXSegmentedView) {
        segmentedView.collectionView.register(MyCustomTabTitleCell.self, forCellWithReuseIdentifier: "cell")
    }
}

class MyCustomTabTitleCell: JXSegmentedTitleCell {
    
    open override func layoutSubviews() {
        super.layoutSubviews()

        guard let myItemModel = itemModel as? JXSegmentedTitleItemModel else {
            return
        }
        
        let labelSize = titleLabel.sizeThatFits(self.contentView.bounds.size)
        var labelBounds = CGRect(x: 0, y: 0, width: labelSize.width, height: labelSize.height)
        
        // 增加以下一句,可限制UILabel 漏出或撑出UICollectionViewCell 问题
        	  //  dataSource 中的 widthForTitleClosure 或 open func widthForTitle(_ title: String) -> CGFloat 会生效
        labelBounds.size.width = myItemModel.textWidth
        
        titleLabel.bounds = labelBounds
        titleLabel.center = contentView.center

        maskTitleLabel.bounds = labelBounds
        maskTitleLabel.center = contentView.center
    }
}

参考代码:

let segmentView = JXSegmentedView()
segmentView.delegate = self
let dataSource = MyCustomTabTitleDataSource()
dataSource.titles = ["很长很长的标题/很长很长的标题/很长很长的标题","不是很长的标题"]
dataSource.widthForTitleClosure = {[weak self] ( aString) -> CGFloat in
    // print("aString:\(aString)")
    return (UIScreen.main.bounds / CGFloat(2))
}
segmentView.dataSource = dataSource
@pujiaxin33 pujiaxin33 added the help wanted Extra attention is needed label Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants