Skip to content

Commit

Permalink
Add auto-calculate bottomline width function.
Browse files Browse the repository at this point in the history
Signed-off-by: RamWire <fy.zhao@renrunkeji.com>
  • Loading branch information
RamWire committed Nov 2, 2016
1 parent 1b9256b commit 74b83a4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions NinaPagerView/NinaBaseView.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
@property (assign, nonatomic) CGFloat topHeight; /**< TopTab高度 **/
@property (assign, nonatomic) BOOL topTabUnderLineHidden; /**< 是否显示下方的下划线 **/
@property (assign, nonatomic) BOOL slideEnabled; /**< 允许下方左右滑动 **/
@property (assign, nonatomic) BOOL autoFitTitleLine; /**< 下划线是否自适应标题宽度 **/
@property (strong, nonatomic) UIColor *btnUnSelectColor; /**< 未选中的标题颜色 **/
@property (strong, nonatomic) UIColor *btnSelectColor; /**< 选中的标题颜色 **/
@property (strong, nonatomic) UIColor *underlineBlockColor; /**< 下划线或滑块颜色 **/
Expand Down
13 changes: 13 additions & 0 deletions NinaPagerView/NinaBaseView.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ @implementation NinaBaseView
UIView *lineBottom;
UIView *topTabBottomLine;
NSMutableArray *btnArray;
NSMutableArray *bottomLineWidthArray;
NSInteger topTabType;
UIView *ninaMaskView;
}
Expand Down Expand Up @@ -81,6 +82,10 @@ - (void)setSlideEnabled:(BOOL)slideEnabled {
_slideEnabled = slideEnabled;
}

- (void)setAutoFitTitleLine:(BOOL)autoFitTitleLine {
_autoFitTitleLine = autoFitTitleLine;
}

- (void)setTopHeight:(CGFloat)topHeight {
_topHeight = topHeight;
}
Expand Down Expand Up @@ -150,11 +155,13 @@ - (UIScrollView *)topTab {
}
}
btnArray = [NSMutableArray array];
bottomLineWidthArray = [NSMutableArray array];
for (NSInteger i = 0; i < _titleArray.count; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = i;
button.titleLabel.font = [UIFont systemFontOfSize:_titlesFont];
if ([_titleArray[i] isKindOfClass:[NSString class]]) {
[bottomLineWidthArray addObject:[NSString stringWithFormat:@"%f",[_titleArray[i] sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:_titlesFont]}].width]];
[button setTitle:_titleArray[i] forState:UIControlStateNormal];
button.titleLabel.numberOfLines = 0;
button.titleLabel.textAlignment = NSTextAlignmentCenter;
Expand Down Expand Up @@ -249,6 +256,9 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (_titleArray.count > 5) {
yourCount = 1.0 / 5.0;
}
if (_autoFitTitleLine) {
_bottomLinePer = [bottomLineWidthArray[yourPage] floatValue] / (FUll_VIEW_WIDTH * yourCount);
}
CGFloat lineBottomDis = yourCount * FUll_VIEW_WIDTH * (1 -_bottomLinePer) / 2;
if (topTabType == 1) {
CGPoint maskCenter = ninaMaskView.center;
Expand Down Expand Up @@ -344,6 +354,9 @@ - (void)initUI {
additionCount = (_titleArray.count - 5.0) / 5.0;
yourCount = 1.0 / 5.0;
}
if (_autoFitTitleLine) {
_bottomLinePer = [bottomLineWidthArray[0] floatValue] / (FUll_VIEW_WIDTH * yourCount);
}
CGFloat lineBottomDis = yourCount * FUll_VIEW_WIDTH * (1 -_bottomLinePer) / 2;
NSInteger defaultPage = (_baseDefaultPage > 0 && _baseDefaultPage < _titleArray.count)?_baseDefaultPage:0;
switch (topTabType) {
Expand Down
5 changes: 5 additions & 0 deletions NinaPagerView/NinaPagerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ typedef NS_ENUM(NSInteger, NinaPagerStyle) {
* Able to scroll scrollview in main view,default is YES.
**/
@property (assign, nonatomic) BOOL nina_scrollEnabled;
/**<
* 是否选择自适应下划线长度(与当前标题文字长度相同),默认为NO,如果设置了selectBottomLinePer属性,此属性将不起作用。
* Auto-calculate bottomline's width,the same with current title's width,default is NO,if you set selectBottomLinePer,this property will not work.
**/
@property (assign, nonatomic) BOOL nina_autoBottomLineEnable;

@property (weak, nonatomic) id<NinaPagerViewDelegate>delegate; /**< NinaPagerView代理 **/

Expand Down
9 changes: 8 additions & 1 deletion NinaPagerView/NinaPagerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ @interface NinaPagerView()<NSCacheDelegate>
@property (nonatomic, strong) NSCache *limitControllerCache; /**< 缓存限制 **/
@property (nonatomic, strong) NinaBaseView *ninaBaseView; /**< 创建NinaBaseView **/
@property (nonatomic, assign) BOOL hasSettingScrollEnabled;
@property (nonatomic, assign) BOOL hasSettingLinePer;
@end

@implementation NinaPagerView
Expand Down Expand Up @@ -89,6 +90,7 @@ - (void)setSliderHeight:(CGFloat)sliderHeight {

- (void)setSelectBottomLinePer:(CGFloat)selectBottomLinePer {
_selectBottomLinePer = selectBottomLinePer;
_hasSettingLinePer = YES;
}

- (void)setSelectBottomLineHeight:(CGFloat)selectBottomLineHeight {
Expand All @@ -108,6 +110,10 @@ - (void)setNina_scrollEnabled:(BOOL)nina_scrollEnabled {
_hasSettingScrollEnabled = YES;
}

- (void)setNina_autoBottomLineEnable:(BOOL)nina_autoBottomLineEnable {
_nina_autoBottomLineEnable = nina_autoBottomLineEnable;
}

- (void)setTopTabHeight:(CGFloat)topTabHeight {
_topTabHeight = topTabHeight;
}
Expand Down Expand Up @@ -162,7 +168,8 @@ - (void)loadDataForView {
self.ninaBaseView.topTabUnderLineHidden = _underLineHidden;
self.ninaBaseView.slideEnabled = _hasSettingScrollEnabled?_nina_scrollEnabled:YES;
self.ninaBaseView.blockHeight = _sliderHeight > 0?_sliderHeight:tabHeight;
self.ninaBaseView.bottomLinePer = _selectBottomLinePer > 0?_selectBottomLinePer:1;
self.ninaBaseView.bottomLinePer = (_selectBottomLinePer > 0 && _selectBottomLinePer < 1 && _hasSettingLinePer)?_selectBottomLinePer:1;
self.ninaBaseView.autoFitTitleLine = (_nina_autoBottomLineEnable && !_hasSettingLinePer)?_nina_autoBottomLineEnable:NO;
self.ninaBaseView.bottomLineHeight = _selectBottomLineHeight > 0?_selectBottomLineHeight:2;
self.ninaBaseView.sliderCornerRadius = _slideBlockCornerRadius > 0?_slideBlockCornerRadius:0;
self.ninaBaseView.titleArray = titlesArray;
Expand Down

0 comments on commit 74b83a4

Please sign in to comment.