Skip to content

Commit

Permalink
Merge pull request #6 from TimOliver/refine-label
Browse files Browse the repository at this point in the history
Refine Label Layout Code
  • Loading branch information
TimOliver committed Sep 23, 2019
2 parents 077fc9b + ca129f3 commit cacac40
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 42 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,14 @@
x.y.z Release Notes (yyyy-MM-dd)
=============================================================

1.0.1 Release Notes (2019-09-24)
=============================================================

### 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)
=============================================================

Expand Down
7 changes: 7 additions & 0 deletions TOSegmentedControl/Private/TOSegmentedControlSegment.m
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
60 changes: 18 additions & 42 deletions TOSegmentedControl/TOSegmentedControl.m
Expand Up @@ -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++;
Expand Down Expand Up @@ -686,10 +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;

// Update the alpha of the reversible arrow
segment.arrowView.alpha = selected ? kTOSegmentedControlDirectionArrowAlpha : 0.0f;
Expand All @@ -699,37 +692,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
Expand Down Expand Up @@ -1103,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; }
Expand Down Expand Up @@ -1262,6 +1233,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];
}
}

// -----------------------------------------------
Expand All @@ -1270,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;
}

// -----------------------------------------------
Expand Down

0 comments on commit cacac40

Please sign in to comment.