Skip to content

Commit

Permalink
Add support for stretchSegmentsToScreenSize to `HMSegmentedControlT…
Browse files Browse the repository at this point in the history
…ypeText` (#310)
  • Loading branch information
ikesyo authored and HeshamMegid committed May 16, 2018
1 parent 40f5b3d commit e83e3de
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion HMSegmentedControl/HMSegmentedControl.m
Expand Up @@ -675,11 +675,23 @@ - (void)updateSegmentsRects {
}];
} else if (self.type == HMSegmentedControlTypeText && self.segmentWidthStyle == HMSegmentedControlSegmentWidthStyleDynamic) {
NSMutableArray *mutableSegmentWidths = [NSMutableArray array];

__block CGFloat totalWidth = 0.0;

[self.sectionTitles enumerateObjectsUsingBlock:^(id titleString, NSUInteger idx, BOOL *stop) {
CGFloat stringWidth = [self measureTitleAtIndex:idx].width + self.segmentEdgeInset.left + self.segmentEdgeInset.right;
totalWidth += stringWidth;
[mutableSegmentWidths addObject:[NSNumber numberWithFloat:stringWidth]];
}];

if (self.shouldStretchSegmentsToScreenSize && totalWidth < self.bounds.size.width) {
CGFloat whitespace = self.bounds.size.width - totalWidth;
CGFloat whitespaceForSegment = whitespace / [mutableSegmentWidths count];
[mutableSegmentWidths enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
CGFloat extendedWidth = whitespaceForSegment + [obj floatValue];
[mutableSegmentWidths replaceObjectAtIndex:idx withObject:[NSNumber numberWithFloat:extendedWidth]];
}];
}

self.segmentWidthsArray = [mutableSegmentWidths copy];
} else if (self.type == HMSegmentedControlTypeImages) {
for (UIImage *sectionImage in self.sectionImages) {
Expand Down

0 comments on commit e83e3de

Please sign in to comment.