Skip to content

Commit 29bbd9a

Browse files
committed
Support different text attributes per segment.
1 parent c5c8c06 commit 29bbd9a

File tree

2 files changed

+44
-29
lines changed

2 files changed

+44
-29
lines changed

HMSegmentedControl/HMSegmentedControl.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ typedef NS_ENUM(NSInteger, HMSegmentedControlSelectionStyle) {
2323
typedef NS_ENUM(NSInteger, HMSegmentedControlSelectionIndicatorLocation) {
2424
HMSegmentedControlSelectionIndicatorLocationUp,
2525
HMSegmentedControlSelectionIndicatorLocationDown,
26-
HMSegmentedControlSelectionIndicatorLocationNone // No selection indicator
26+
HMSegmentedControlSelectionIndicatorLocationNone // No selection indicator
2727
};
2828

2929
typedef NS_ENUM(NSInteger, HMSegmentedControlSegmentWidthStyle) {
@@ -46,7 +46,7 @@ enum {
4646
typedef NS_ENUM(NSInteger, HMSegmentedControlType) {
4747
HMSegmentedControlTypeText,
4848
HMSegmentedControlTypeImages,
49-
HMSegmentedControlTypeTextImages
49+
HMSegmentedControlTypeTextImages
5050
};
5151

5252
typedef NS_ENUM(NSInteger, HMSegmentedControlImagePosition) {
@@ -82,6 +82,10 @@ typedef NS_ENUM(NSInteger, HMSegmentedControlImagePosition) {
8282
*/
8383
@property (nonatomic, strong) NSDictionary *titleTextAttributes UI_APPEARANCE_SELECTOR;
8484

85+
@property (nonatomic, strong) NSArray <NSDictionary *> *segmentTitleTextAttributes;
86+
87+
@property (nonatomic, strong) NSArray <NSDictionary *> *segmentSelectedTitleTextAttributes;
88+
8589
/*
8690
Text attributes to apply to selected item title text.
8791

HMSegmentedControl/HMSegmentedControl.m

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,18 @@ - (id)initWithSectionImages:(NSArray<UIImage *> *)sectionImages sectionSelectedI
9898
}
9999

100100
- (instancetype)initWithSectionImages:(NSArray<UIImage *> *)sectionImages sectionSelectedImages:(NSArray<UIImage *> *)sectionSelectedImages titlesForSections:(NSArray<NSString *> *)sectiontitles {
101-
self = [super initWithFrame:CGRectZero];
101+
self = [super initWithFrame:CGRectZero];
102102

103103
if (self) {
104104
[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];
108108
}
109-
109+
110110
self.sectionImages = sectionImages;
111111
self.sectionSelectedImages = sectionSelectedImages;
112-
self.sectionTitles = sectiontitles;
112+
self.sectionTitles = sectiontitles;
113113
self.type = HMSegmentedControlTypeTextImages;
114114
}
115115

@@ -189,11 +189,11 @@ - (void)setSectionImages:(NSArray<UIImage *> *)sectionImages {
189189
}
190190

191191
- (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+
}
197197
}
198198

199199
- (void)setSelectionIndicatorBoxOpacity:(CGFloat)selectionIndicatorBoxOpacity {
@@ -227,7 +227,7 @@ - (CGSize)measureTitleAtIndex:(NSUInteger)index {
227227
CGSize size = CGSizeZero;
228228
BOOL selected = (index == self.selectedSegmentIndex) ? YES : NO;
229229
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];
231231
size = [(NSString *)title sizeWithAttributes:titleAttrs];
232232
UIFont *font = titleAttrs[@"NSFont"];
233233
size = CGSizeMake(ceil(size.width), ceil(size.height-font.descender));
@@ -249,7 +249,7 @@ - (NSAttributedString *)attributedTitleAtIndex:(NSUInteger)index {
249249
if ([title isKindOfClass:[NSAttributedString class]]) {
250250
return (NSAttributedString *)title;
251251
} else if (!self.titleFormatter) {
252-
NSDictionary *titleAttrs = selected ? [self resultingSelectedTitleTextAttributes] : [self resultingTitleTextAttributes];
252+
NSDictionary *titleAttrs = selected ? [self resultingSelectedTitleTextAttributesAtIndex:index] : [self resultingTitleTextAttributesAtIndex:index];
253253

254254
// the color should be cast to CGColor in order to avoid invalid context on iOS7
255255
UIColor *titleColor = titleAttrs[NSForegroundColorAttributeName];
@@ -383,11 +383,11 @@ - (void)drawRect:(CGRect)rect {
383383
[self addBackgroundAndBorderLayerWithRect:rect];
384384
}];
385385
} 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) {
387387
UIImage *icon = iconImage;
388388
CGFloat imageWidth = icon.size.width;
389389
CGFloat imageHeight = icon.size.height;
390-
390+
391391
CGSize stringSize = [self measureTitleAtIndex:idx];
392392
CGFloat stringHeight = stringSize.height;
393393
CGFloat stringWidth = stringSize.width;
@@ -472,7 +472,7 @@ - (void)drawRect:(CGRect)rect {
472472
}
473473
CALayer *imageLayer = [CALayer layer];
474474
imageLayer.frame = imageRect;
475-
475+
476476
if (self.selectedSegmentIndex == idx) {
477477
if (self.sectionSelectedImages) {
478478
UIImage *highlightIcon = [self.sectionSelectedImages objectAtIndex:idx];
@@ -485,12 +485,12 @@ - (void)drawRect:(CGRect)rect {
485485
}
486486

487487
[self.scrollView.layer addSublayer:imageLayer];
488-
titleLayer.contentsScale = [[UIScreen mainScreen] scale];
488+
titleLayer.contentsScale = [[UIScreen mainScreen] scale];
489489
[self.scrollView.layer addSublayer:titleLayer];
490-
490+
491491
[self addBackgroundAndBorderLayerWithRect:imageRect];
492492
}];
493-
}
493+
}
494494

495495
// Add the selection indicators
496496
if (self.selectedSegmentIndex != HMSegmentedControlNoSegment) {
@@ -601,11 +601,11 @@ - (CGRect)frameForSelectionIndicator {
601601
CGFloat imageWidth = sectionImage.size.width;
602602
sectionWidth = imageWidth;
603603
} 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;
607607
sectionWidth = MAX(stringWidth, imageWidth);
608-
}
608+
}
609609

610610
if (self.selectionStyle == HMSegmentedControlSelectionStyleArrow) {
611611
CGFloat widthToEndOfSelectedSegment = (self.segmentWidth * self.selectedSegmentIndex) + self.segmentWidth;
@@ -944,28 +944,39 @@ - (void)notifyForSegmentChangeToIndex:(NSInteger)index {
944944

945945
#pragma mark - Styling Support
946946

947-
- (NSDictionary *)resultingTitleTextAttributes {
947+
- (NSDictionary *)resultingTitleTextAttributesAtIndex:(NSInteger)index {
948948
NSDictionary *defaults = @{
949949
NSFontAttributeName : [UIFont systemFontOfSize:19.0f],
950950
NSForegroundColorAttributeName : [UIColor blackColor],
951951
};
952952

953953
NSMutableDictionary *resultingAttrs = [NSMutableDictionary dictionaryWithDictionary:defaults];
954954

955-
if (self.titleTextAttributes) {
955+
if (self.titleTextAttributes)
956+
{
956957
[resultingAttrs addEntriesFromDictionary:self.titleTextAttributes];
957958
}
959+
960+
if (self.segmentTitleTextAttributes)
961+
{
962+
[resultingAttrs addEntriesFromDictionary:self.segmentTitleTextAttributes[index]];
963+
}
958964

959965
return [resultingAttrs copy];
960966
}
961967

962-
- (NSDictionary *)resultingSelectedTitleTextAttributes {
963-
NSMutableDictionary *resultingAttrs = [NSMutableDictionary dictionaryWithDictionary:[self resultingTitleTextAttributes]];
968+
- (NSDictionary *)resultingSelectedTitleTextAttributesAtIndex:(NSInteger)index {
969+
NSMutableDictionary *resultingAttrs = [NSMutableDictionary dictionaryWithDictionary:[self resultingTitleTextAttributesAtIndex:index]];
964970

965971
if (self.selectedTitleTextAttributes) {
966972
[resultingAttrs addEntriesFromDictionary:self.selectedTitleTextAttributes];
967973
}
968974

975+
if (self.segmentSelectedTitleTextAttributes)
976+
{
977+
[resultingAttrs addEntriesFromDictionary:self.segmentSelectedTitleTextAttributes[index]];
978+
}
979+
969980
return [resultingAttrs copy];
970981
}
971982

0 commit comments

Comments
 (0)