From bf2cafe652d68e9333efff8df509a1508d12d702 Mon Sep 17 00:00:00 2001 From: Tim Oliver Date: Tue, 24 Sep 2019 01:39:33 +0900 Subject: [PATCH 1/5] Reset label sizing to only happen on label setting --- TOSegmentedControl/Private/TOSegmentedControlSegment.m | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/TOSegmentedControl/Private/TOSegmentedControlSegment.m b/TOSegmentedControl/Private/TOSegmentedControlSegment.m index 4996cad..76279c4 100644 --- a/TOSegmentedControl/Private/TOSegmentedControlSegment.m +++ b/TOSegmentedControl/Private/TOSegmentedControlSegment.m @@ -210,6 +210,8 @@ - (UILabel *)makeLabelForTitle:(NSString *)title label.text = title; label.textAlignment = NSTextAlignmentCenter; label.textColor = self.segmentedControl.itemColor; + label.font = self.segmentedControl.selectedTextFont; + [label sizeToFit]; // Size to the selected font label.font = self.segmentedControl.textFont; label.backgroundColor = [UIColor clearColor]; return label; @@ -264,6 +266,11 @@ - (void)refreshItemView // Update the label view label.textColor = self.segmentedControl.itemColor; + // Set the frame off the selected text as it is larger + label.font = self.segmentedControl.selectedTextFont; + [label sizeToFit]; + + // Set back to default font label.font = self.segmentedControl.textFont; // Update the image view From a4b9b57cd8f6cc223ca74a395c09a029aba26d0c Mon Sep 17 00:00:00 2001 From: Tim Oliver Date: Tue, 24 Sep 2019 01:40:04 +0900 Subject: [PATCH 2/5] Removed overly complex transformation logic --- TOSegmentedControl/TOSegmentedControl.m | 53 ++++++++----------------- 1 file changed, 16 insertions(+), 37 deletions(-) diff --git a/TOSegmentedControl/TOSegmentedControl.m b/TOSegmentedControl/TOSegmentedControl.m index c7df706..9a5abe7 100644 --- a/TOSegmentedControl/TOSegmentedControl.m +++ b/TOSegmentedControl/TOSegmentedControl.m @@ -516,21 +516,18 @@ - (void)layoutItemViews UIView *itemView = item.itemView; [self.trackView addSubview:itemView]; - // Size to fit - [itemView sizeToFit]; - - // Make sure they are all unselected - [self setItemAtIndex:i selected:NO]; - // Lay out the frame CGRect thumbFrame = [self frameForSegmentAtIndex:i]; itemView.center = (CGPoint){CGRectGetMidX(thumbFrame), CGRectGetMidY(thumbFrame)}; itemView.frame = CGRectIntegral(itemView.frame); + // Make sure they are all unselected + [self setItemAtIndex:i selected:NO]; + // If the item is disabled, make it faded if (!self.enabled || item.isDisabled) { - itemView.alpha = kTOSegmentedControlDisabledAlpha; + itemView.alpha = kTOSegmentedControlDisabledAlpha; } i++; @@ -686,7 +683,6 @@ - (void)setItemAtIndex:(NSInteger)index selected:(BOOL)selected // Tell the segment to select itself in order to show the reversible arrow TOSegmentedControlSegment *segment = self.segments[index]; - if (segment.isSelected == selected) { return; } // Update the segment state segment.isSelected = selected; @@ -699,37 +695,15 @@ - (void)setItemAtIndex:(NSInteger)index selected:(BOOL)selected UILabel *label = segment.label; if (label == nil) { return; } - [UIView performWithoutAnimation:^{ - // Capture its current position and scale - CGPoint center = label.center; - CGAffineTransform transform = label.transform; - - // Reset its transform so we don't mangle the frame - label.transform = CGAffineTransformIdentity; - - // Set the font - UIFont *font = selected ? self.selectedTextFont : self.textFont; - label.font = font; + // Set the font + UIFont *font = selected ? self.selectedTextFont : self.textFont; + label.font = font; - // Resize the frame in case the new font exceeded the bounds - [label sizeToFit]; - label.frame = CGRectIntegral(label.frame); + // Re-apply the arrow image view to the translated frame + segment.arrowView.frame = [self frameForImageArrowViewWithItemFrame:label.frame]; - // Re-apply the arrow image view to the translated frame - if (selected) { - CGAffineTransform transform = segment.arrowView.transform; - segment.arrowView.transform = CGAffineTransformIdentity; - segment.arrowView.frame = [self frameForImageArrowViewWithItemFrame:label.frame]; - segment.arrowView.transform = transform; - } - - // Ensure the arrow view is set to the right orientation - [segment setArrowImageReversed:segment.isReversed]; - - // Re-apply the transform and the positioning - label.transform = transform; - label.center = center; - }]; + // Ensure the arrow view is set to the right orientation + [segment setArrowImageReversed:segment.isReversed]; } - (void)setItemAtIndex:(NSInteger)index faded:(BOOL)faded @@ -1262,6 +1236,11 @@ - (void)setSelectedTextFont:(UIFont *)selectedTextFont if (_selectedTextFont == nil) { _selectedTextFont = [UIFont systemFontOfSize:13.0f weight:UIFontWeightSemibold]; } + + // Set each item to adopt the new font + for (TOSegmentedControlSegment *item in self.segments) { + [item refreshItemView]; + } } // ----------------------------------------------- From ec063f72dd221f9db25103529ffe319e5ee5330b Mon Sep 17 00:00:00 2001 From: Tim Oliver Date: Tue, 24 Sep 2019 02:05:46 +0900 Subject: [PATCH 3/5] Very slightly tweaked corner radius --- TOSegmentedControl/TOSegmentedControl.m | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/TOSegmentedControl/TOSegmentedControl.m b/TOSegmentedControl/TOSegmentedControl.m index 9a5abe7..1c6a5c0 100644 --- a/TOSegmentedControl/TOSegmentedControl.m +++ b/TOSegmentedControl/TOSegmentedControl.m @@ -684,9 +684,6 @@ - (void)setItemAtIndex:(NSInteger)index selected:(BOOL)selected // Tell the segment to select itself in order to show the reversible arrow TOSegmentedControlSegment *segment = self.segments[index]; - // Update the segment state - segment.isSelected = selected; - // Update the alpha of the reversible arrow segment.arrowView.alpha = selected ? kTOSegmentedControlDirectionArrowAlpha : 0.0f; @@ -1077,7 +1074,7 @@ - (void)setItems:(NSArray *)items - (void)setCornerRadius:(CGFloat)cornerRadius { self.trackView.layer.cornerRadius = cornerRadius; - self.thumbView.layer.cornerRadius = (cornerRadius - _thumbInset) + 0.5f; + self.thumbView.layer.cornerRadius = (self.cornerRadius - _thumbInset) + 1.0f; } - (CGFloat)cornerRadius { return self.trackView.layer.cornerRadius; } @@ -1249,7 +1246,7 @@ - (void)setSelectedTextFont:(UIFont *)selectedTextFont - (void)setThumbInset:(CGFloat)thumbInset { _thumbInset = thumbInset; - self.thumbView.layer.cornerRadius = (self.cornerRadius - _thumbInset) + 0.5f; + self.thumbView.layer.cornerRadius = (self.cornerRadius - _thumbInset) + 1.0f; } // ----------------------------------------------- From 767987c952542d86d302a77cd6eb9408f322e5d8 Mon Sep 17 00:00:00 2001 From: Tim Oliver Date: Tue, 24 Sep 2019 02:07:04 +0900 Subject: [PATCH 4/5] Updated CHANGELOG --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f733ad..0845149 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,14 @@ x.y.z Release Notes (yyyy-MM-dd) ============================================================= +1.0.1 Release Notes (2019-09-23) +============================================================= + +### Fixed + +* Simplified the segment selection logic to fix a layout bug with the reversible arrow icon. +* Slightly tweaked the corner radius to match `UISegmentedControl` more closely. + 1.0.0 Release Notes (2019-09-23) ============================================================= From ca129f373fb7c42e85c57a32b860cf85c156409a Mon Sep 17 00:00:00 2001 From: Tim Oliver Date: Tue, 24 Sep 2019 02:12:22 +0900 Subject: [PATCH 5/5] Updated date in changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0845149..dd1f2ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ x.y.z Release Notes (yyyy-MM-dd) ============================================================= -1.0.1 Release Notes (2019-09-23) +1.0.1 Release Notes (2019-09-24) ============================================================= ### Fixed