From 906b3b257215ec9a19f90195b812a5c8e1e23ff6 Mon Sep 17 00:00:00 2001 From: CodeEagle Date: Fri, 18 Dec 2020 16:42:49 +0800 Subject: [PATCH] feat: add more flex config --- HMSegmentedControl/HMSegmentedControl.h | 9 +++++++++ HMSegmentedControl/HMSegmentedControl.m | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/HMSegmentedControl/HMSegmentedControl.h b/HMSegmentedControl/HMSegmentedControl.h index ab8a34ec..84aede68 100755 --- a/HMSegmentedControl/HMSegmentedControl.h +++ b/HMSegmentedControl/HMSegmentedControl.h @@ -255,6 +255,15 @@ typedef NS_ENUM(NSInteger, HMSegmentedControlImagePosition) { */ @property (nonatomic) BOOL shouldAnimateUserSelection; +/* Describes how individual lines of text are aligned within the layer + * bounds. The possible options are `natural', `left', `right', + * `center' and `justified'. Defaults to `natural'. */ + +@property(nonatomic, copy) CATextLayerAlignmentMode textAlignmentMode; + +/* Using segmentEdgeInset left to layout text layer */ +@property(nonatomic, assign) BOOL usingSegmentEdgeInsetLeftToLayout; + - (instancetype)initWithSectionTitles:(NSArray *)sectiontitles; - (instancetype)initWithSectionImages:(NSArray *)sectionImages sectionSelectedImages:(NSArray *)sectionSelectedImages; - (instancetype)initWithSectionImages:(NSArray *)sectionImages sectionSelectedImages:(NSArray *)sectionSelectedImages titlesForSections:(NSArray *)sectiontitles; diff --git a/HMSegmentedControl/HMSegmentedControl.m b/HMSegmentedControl/HMSegmentedControl.m index 51e26e4e..6cb26953 100755 --- a/HMSegmentedControl/HMSegmentedControl.m +++ b/HMSegmentedControl/HMSegmentedControl.m @@ -177,6 +177,9 @@ - (void)commonInit { self.selectionIndicatorBoxLayer.borderWidth = 1.0f; self.selectionIndicatorBoxOpacity = 0.2; + self.usingSegmentEdgeInsetLeftToLayout = NO; + self.textAlignmentMode = kCAAlignmentCenter; + self.contentMode = UIViewContentModeRedraw; } @@ -242,6 +245,16 @@ - (NSMutableArray *)titleBackgroundLayers { return _titleBackgroundLayers; } +- (void)setUsingSegmentEdgeInsetLeftToLayout:(BOOL)usingSegmentEdgeInsetLeftToLayout { + _usingSegmentEdgeInsetLeftToLayout = usingSegmentEdgeInsetLeftToLayout; + [self setNeedsDisplay]; +} + +- (void)setTextAlignmentMode:(CATextLayerAlignmentMode)textAlignmentMode { + _textAlignmentMode = textAlignmentMode; + [self setNeedsDisplay]; +} + #pragma mark - Drawing - (CGSize)measureTitleAtIndex:(NSUInteger)index { @@ -348,6 +361,10 @@ - (void)drawRect:(CGRect)rect { CGFloat widthForIndex = [[self.segmentWidthsArray objectAtIndex:idx] floatValue]; rect = CGRectMake(xOffset, y, widthForIndex, stringHeight); + if (self.usingSegmentEdgeInsetLeftToLayout == YES) { + rect.origin.x += self.segmentEdgeInset.left; + rect.size.width -= self.segmentEdgeInset.left; + } fullRect = CGRectMake(xOffset, 0, widthForIndex, oldRect.size.height); rectDiv = CGRectMake(xOffset - (self.verticalDividerWidth / 2), self.selectionIndicatorHeight * 2, self.verticalDividerWidth, self.frame.size.height - (self.selectionIndicatorHeight * 4)); } @@ -358,6 +375,9 @@ - (void)drawRect:(CGRect)rect { CATextLayer *titleLayer = [CATextLayer layer]; titleLayer.frame = rect; titleLayer.alignmentMode = kCAAlignmentCenter; + if (self.textAlignmentMode != nil) { + titleLayer.alignmentMode = self.textAlignmentMode; + } if ([UIDevice currentDevice].systemVersion.floatValue < 10.0 ) { titleLayer.truncationMode = kCATruncationEnd; }