Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow text align using segment inset left #357

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions HMSegmentedControl/HMSegmentedControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<NSString *> *)sectiontitles;
- (instancetype)initWithSectionImages:(NSArray<UIImage *> *)sectionImages sectionSelectedImages:(NSArray<UIImage *> *)sectionSelectedImages;
- (instancetype)initWithSectionImages:(NSArray<UIImage *> *)sectionImages sectionSelectedImages:(NSArray<UIImage *> *)sectionSelectedImages titlesForSections:(NSArray<NSString *> *)sectiontitles;
Expand Down
20 changes: 20 additions & 0 deletions HMSegmentedControl/HMSegmentedControl.m
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ - (void)commonInit {
self.selectionIndicatorBoxLayer.borderWidth = 1.0f;
self.selectionIndicatorBoxOpacity = 0.2;

self.usingSegmentEdgeInsetLeftToLayout = NO;
self.textAlignmentMode = kCAAlignmentCenter;

self.contentMode = UIViewContentModeRedraw;
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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));
}
Expand All @@ -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;
}
Expand Down