Skip to content

Commit

Permalink
Merge pull request #12 from JNDisrupter/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
jayelzaghmoutt authored Oct 13, 2022
2 parents db3a4bb + 2b09058 commit 1070854
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ class JNSegmentedControlDetailViewController: UIViewController {
imageAttachment.bounds = CGRect(origin: CGPoint(x: 0.0, y: attachmentYPosition), size: CGSize(width: imageWidthHeight, height: imageWidthHeight))

fullString.append(NSAttributedString(attachment: imageAttachment))
fullString.append(NSAttributedString(string: " "))
fullString.append(NSAttributedString(string: " ", attributes: defaultAttributes))
fullString.append(NSAttributedString(string: self.textArray[index], attributes: defaultAttributes))
self.attributedStringItems.append(fullString)

Expand All @@ -349,7 +349,7 @@ class JNSegmentedControlDetailViewController: UIViewController {
selectedImageAttachment.bounds = CGRect(origin: CGPoint(x: 0.0, y: attachmentYPosition), size: CGSize(width: imageWidthHeight, height: imageWidthHeight))

selectedFullString.append(NSAttributedString(attachment: selectedImageAttachment))
selectedFullString.append(NSAttributedString(string: " "))
selectedFullString.append(NSAttributedString(string: " ", attributes: selectedAttributes))
selectedFullString.append(NSAttributedString(string: item, attributes: selectedAttributes))

self.selectedAttributedStringItems.append(selectedFullString)
Expand Down
2 changes: 1 addition & 1 deletion JNSegmentedControl.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "JNSegmentedControl"
s.version = "1.0.4"
s.version = "1.0.5"
s.summary = "JNSegmentedControl is animated segmented control for iOS applications."
s.description = "JNSegmentedControl is an easy to use, animated segmented control for iOS written in Swift. JNSegmentedControl supports 3 main styles of segments."
s.homepage = "https://github.com/JNDisrupter"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import UIKit
private struct ComponentValues {
static let badgeCountLabelMargins: CGFloat = 2.0
static let badgeCountLabelContainerViewTrailing: CGFloat = 5.0
static let badgeCountLabelContainerViewLeading: CGFloat = 2.0
static let badgeCountLabelContainerViewLeading: CGFloat = 5.0
}

/// JN Segmented Control Collection View Cell
Expand All @@ -39,6 +39,9 @@ class JNSegmentedControlCollectionViewCell: UICollectionViewCell {
/// Badge Count Label Container View Leading Constraint
@IBOutlet private weak var badgeCountLabelContainerViewLeadingConstraint: NSLayoutConstraint!

/// Badge Count Container View Width
@IBOutlet private weak var badgeCountContainerViewWidth: NSLayoutConstraint!

/// Badge Count Label Top Constraint
@IBOutlet private weak var badgeCountLabelTopConstraint: NSLayoutConstraint!

Expand All @@ -51,6 +54,7 @@ class JNSegmentedControlCollectionViewCell: UICollectionViewCell {
/// Badge Count Label Trailing Constraint
@IBOutlet private weak var badgeCountLabelTrailingConstraint: NSLayoutConstraint!


/**
Awake From Nib
*/
Expand Down Expand Up @@ -82,12 +86,12 @@ class JNSegmentedControlCollectionViewCell: UICollectionViewCell {
*/
func setup(with representable: JNSegmentedControlCollectionViewCellRepresentable) {

// set title number of lines
// Set title number of lines
self.titleLabel.numberOfLines = 0

// Set title label
self.titleLabel.attributedText = representable.attributedString
self.titleLabel.minimumScaleFactor = 0.3
self.titleLabel.minimumScaleFactor = 0.5
self.titleLabel.adjustsFontSizeToFitWidth = true

// Set background colors
Expand All @@ -108,19 +112,20 @@ class JNSegmentedControlCollectionViewCell: UICollectionViewCell {
self.badgeCountLabelTrailingConstraint.constant = ComponentValues.badgeCountLabelMargins
self.badgeCountLabelContainerViewTrailingConstraint.constant = ComponentValues.badgeCountLabelContainerViewTrailing
self.badgeCountLabelContainerViewLeadingConstraint.constant = ComponentValues.badgeCountLabelContainerViewLeading
self.badgeCountContainerViewWidth.constant = representable.badgeContainerViewWidth
}
else {

// Remove the text
self.badgeCountLabel.text = nil

// Set constraints to zero
self.badgeCountLabelContainerViewTrailingConstraint.constant = 0
self.badgeCountLabelTopConstraint.constant = 0
self.badgeCountLabelBottomConstraint.constant = 0
self.badgeCountLabelLeadingConstraint.constant = 0
self.badgeCountLabelTrailingConstraint.constant = 0
self.badgeCountLabelContainerViewLeadingConstraint.constant = 0
self.badgeCountContainerViewWidth.constant = 0
}

// Layout
Expand Down Expand Up @@ -153,20 +158,29 @@ class JNSegmentedControlCollectionViewCell: UICollectionViewCell {
/**
Calculate Cell Width
- Parameter attributedString: Attributed String.
- Parameter badgeAttributedString: Badge Attributed String
- Parameter maxBadgeContainerViewWidth: Max Badge Container View Width
- Returns: the calculated width, CGFloat value.
*/
class func calculateCellWidth(with attributedString: NSAttributedString, badgeAttributedString: NSAttributedString?, collectionViewHeight: CGFloat) -> CGFloat {
class func calculateCellWidth(with attributedString: NSAttributedString, badgeAttributedString: NSAttributedString?, collectionViewHeight: CGFloat, maxBadgeContainerViewWidth: CGFloat) -> CGFloat {
var width = attributedString.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude , height: collectionViewHeight), options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil).width

// Calculate badge
if let badgeAttributedString = badgeAttributedString {
let maxWidthAttributedString = NSAttributedString(string: "+99", attributes: badgeAttributedString.attributes(at: 0, effectiveRange: nil))

if badgeAttributedString != nil {

// Calculate the max for the badge
width += maxWidthAttributedString.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude , height: collectionViewHeight), options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil).width
width += ComponentValues.badgeCountLabelMargins*2
width += maxBadgeContainerViewWidth
width += ComponentValues.badgeCountLabelContainerViewTrailing
width += ComponentValues.badgeCountLabelContainerViewLeading
}
return ceil(width)
}

/**
Get Badge Count Labe lMargins
- Returns: CGFloat value.
*/
class func getBadgeCountLabelMargins() -> CGFloat {
return ComponentValues.badgeCountLabelMargins
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="none" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" rowHeight="62" id="zHH-l5-qd1" customClass="JNSegmentedControlCollectionViewCell" customModule="JNSegmentedControl" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="203" height="62"/>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="none" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" rowHeight="43" id="zHH-l5-qd1" customClass="JNSegmentedControlCollectionViewCell" customModule="JNSegmentedControl" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="131" height="43"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="zHH-l5-qd1" id="quV-eD-H6E">
<rect key="frame" x="0.0" y="0.0" width="203" height="62"/>
<rect key="frame" x="0.0" y="0.0" width="131" height="43"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="xfu-v6-agp">
<rect key="frame" x="0.0" y="0.0" width="203" height="62"/>
<rect key="frame" x="0.0" y="0.0" width="131" height="43"/>
<subviews>
<view clipsSubviews="YES" contentMode="scaleToFill" horizontalCompressionResistancePriority="1000" translatesAutoresizingMaskIntoConstraints="NO" id="PBr-Z2-jYD">
<rect key="frame" x="194" y="29" width="4" height="4"/>
<rect key="frame" x="96" y="6.5" width="30" height="30"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="255" text="" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Hla-gx-ahU">
<rect key="frame" x="2" y="2" width="0.0" height="0.0"/>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="255" horizontalCompressionResistancePriority="752" text="" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Hla-gx-ahU">
<rect key="frame" x="2" y="2" width="26" height="26"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="height" secondItem="Hla-gx-ahU" secondAttribute="width" multiplier="1:1" id="yQs-AS-sMI"/>
Expand All @@ -37,16 +37,18 @@
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="Hla-gx-ahU" firstAttribute="leading" secondItem="PBr-Z2-jYD" secondAttribute="leading" constant="2" id="6tM-9b-ZLv"/>
<constraint firstAttribute="width" secondItem="PBr-Z2-jYD" secondAttribute="height" multiplier="1:1" id="DzH-KU-w4E"/>
<constraint firstItem="Hla-gx-ahU" firstAttribute="top" secondItem="PBr-Z2-jYD" secondAttribute="top" constant="2" id="Imf-eX-GjM"/>
<constraint firstAttribute="bottom" secondItem="Hla-gx-ahU" secondAttribute="bottom" constant="2" id="Mwq-hj-6cT"/>
<constraint firstAttribute="width" constant="30" id="exK-f4-YNC"/>
<constraint firstAttribute="trailing" secondItem="Hla-gx-ahU" secondAttribute="trailing" constant="2" id="yt8-zg-NjT"/>
</constraints>
</view>
<view clipsSubviews="YES" contentMode="scaleToFill" horizontalCompressionResistancePriority="751" verticalCompressionResistancePriority="751" translatesAutoresizingMaskIntoConstraints="NO" id="A1g-Sv-Or2">
<rect key="frame" x="0.0" y="0.0" width="192" height="62"/>
<view clipsSubviews="YES" contentMode="scaleToFill" horizontalHuggingPriority="252" horizontalCompressionResistancePriority="751" verticalCompressionResistancePriority="751" translatesAutoresizingMaskIntoConstraints="NO" id="A1g-Sv-Or2">
<rect key="frame" x="0.0" y="0.0" width="91" height="43"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" verticalCompressionResistancePriority="751" text="" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="MI3-yt-cv9">
<rect key="frame" x="0.0" y="0.0" width="192" height="62"/>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="252" verticalCompressionResistancePriority="751" text="" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="MI3-yt-cv9">
<rect key="frame" x="0.0" y="0.0" width="91" height="43"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<nil key="textColor"/>
Expand All @@ -70,7 +72,7 @@
<constraint firstItem="A1g-Sv-Or2" firstAttribute="leading" secondItem="xfu-v6-agp" secondAttribute="leading" id="VTg-z1-0Re"/>
<constraint firstAttribute="trailing" secondItem="PBr-Z2-jYD" secondAttribute="trailing" constant="5" id="XGC-UH-uXE"/>
<constraint firstItem="PBr-Z2-jYD" firstAttribute="top" relation="greaterThanOrEqual" secondItem="xfu-v6-agp" secondAttribute="top" id="Yao-bM-ADV"/>
<constraint firstItem="PBr-Z2-jYD" firstAttribute="leading" secondItem="A1g-Sv-Or2" secondAttribute="trailing" constant="2" id="bpd-xy-Dtm"/>
<constraint firstItem="PBr-Z2-jYD" firstAttribute="leading" secondItem="A1g-Sv-Or2" secondAttribute="trailing" constant="5" id="bpd-xy-Dtm"/>
<constraint firstItem="PBr-Z2-jYD" firstAttribute="centerY" secondItem="A1g-Sv-Or2" secondAttribute="centerY" id="ogo-GB-H7B"/>
</constraints>
</view>
Expand All @@ -85,6 +87,7 @@
</tableViewCellContentView>
<connections>
<outlet property="badgeCountContainerView" destination="PBr-Z2-jYD" id="aGq-nY-Dt7"/>
<outlet property="badgeCountContainerViewWidth" destination="exK-f4-YNC" id="254-VF-dyc"/>
<outlet property="badgeCountLabel" destination="Hla-gx-ahU" id="FO8-3m-PUT"/>
<outlet property="badgeCountLabelBottomConstraint" destination="Mwq-hj-6cT" id="34P-Xg-64W"/>
<outlet property="badgeCountLabelContainerViewLeadingConstraint" destination="bpd-xy-Dtm" id="mKA-5J-m8z"/>
Expand All @@ -96,7 +99,7 @@
<outlet property="titleLabel" destination="MI3-yt-cv9" id="DjI-J3-T3A"/>
<outlet property="titleLabelContainerView" destination="A1g-Sv-Or2" id="idi-tU-Z18"/>
</connections>
<point key="canvasLocation" x="231.19999999999999" y="-94.452773613193415"/>
<point key="canvasLocation" x="173.59999999999999" y="-102.99850074962519"/>
</tableViewCell>
</objects>
<resources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class JNSegmentedControlCollectionViewCellRepresentable {
/// Badge attributed string
private(set) var badgeAttributedString: NSAttributedString?

/// Badge container view width
private(set) var badgeContainerViewWidth: CGFloat
/**
Initializer
*/
Expand All @@ -53,12 +55,13 @@ class JNSegmentedControlCollectionViewCellRepresentable {
self.containerViewBackgroundColor = .clear
self.badgeContainerViewBackgroundColor = .clear
self.badgeAttributedString = nil
self.badgeContainerViewWidth = 0.0
}

/**
Initialize
*/
convenience init(attributedString: NSAttributedString, options: JNSegmentedCollectionOptions, isLastItem: Bool = false, isSelected: Bool = false, cellSize: CGSize = CGSize.zero, containerViewCornerRadius: CGFloat, containerViewBackgroundColor: UIColor, badgeContainerViewBackgroundColor: UIColor, badgeFont: UIFont, badgeCount: Int?, badgeTextColor: UIColor) {
convenience init(attributedString: NSAttributedString, options: JNSegmentedCollectionOptions, isLastItem: Bool = false, isSelected: Bool = false, cellSize: CGSize = CGSize.zero, containerViewCornerRadius: CGFloat, containerViewBackgroundColor: UIColor, badgeContainerViewBackgroundColor: UIColor, badgeFont: UIFont, badgeCount: Int?, badgeTextColor: UIColor, badgeContainerViewWidth: CGFloat) {
self.init()

// build representable
Expand All @@ -82,6 +85,7 @@ class JNSegmentedControlCollectionViewCellRepresentable {
}

self.badgeAttributedString = NSAttributedString(string: badgeCountString, attributes: [NSAttributedString.Key.font: badgeFont, NSAttributedString.Key.foregroundColor: badgeTextColor])
self.badgeContainerViewWidth = badgeContainerViewWidth
}
}

Expand Down
Loading

0 comments on commit 1070854

Please sign in to comment.