From 4e17c3a89faf6cbc27b74c8aab425fd47a449e68 Mon Sep 17 00:00:00 2001 From: hillyoung <> Date: Wed, 21 Sep 2022 17:23:02 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E6=A0=87?= =?UTF-8?q?=E9=A2=98=E8=83=8C=E6=99=AF=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HMSegmentedControl/HMSegmentedControl.h | 18 ++++++++ HMSegmentedControl/HMSegmentedControl.m | 50 +++++++++++----------- HMSegmentedControlExample/AppDelegate.m | 2 +- HMSegmentedControlExample/ViewController.m | 26 +++++++++-- 4 files changed, 68 insertions(+), 28 deletions(-) diff --git a/HMSegmentedControl/HMSegmentedControl.h b/HMSegmentedControl/HMSegmentedControl.h index ab8a34ec..aede2b80 100755 --- a/HMSegmentedControl/HMSegmentedControl.h +++ b/HMSegmentedControl/HMSegmentedControl.h @@ -250,6 +250,24 @@ typedef NS_ENUM(NSInteger, HMSegmentedControlImagePosition) { @property (nonatomic, readwrite) UIEdgeInsets enlargeEdgeInset; +/** + Inset left and right edges of between segments. + + Default is UIEdgeInsetsMake(0, 0, 0, 0) + */ +@property (nonatomic, readwrite) UIEdgeInsets segmentMarginInset; +/** + Color for the title background + */ +@property (nonatomic, strong) UIColor *titleBackgroundColor; +/** + Radius for the title background:0~1.0 + */ +@property (nonatomic) CGFloat titleBackgroundRadius; +/** + Height for the segment item + */ +@property (nonatomic) CGFloat segmentHeight; /** Default is YES. Set to NO to disable animation during user selection. */ diff --git a/HMSegmentedControl/HMSegmentedControl.m b/HMSegmentedControl/HMSegmentedControl.m index 51e26e4e..a45f0d54 100755 --- a/HMSegmentedControl/HMSegmentedControl.m +++ b/HMSegmentedControl/HMSegmentedControl.m @@ -117,17 +117,17 @@ - (instancetype)initWithSectionImages:(NSArray *)sectionImages sectio } - (instancetype)initWithSectionImages:(NSArray *)sectionImages sectionSelectedImages:(NSArray *)sectionSelectedImages titlesForSections:(NSArray *)sectiontitles { - self = [super initWithFrame:CGRectZero]; + self = [super initWithFrame:CGRectZero]; if (self) { [self commonInit]; - - if (sectionImages.count != sectiontitles.count) { - [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]; + + if (sectionImages.count != sectiontitles.count) { + [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]; } - + self.sectionImages = sectionImages; self.sectionSelectedImages = sectionSelectedImages; - self.sectionTitles = sectiontitles; + self.sectionTitles = sectiontitles; self.type = HMSegmentedControlTypeTextImages; } return self; @@ -207,11 +207,11 @@ - (void)setSectionImages:(NSArray *)sectionImages { } - (void)setSelectionIndicatorLocation:(HMSegmentedControlSelectionIndicatorLocation)selectionIndicatorLocation { - _selectionIndicatorLocation = selectionIndicatorLocation; - - if (selectionIndicatorLocation == HMSegmentedControlSelectionIndicatorLocationNone) { - self.selectionIndicatorHeight = 0.0f; - } + _selectionIndicatorLocation = selectionIndicatorLocation; + + if (selectionIndicatorLocation == HMSegmentedControlSelectionIndicatorLocationNone) { + self.selectionIndicatorHeight = 0.0f; + } } - (void)setSelectionIndicatorBoxOpacity:(CGFloat)selectionIndicatorBoxOpacity { @@ -321,7 +321,7 @@ - (void)drawRect:(CGRect)rect { CGFloat stringHeight = 0; CGSize size = [self measureTitleAtIndex:idx]; stringWidth = size.width; - stringHeight = size.height; + stringHeight = self.segmentHeight > 0 ? self.segmentHeight:size.height; CGRect rectDiv = CGRectZero; CGRect fullRect = CGRectZero; @@ -342,7 +342,7 @@ - (void)drawRect:(CGRect)rect { for (NSNumber *width in self.segmentWidthsArray) { if (idx == i) break; - xOffset = xOffset + [width floatValue]; + xOffset = xOffset + [width floatValue] + self.segmentMarginInset.left + self.segmentMarginInset.right; i++; } @@ -363,6 +363,8 @@ - (void)drawRect:(CGRect)rect { } titleLayer.string = [self attributedTitleAtIndex:idx]; titleLayer.contentsScale = [[UIScreen mainScreen] scale]; + titleLayer.backgroundColor = self.titleBackgroundColor.CGColor; + titleLayer.cornerRadius = CGRectGetHeight(rect) * self.titleBackgroundRadius; [self.scrollView.layer addSublayer:titleLayer]; @@ -465,11 +467,11 @@ - (void)drawRect:(CGRect)rect { }]; } else if (self.type == HMSegmentedControlTypeTextImages){ [self removeTitleBackgroundLayers]; - [self.sectionImages enumerateObjectsUsingBlock:^(id iconImage, NSUInteger idx, BOOL *stop) { + [self.sectionImages enumerateObjectsUsingBlock:^(id iconImage, NSUInteger idx, BOOL *stop) { UIImage *icon = iconImage; CGFloat imageWidth = icon.size.width; CGFloat imageHeight = icon.size.height; - + CGSize stringSize = [self measureTitleAtIndex:idx]; CGFloat stringHeight = stringSize.height; CGFloat stringWidth = stringSize.width; @@ -554,7 +556,7 @@ - (void)drawRect:(CGRect)rect { } CALayer *imageLayer = [CALayer layer]; imageLayer.frame = imageRect; - + if (self.selectedSegmentIndex == idx) { if (self.sectionSelectedImages) { UIImage *highlightIcon = [self.sectionSelectedImages objectAtIndex:idx]; @@ -567,7 +569,7 @@ - (void)drawRect:(CGRect)rect { } [self.scrollView.layer addSublayer:imageLayer]; - titleLayer.contentsScale = [[UIScreen mainScreen] scale]; + titleLayer.contentsScale = [[UIScreen mainScreen] scale]; [self.scrollView.layer addSublayer:titleLayer]; if ([self.accessibilityElements count]<=idx) { @@ -594,10 +596,10 @@ - (void)drawRect:(CGRect)rect { else element.accessibilityTraits = UIAccessibilityTraitButton; } - + [self addBackgroundAndBorderLayerWithRect:imageRect]; }]; - } + } // Add the selection indicators if (self.selectedSegmentIndex != HMSegmentedControlNoSegment) { @@ -714,11 +716,11 @@ - (CGRect)frameForSelectionIndicator { CGFloat imageWidth = sectionImage.size.width; sectionWidth = imageWidth; } else if (self.type == HMSegmentedControlTypeTextImages) { - CGFloat stringWidth = [self measureTitleAtIndex:self.selectedSegmentIndex].width; - UIImage *sectionImage = [self.sectionImages objectAtIndex:self.selectedSegmentIndex]; - CGFloat imageWidth = sectionImage.size.width; + CGFloat stringWidth = [self measureTitleAtIndex:self.selectedSegmentIndex].width; + UIImage *sectionImage = [self.sectionImages objectAtIndex:self.selectedSegmentIndex]; + CGFloat imageWidth = sectionImage.size.width; sectionWidth = MAX(stringWidth, imageWidth); - } + } if (self.selectionStyle == HMSegmentedControlSelectionStyleArrow) { CGFloat widthToEndOfSelectedSegment = (self.segmentWidth * self.selectedSegmentIndex) + self.segmentWidth; @@ -767,7 +769,7 @@ - (CGRect)frameForFillerSelectionIndicator { if (self.selectedSegmentIndex == i) { break; } - selectedSegmentOffset = selectedSegmentOffset + [width floatValue]; + selectedSegmentOffset = selectedSegmentOffset + [width floatValue] + self.segmentMarginInset.left + self.segmentMarginInset.right; i++; } diff --git a/HMSegmentedControlExample/AppDelegate.m b/HMSegmentedControlExample/AppDelegate.m index f6a43f20..023d6ac4 100644 --- a/HMSegmentedControlExample/AppDelegate.m +++ b/HMSegmentedControlExample/AppDelegate.m @@ -14,7 +14,7 @@ @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; - self.viewController = [[ExampleViewController alloc] init]; + self.viewController = [[ViewController alloc] init]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; return YES; diff --git a/HMSegmentedControlExample/ViewController.m b/HMSegmentedControlExample/ViewController.m index faab2f72..e6590ce4 100644 --- a/HMSegmentedControlExample/ViewController.m +++ b/HMSegmentedControlExample/ViewController.m @@ -29,10 +29,30 @@ - (void)viewDidLoad { // Minimum code required to use the segmented control with the default styling. HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"Trending", @"News", @"Library"]]; segmentedControl.frame = CGRectMake(0, 60, viewWidth, 40); - segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth; +// segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth; + segmentedControl.type = HMSegmentedControlTypeText; + segmentedControl.segmentEdgeInset = UIEdgeInsetsMake(0, 16, 0, 16); + segmentedControl.segmentWidthStyle = HMSegmentedControlSegmentWidthStyleDynamic; + segmentedControl.borderType = HMSegmentedControlBorderTypeNone; + segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationNone; + segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleBox;; + segmentedControl.selectionIndicatorBoxColor = [UIColor colorWithRed:0.882 green:0 blue:1 alpha:1]; [segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged]; + segmentedControl.titleBackgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:0.3]; + segmentedControl.titleBackgroundRadius = 0.5; + segmentedControl.segmentMarginInset = UIEdgeInsetsMake(0, 0, 0, 8); + segmentedControl.segmentHeight = 40; + [segmentedControl setTitleFormatter:^NSAttributedString * _Nonnull(HMSegmentedControl * _Nonnull segmentedControl, NSString * _Nonnull title, NSUInteger index, BOOL selected) { + return [[NSAttributedString alloc] initWithString:title attributes:@{ + NSFontAttributeName: [UIFont systemFontOfSize:14], + NSForegroundColorAttributeName: [UIColor purpleColor], + NSBaselineOffsetAttributeName:@(-10) + }]; + }]; [self.view addSubview:segmentedControl]; + CALayer *layer = [segmentedControl valueForKey:@"selectionIndicatorBoxLayer"]; + layer.cornerRadius = 20; // Segmented control with scrolling HMSegmentedControl *segmentedControl1 = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"One", @"Two", @"Three", @"Four", @"Five", @"Six", @"Seven", @"Eight"]]; @@ -154,11 +174,11 @@ - (void)setApperanceForLabel:(UILabel *)label { } - (void)segmentedControlChangedValue:(HMSegmentedControl *)segmentedControl { - NSLog(@"Selected index %tu (via UIControlEventValueChanged)", segmentedControl.selectedSegmentIndex); + NSLog(@"Selected index %tu (via UIControlEventValueChanged)", segmentedControl.selectedSegmentIndex); } - (void)uisegmentedControlChangedValue:(UISegmentedControl *)segmentedControl { - NSLog(@"Selected index %tu", segmentedControl.selectedSegmentIndex); + NSLog(@"Selected index %tu", segmentedControl.selectedSegmentIndex); } #pragma mark - UIScrollViewDelegate From b66e1ec27b5911352d207e809a2e4741c4334dfc Mon Sep 17 00:00:00 2001 From: hillyoung <1666487339@qq.com> Date: Sat, 17 Feb 2024 17:57:44 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E7=82=B9=E5=87=BB?= =?UTF-8?q?=E5=90=84=E4=B8=AAseg=E4=B9=8B=E9=97=B4=E7=9A=84=E7=A9=BA?= =?UTF-8?q?=E7=99=BD=E4=B9=9F=E4=BC=9A=E8=A7=A6=E5=8F=91=E9=80=89=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HMSegmentedControl/HMSegmentedControl.m | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/HMSegmentedControl/HMSegmentedControl.m b/HMSegmentedControl/HMSegmentedControl.m index a45f0d54..badc727e 100755 --- a/HMSegmentedControl/HMSegmentedControl.m +++ b/HMSegmentedControl/HMSegmentedControl.m @@ -901,12 +901,18 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { // To know which segment the user touched, we need to loop over the widths and substract it from the x position. CGFloat widthLeft = (touchLocation.x + self.scrollView.contentOffset.x); for (NSNumber *width in self.segmentWidthsArray) { - widthLeft = widthLeft - [width floatValue]; + CGFloat fullWidth = (self.segmentMarginInset.left + self.segmentMarginInset.right + [width floatValue]); + if(widthLeft < self.segmentMarginInset.left || + (widthLeft > self.segmentMarginInset.left + [width floatValue] && widthLeft < fullWidth)) { + segment = self.selectedSegmentIndex; + break; + } + + widthLeft = widthLeft - fullWidth ; // When we don't have any width left to substract, we have the segment index. if (widthLeft <= 0) break; - segment++; } } From 3d40849a5b69891f64ceb8995c98b0f31dfee000 Mon Sep 17 00:00:00 2001 From: hillyoung <1666487339@qq.com> Date: Tue, 26 Mar 2024 10:26:44 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E2=80=9C=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E5=86=85=E5=AE=B9=E5=AE=BD=E5=BA=A6=E5=8A=A0=E4=B8=8A?= =?UTF-8?q?Item=E7=9A=84=E5=B7=A6=E5=8F=B3=E8=BE=B9=E8=B7=9D=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HMSegmentedControl/HMSegmentedControl.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HMSegmentedControl/HMSegmentedControl.m b/HMSegmentedControl/HMSegmentedControl.m index badc727e..d359da48 100755 --- a/HMSegmentedControl/HMSegmentedControl.m +++ b/HMSegmentedControl/HMSegmentedControl.m @@ -858,7 +858,7 @@ - (void)updateSegmentsRects { } self.scrollView.scrollEnabled = self.isUserDraggable; - self.scrollView.contentSize = CGSizeMake([self totalSegmentedControlWidth], self.frame.size.height); + self.scrollView.contentSize = CGSizeMake([self totalSegmentedControlWidth] + (self.segmentMarginInset.left + self.segmentMarginInset.right)*[self sectionCount], self.frame.size.height); } - (NSUInteger)sectionCount { From 8c6530efd95385572410fc1b4d5b8769b2173dd7 Mon Sep 17 00:00:00 2001 From: hillyoung <1666487339@qq.com> Date: Mon, 21 Oct 2024 17:20:25 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E5=A4=84=E7=90=86segment=E5=88=86=E6=AE=B5?= =?UTF-8?q?=E6=8E=A7=E4=BB=B6=E6=95=B0=E6=8D=AE=E6=BA=90=E4=B8=BA=E7=A9=BA?= =?UTF-8?q?=E6=97=B6=E5=AF=BC=E8=87=B4=E9=97=AA=E9=80=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HMSegmentedControl/HMSegmentedControl.m | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/HMSegmentedControl/HMSegmentedControl.m b/HMSegmentedControl/HMSegmentedControl.m index d359da48..5e30bb43 100755 --- a/HMSegmentedControl/HMSegmentedControl.m +++ b/HMSegmentedControl/HMSegmentedControl.m @@ -969,12 +969,14 @@ - (void)scrollTo:(NSUInteger)index animated:(BOOL)animated { i++; } - rectForSelectedIndex = CGRectMake(offsetter, - 0, - [[self.segmentWidthsArray objectAtIndex:index] floatValue], - self.frame.size.height); - - selectedSegmentOffset = (CGRectGetWidth(self.frame) / 2) - ([[self.segmentWidthsArray objectAtIndex:index] floatValue] / 2); + if (self.segmentWidthsArray.count > index) { + rectForSelectedIndex = CGRectMake(offsetter, + 0, + [[self.segmentWidthsArray objectAtIndex:index] floatValue], + self.frame.size.height); + + selectedSegmentOffset = (CGRectGetWidth(self.frame) / 2) - ([[self.segmentWidthsArray objectAtIndex:index] floatValue] / 2); + } } From 7a373e41dd930ea3487d54617215d2469bb3815a Mon Sep 17 00:00:00 2001 From: hillyoung <1666487339@qq.com> Date: Tue, 22 Oct 2024 15:30:55 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E5=A4=84=E7=90=86HMSegmentedControl=20?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=BA=90=E4=B8=BA=E7=A9=BA=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E9=97=AA=E9=80=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HMSegmentedControl/HMSegmentedControl.m | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/HMSegmentedControl/HMSegmentedControl.m b/HMSegmentedControl/HMSegmentedControl.m index 5e30bb43..a6dabbbf 100755 --- a/HMSegmentedControl/HMSegmentedControl.m +++ b/HMSegmentedControl/HMSegmentedControl.m @@ -313,6 +313,10 @@ - (void)drawRect:(CGRect)rect { if (self.accessibilityElements==nil) self.accessibilityElements = [NSMutableArray arrayWithCapacity:0]; + if (self.segmentWidthsArray.count == 0 && self.sectionTitles.count == 0 && self.sectionImages.count == 0) { + return; + } + if (self.type == HMSegmentedControlTypeText) { [self removeTitleBackgroundLayers]; [self.sectionTitles enumerateObjectsUsingBlock:^(id titleString, NSUInteger idx, BOOL *stop) {