Skip to content

Commit cacac40

Browse files
authored
Merge pull request #6 from TimOliver/refine-label
Refine Label Layout Code
2 parents 077fc9b + ca129f3 commit cacac40

3 files changed

Lines changed: 33 additions & 42 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
x.y.z Release Notes (yyyy-MM-dd)
22
=============================================================
33

4+
1.0.1 Release Notes (2019-09-24)
5+
=============================================================
6+
7+
### Fixed
8+
9+
* Simplified the segment selection logic to fix a layout bug with the reversible arrow icon.
10+
* Slightly tweaked the corner radius to match `UISegmentedControl` more closely.
11+
412
1.0.0 Release Notes (2019-09-23)
513
=============================================================
614

TOSegmentedControl/Private/TOSegmentedControlSegment.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ - (UILabel *)makeLabelForTitle:(NSString *)title
210210
label.text = title;
211211
label.textAlignment = NSTextAlignmentCenter;
212212
label.textColor = self.segmentedControl.itemColor;
213+
label.font = self.segmentedControl.selectedTextFont;
214+
[label sizeToFit]; // Size to the selected font
213215
label.font = self.segmentedControl.textFont;
214216
label.backgroundColor = [UIColor clearColor];
215217
return label;
@@ -264,6 +266,11 @@ - (void)refreshItemView
264266

265267
// Update the label view
266268
label.textColor = self.segmentedControl.itemColor;
269+
// Set the frame off the selected text as it is larger
270+
label.font = self.segmentedControl.selectedTextFont;
271+
[label sizeToFit];
272+
273+
// Set back to default font
267274
label.font = self.segmentedControl.textFont;
268275

269276
// Update the image view

TOSegmentedControl/TOSegmentedControl.m

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -516,21 +516,18 @@ - (void)layoutItemViews
516516
UIView *itemView = item.itemView;
517517
[self.trackView addSubview:itemView];
518518

519-
// Size to fit
520-
[itemView sizeToFit];
521-
522-
// Make sure they are all unselected
523-
[self setItemAtIndex:i selected:NO];
524-
525519
// Lay out the frame
526520
CGRect thumbFrame = [self frameForSegmentAtIndex:i];
527521
itemView.center = (CGPoint){CGRectGetMidX(thumbFrame),
528522
CGRectGetMidY(thumbFrame)};
529523
itemView.frame = CGRectIntegral(itemView.frame);
530524

525+
// Make sure they are all unselected
526+
[self setItemAtIndex:i selected:NO];
527+
531528
// If the item is disabled, make it faded
532529
if (!self.enabled || item.isDisabled) {
533-
itemView.alpha = kTOSegmentedControlDisabledAlpha;
530+
itemView.alpha = kTOSegmentedControlDisabledAlpha;
534531
}
535532

536533
i++;
@@ -686,10 +683,6 @@ - (void)setItemAtIndex:(NSInteger)index selected:(BOOL)selected
686683

687684
// Tell the segment to select itself in order to show the reversible arrow
688685
TOSegmentedControlSegment *segment = self.segments[index];
689-
if (segment.isSelected == selected) { return; }
690-
691-
// Update the segment state
692-
segment.isSelected = selected;
693686

694687
// Update the alpha of the reversible arrow
695688
segment.arrowView.alpha = selected ? kTOSegmentedControlDirectionArrowAlpha : 0.0f;
@@ -699,37 +692,15 @@ - (void)setItemAtIndex:(NSInteger)index selected:(BOOL)selected
699692
UILabel *label = segment.label;
700693
if (label == nil) { return; }
701694

702-
[UIView performWithoutAnimation:^{
703-
// Capture its current position and scale
704-
CGPoint center = label.center;
705-
CGAffineTransform transform = label.transform;
706-
707-
// Reset its transform so we don't mangle the frame
708-
label.transform = CGAffineTransformIdentity;
709-
710-
// Set the font
711-
UIFont *font = selected ? self.selectedTextFont : self.textFont;
712-
label.font = font;
695+
// Set the font
696+
UIFont *font = selected ? self.selectedTextFont : self.textFont;
697+
label.font = font;
713698

714-
// Resize the frame in case the new font exceeded the bounds
715-
[label sizeToFit];
716-
label.frame = CGRectIntegral(label.frame);
699+
// Re-apply the arrow image view to the translated frame
700+
segment.arrowView.frame = [self frameForImageArrowViewWithItemFrame:label.frame];
717701

718-
// Re-apply the arrow image view to the translated frame
719-
if (selected) {
720-
CGAffineTransform transform = segment.arrowView.transform;
721-
segment.arrowView.transform = CGAffineTransformIdentity;
722-
segment.arrowView.frame = [self frameForImageArrowViewWithItemFrame:label.frame];
723-
segment.arrowView.transform = transform;
724-
}
725-
726-
// Ensure the arrow view is set to the right orientation
727-
[segment setArrowImageReversed:segment.isReversed];
728-
729-
// Re-apply the transform and the positioning
730-
label.transform = transform;
731-
label.center = center;
732-
}];
702+
// Ensure the arrow view is set to the right orientation
703+
[segment setArrowImageReversed:segment.isReversed];
733704
}
734705

735706
- (void)setItemAtIndex:(NSInteger)index faded:(BOOL)faded
@@ -1103,7 +1074,7 @@ - (void)setItems:(NSArray *)items
11031074
- (void)setCornerRadius:(CGFloat)cornerRadius
11041075
{
11051076
self.trackView.layer.cornerRadius = cornerRadius;
1106-
self.thumbView.layer.cornerRadius = (cornerRadius - _thumbInset) + 0.5f;
1077+
self.thumbView.layer.cornerRadius = (self.cornerRadius - _thumbInset) + 1.0f;
11071078
}
11081079

11091080
- (CGFloat)cornerRadius { return self.trackView.layer.cornerRadius; }
@@ -1262,6 +1233,11 @@ - (void)setSelectedTextFont:(UIFont *)selectedTextFont
12621233
if (_selectedTextFont == nil) {
12631234
_selectedTextFont = [UIFont systemFontOfSize:13.0f weight:UIFontWeightSemibold];
12641235
}
1236+
1237+
// Set each item to adopt the new font
1238+
for (TOSegmentedControlSegment *item in self.segments) {
1239+
[item refreshItemView];
1240+
}
12651241
}
12661242

12671243
// -----------------------------------------------
@@ -1270,7 +1246,7 @@ - (void)setSelectedTextFont:(UIFont *)selectedTextFont
12701246
- (void)setThumbInset:(CGFloat)thumbInset
12711247
{
12721248
_thumbInset = thumbInset;
1273-
self.thumbView.layer.cornerRadius = (self.cornerRadius - _thumbInset) + 0.5f;
1249+
self.thumbView.layer.cornerRadius = (self.cornerRadius - _thumbInset) + 1.0f;
12741250
}
12751251

12761252
// -----------------------------------------------

0 commit comments

Comments
 (0)