JXSegmentedTitleAttributeCell.swift 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //
  2. // JXSegmentedTitleAttributeCell.swift
  3. // JXSegmentedView
  4. //
  5. // Created by jiaxin on 2019/1/3.
  6. // Copyright © 2019 jiaxin. All rights reserved.
  7. //
  8. import UIKit
  9. open class JXSegmentedTitleAttributeCell: JXSegmentedBaseCell {
  10. open var titleLabel = UILabel()
  11. open override func commonInit() {
  12. super.commonInit()
  13. titleLabel.textAlignment = .center
  14. titleLabel.translatesAutoresizingMaskIntoConstraints = false
  15. contentView.addSubview(titleLabel)
  16. let centerX = NSLayoutConstraint(item: titleLabel, attribute: .centerX, relatedBy: .equal, toItem: contentView, attribute: .centerX, multiplier: 1, constant: 0)
  17. contentView.addConstraint(centerX)
  18. let centerY = NSLayoutConstraint(item: titleLabel, attribute: .centerY, relatedBy: .equal, toItem: contentView, attribute: .centerY, multiplier: 1, constant: 0)
  19. contentView.addConstraint(centerY)
  20. }
  21. open override func reloadData(itemModel: JXSegmentedBaseItemModel, selectedType: JXSegmentedViewItemSelectedType) {
  22. super.reloadData(itemModel: itemModel, selectedType: selectedType )
  23. guard let myItemModel = itemModel as? JXSegmentedTitleAttributeItemModel else {
  24. return
  25. }
  26. titleLabel.numberOfLines = myItemModel.titleNumberOfLines
  27. if myItemModel.isSelected && myItemModel.selectedAttributedTitle != nil {
  28. titleLabel.attributedText = myItemModel.selectedAttributedTitle
  29. }else {
  30. titleLabel.attributedText = myItemModel.attributedTitle
  31. }
  32. }
  33. }