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

Add more configs #365

Open
wants to merge 2 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions HMSegmentedControl/HMSegmentedControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ typedef NS_ENUM(NSInteger, HMSegmentedControlImagePosition) {
*/
@property (nonatomic, strong) NSDictionary *titleTextAttributes UI_APPEARANCE_SELECTOR;

/**
Alignment mode for the title layer.
Defaults to Center.
*/
@property (nonatomic, strong) CATextLayerAlignmentMode titleLayerAlignmentMode;
/*
Text attributes to apply to selected item title text.

Expand Down Expand Up @@ -226,6 +231,12 @@ typedef NS_ENUM(NSInteger, HMSegmentedControlImagePosition) {
*/
@property (nonatomic, readwrite) CGFloat selectionIndicatorHeight;

/**
Insets for scrollView
Default is .zero
*/
@property (nonatomic, readwrite) UIEdgeInsets scrollViewInsets;

/**
Edge insets for the selection indicator.
NOTE: This does not affect the bounding box of HMSegmentedControlSelectionStyleBox
Expand Down Expand Up @@ -255,6 +266,12 @@ typedef NS_ENUM(NSInteger, HMSegmentedControlImagePosition) {
*/
@property (nonatomic) BOOL shouldAnimateUserSelection;

/**
Default is YES. Set to NO to disable scrolling to the selected segment on user selection.
Use shouldAnimateUserSelection to disable the animation on indicator.
*/
@property (nonatomic) BOOL shouldScrollToUserSelectionAnimated;

- (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
19 changes: 13 additions & 6 deletions HMSegmentedControl/HMSegmentedControl.m
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,13 @@ - (void)awakeFromNib {
- (void)commonInit {
self.scrollView = [[HMScrollView alloc] init];
self.scrollView.delegate = self;
#ifdef TARGET_OS_TV
#else
self.scrollView.scrollsToTop = NO;
#endif
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
[self addSubview:self.scrollView];

_backgroundColor = [UIColor whiteColor];
Expand All @@ -154,6 +158,7 @@ - (void)commonInit {

self.selectedSegmentIndex = 0;
self.segmentEdgeInset = UIEdgeInsetsMake(0, 5, 0, 5);
self.scrollViewInsets = UIEdgeInsetsZero;
self.selectionIndicatorHeight = 5.0f;
self.selectionIndicatorEdgeInsets = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f);
self.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe;
Expand All @@ -167,8 +172,10 @@ - (void)commonInit {
_verticalDividerColor = [UIColor blackColor];
self.borderColor = [UIColor blackColor];
self.borderWidth = 1.0f;
self.titleLayerAlignmentMode = kCAAlignmentCenter;

self.shouldAnimateUserSelection = YES;
self.shouldScrollToUserSelectionAnimated = YES;

self.selectionIndicatorArrowLayer = [CALayer layer];
self.selectionIndicatorStripLayer = [CALayer layer];
Expand Down Expand Up @@ -357,7 +364,7 @@ - (void)drawRect:(CGRect)rect {

CATextLayer *titleLayer = [CATextLayer layer];
titleLayer.frame = rect;
titleLayer.alignmentMode = kCAAlignmentCenter;
titleLayer.alignmentMode = self.titleLayerAlignmentMode;
if ([UIDevice currentDevice].systemVersion.floatValue < 10.0 ) {
titleLayer.truncationMode = kCATruncationEnd;
}
Expand Down Expand Up @@ -778,7 +785,7 @@ - (CGRect)frameForFillerSelectionIndicator {
}

- (void)updateSegmentsRects {
self.scrollView.contentInset = UIEdgeInsetsZero;
self.scrollView.contentInset = self.scrollViewInsets;
self.scrollView.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));

if ([self sectionCount] > 0) {
Expand Down Expand Up @@ -937,8 +944,8 @@ - (CGFloat)totalSegmentedControlWidth {
}
}

- (void)scrollToSelectedSegmentIndex:(BOOL)animated {
[self scrollTo:self.selectedSegmentIndex animated:animated];
- (void)scrollToSelectedSegmentIndex {
[self scrollTo:self.selectedSegmentIndex animated:self.shouldScrollToUserSelectionAnimated];
}

- (void)scrollTo:(NSUInteger)index animated:(BOOL)animated {
Expand Down Expand Up @@ -995,8 +1002,8 @@ - (void)setSelectedSegmentIndex:(NSUInteger)index animated:(BOOL)animated notify
[self.selectionIndicatorStripLayer removeFromSuperlayer];
[self.selectionIndicatorBoxLayer removeFromSuperlayer];
} else {
[self scrollToSelectedSegmentIndex:animated];
[self scrollToSelectedSegmentIndex];

if (animated) {
// If the selected segment layer is not added to the super layer, that means no
// index is currently selected, so add the layer then move it to the new
Expand Down