@@ -98,18 +98,18 @@ - (id)initWithSectionImages:(NSArray<UIImage *> *)sectionImages sectionSelectedI
98
98
}
99
99
100
100
- (instancetype )initWithSectionImages : (NSArray <UIImage *> *)sectionImages sectionSelectedImages : (NSArray <UIImage *> *)sectionSelectedImages titlesForSections : (NSArray <NSString *> *)sectiontitles {
101
- self = [super initWithFrame: CGRectZero];
101
+ self = [super initWithFrame: CGRectZero];
102
102
103
103
if (self) {
104
104
[self commonInit ];
105
-
106
- if (sectionImages.count != sectiontitles.count ) {
107
- [NSException raise: NSRangeException format: @" ***%s : Images bounds (%ld ) Don't match Title bounds (%ld )" , sel_getName (_cmd ), (unsigned long )sectionImages.count, (unsigned long )sectiontitles.count];
105
+
106
+ if (sectionImages.count != sectiontitles.count ) {
107
+ [NSException raise: NSRangeException format: @" ***%s : Images bounds (%ld ) Don't match Title bounds (%ld )" , sel_getName (_cmd ), (unsigned long )sectionImages.count, (unsigned long )sectiontitles.count];
108
108
}
109
-
109
+
110
110
self.sectionImages = sectionImages;
111
111
self.sectionSelectedImages = sectionSelectedImages;
112
- self.sectionTitles = sectiontitles;
112
+ self.sectionTitles = sectiontitles;
113
113
self.type = HMSegmentedControlTypeTextImages;
114
114
}
115
115
@@ -189,11 +189,11 @@ - (void)setSectionImages:(NSArray<UIImage *> *)sectionImages {
189
189
}
190
190
191
191
- (void )setSelectionIndicatorLocation : (HMSegmentedControlSelectionIndicatorLocation)selectionIndicatorLocation {
192
- _selectionIndicatorLocation = selectionIndicatorLocation;
193
-
194
- if (selectionIndicatorLocation == HMSegmentedControlSelectionIndicatorLocationNone) {
195
- self.selectionIndicatorHeight = 0 .0f ;
196
- }
192
+ _selectionIndicatorLocation = selectionIndicatorLocation;
193
+
194
+ if (selectionIndicatorLocation == HMSegmentedControlSelectionIndicatorLocationNone) {
195
+ self.selectionIndicatorHeight = 0 .0f ;
196
+ }
197
197
}
198
198
199
199
- (void )setSelectionIndicatorBoxOpacity : (CGFloat)selectionIndicatorBoxOpacity {
@@ -227,7 +227,7 @@ - (CGSize)measureTitleAtIndex:(NSUInteger)index {
227
227
CGSize size = CGSizeZero;
228
228
BOOL selected = (index == self.selectedSegmentIndex ) ? YES : NO ;
229
229
if ([title isKindOfClass: [NSString class ]] && !self.titleFormatter ) {
230
- NSDictionary *titleAttrs = selected ? [self resultingSelectedTitleTextAttributes ] : [self resultingTitleTextAttributes ];
230
+ NSDictionary *titleAttrs = selected ? [self resultingSelectedTitleTextAttributesAtIndex: index ] : [self resultingTitleTextAttributesAtIndex: index ];
231
231
size = [(NSString *)title sizeWithAttributes: titleAttrs];
232
232
UIFont *font = titleAttrs[@" NSFont" ];
233
233
size = CGSizeMake (ceil (size.width ), ceil (size.height -font.descender ));
@@ -249,7 +249,7 @@ - (NSAttributedString *)attributedTitleAtIndex:(NSUInteger)index {
249
249
if ([title isKindOfClass: [NSAttributedString class ]]) {
250
250
return (NSAttributedString *)title;
251
251
} else if (!self.titleFormatter ) {
252
- NSDictionary *titleAttrs = selected ? [self resultingSelectedTitleTextAttributes ] : [self resultingTitleTextAttributes ];
252
+ NSDictionary *titleAttrs = selected ? [self resultingSelectedTitleTextAttributesAtIndex: index ] : [self resultingTitleTextAttributesAtIndex: index ];
253
253
254
254
// the color should be cast to CGColor in order to avoid invalid context on iOS7
255
255
UIColor *titleColor = titleAttrs[NSForegroundColorAttributeName ];
@@ -383,11 +383,11 @@ - (void)drawRect:(CGRect)rect {
383
383
[self addBackgroundAndBorderLayerWithRect: rect];
384
384
}];
385
385
} else if (self.type == HMSegmentedControlTypeTextImages){
386
- [self .sectionImages enumerateObjectsUsingBlock: ^(id iconImage, NSUInteger idx, BOOL *stop) {
386
+ [self .sectionImages enumerateObjectsUsingBlock: ^(id iconImage, NSUInteger idx, BOOL *stop) {
387
387
UIImage *icon = iconImage;
388
388
CGFloat imageWidth = icon.size .width ;
389
389
CGFloat imageHeight = icon.size .height ;
390
-
390
+
391
391
CGSize stringSize = [self measureTitleAtIndex: idx];
392
392
CGFloat stringHeight = stringSize.height ;
393
393
CGFloat stringWidth = stringSize.width ;
@@ -472,7 +472,7 @@ - (void)drawRect:(CGRect)rect {
472
472
}
473
473
CALayer *imageLayer = [CALayer layer ];
474
474
imageLayer.frame = imageRect;
475
-
475
+
476
476
if (self.selectedSegmentIndex == idx) {
477
477
if (self.sectionSelectedImages ) {
478
478
UIImage *highlightIcon = [self .sectionSelectedImages objectAtIndex: idx];
@@ -485,12 +485,12 @@ - (void)drawRect:(CGRect)rect {
485
485
}
486
486
487
487
[self .scrollView.layer addSublayer: imageLayer];
488
- titleLayer.contentsScale = [[UIScreen mainScreen ] scale ];
488
+ titleLayer.contentsScale = [[UIScreen mainScreen ] scale ];
489
489
[self .scrollView.layer addSublayer: titleLayer];
490
-
490
+
491
491
[self addBackgroundAndBorderLayerWithRect: imageRect];
492
492
}];
493
- }
493
+ }
494
494
495
495
// Add the selection indicators
496
496
if (self.selectedSegmentIndex != HMSegmentedControlNoSegment) {
@@ -601,11 +601,11 @@ - (CGRect)frameForSelectionIndicator {
601
601
CGFloat imageWidth = sectionImage.size .width ;
602
602
sectionWidth = imageWidth;
603
603
} else if (self.type == HMSegmentedControlTypeTextImages) {
604
- CGFloat stringWidth = [self measureTitleAtIndex: self .selectedSegmentIndex].width ;
605
- UIImage *sectionImage = [self .sectionImages objectAtIndex: self .selectedSegmentIndex];
606
- CGFloat imageWidth = sectionImage.size .width ;
604
+ CGFloat stringWidth = [self measureTitleAtIndex: self .selectedSegmentIndex].width ;
605
+ UIImage *sectionImage = [self .sectionImages objectAtIndex: self .selectedSegmentIndex];
606
+ CGFloat imageWidth = sectionImage.size .width ;
607
607
sectionWidth = MAX (stringWidth, imageWidth);
608
- }
608
+ }
609
609
610
610
if (self.selectionStyle == HMSegmentedControlSelectionStyleArrow) {
611
611
CGFloat widthToEndOfSelectedSegment = (self.segmentWidth * self.selectedSegmentIndex ) + self.segmentWidth ;
@@ -944,28 +944,39 @@ - (void)notifyForSegmentChangeToIndex:(NSInteger)index {
944
944
945
945
#pragma mark - Styling Support
946
946
947
- - (NSDictionary *)resultingTitleTextAttributes {
947
+ - (NSDictionary *)resultingTitleTextAttributesAtIndex : ( NSInteger ) index {
948
948
NSDictionary *defaults = @{
949
949
NSFontAttributeName : [UIFont systemFontOfSize: 19 .0f ],
950
950
NSForegroundColorAttributeName : [UIColor blackColor ],
951
951
};
952
952
953
953
NSMutableDictionary *resultingAttrs = [NSMutableDictionary dictionaryWithDictionary: defaults];
954
954
955
- if (self.titleTextAttributes ) {
955
+ if (self.titleTextAttributes )
956
+ {
956
957
[resultingAttrs addEntriesFromDictionary: self .titleTextAttributes];
957
958
}
959
+
960
+ if (self.segmentTitleTextAttributes )
961
+ {
962
+ [resultingAttrs addEntriesFromDictionary: self .segmentTitleTextAttributes[index]];
963
+ }
958
964
959
965
return [resultingAttrs copy ];
960
966
}
961
967
962
- - (NSDictionary *)resultingSelectedTitleTextAttributes {
963
- NSMutableDictionary *resultingAttrs = [NSMutableDictionary dictionaryWithDictionary: [self resultingTitleTextAttributes ]];
968
+ - (NSDictionary *)resultingSelectedTitleTextAttributesAtIndex : ( NSInteger ) index {
969
+ NSMutableDictionary *resultingAttrs = [NSMutableDictionary dictionaryWithDictionary: [self resultingTitleTextAttributesAtIndex: index ]];
964
970
965
971
if (self.selectedTitleTextAttributes ) {
966
972
[resultingAttrs addEntriesFromDictionary: self .selectedTitleTextAttributes];
967
973
}
968
974
975
+ if (self.segmentSelectedTitleTextAttributes )
976
+ {
977
+ [resultingAttrs addEntriesFromDictionary: self .segmentSelectedTitleTextAttributes[index]];
978
+ }
979
+
969
980
return [resultingAttrs copy ];
970
981
}
971
982
0 commit comments