if ([title isEqualToString:_selectString]) {
_selectIndex = i;
break;
}
}
}
for (int i = 0; i < _titleArray.count; i ++) {
CGSize size = [self measureTitleIndex:i];
NSNumber *value = [NSNumber numberWithFloat:size.width];
[self.widthsArray addObject:value];
_totalContentWidth = _totalContentWidth + size.width;
if (i < _titleArray.count - 1) {
_totalContentWidth = _totalContentWidth + _margin_space;
}
}
_totalContentWidth = _totalContentWidth + _margin_left + _margin_right;
}
- (CGSize)measureTitleIndex:(NSUInteger)index {
if (index >= _titleArray.count) {
return CGSizeZero;
}
id title = _titleArray[index];
CGSize size = CGSizeZero;
BOOL selected = (index == _selectIndex);
NSDictionary *titleAttrs = selected ? [self resultingSelectedTitleTextAttributes] : [self resultingTitleTextAttributes];
size = [(NSString *)title sizeWithAttributes:titleAttrs];
UIFont *font = titleAttrs[@"NSFont"];
size = CGSizeMake(ceil(size.width), ceil(size.height - font.descender));
CGSize resault = CGRectIntegral((CGRect){CGPointZero, size}).size;
return resault;
}
- (NSDictionary *)resultingSelectedTitleTextAttributes {
NSDictionary *resultingAttrs = @{NSForegroundColorAttributeName : [UIColor blackColor] ,NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:18.0]};
return resultingAttrs;
}
- (NSDictionary *)resultingTitleTextAttributes {
NSDictionary *resultingAttrs = @{NSForegroundColorAttributeName : [UIColor lightGrayColor],NSFontAttributeName:[UIFont systemFontOfSize:14.0]};
return resultingAttrs;
}
#pragma mark ========== 计算下划线位置 ==========
- (CGRect)measureLineFrame{
CGRect lineRect = CGRectZero;
CGFloat lineRectX = 0;
for (int i = 0; i < _selectIndex; i ++) {
NSNumber *number = self.widthsArray[i];
lineRectX = lineRectX + [number floatValue] + _margin_space;
}
CGFloat widthSelect = [self.widthsArray[_selectIndex] floatValue];
CGFloat lastLocation = widthSelect >= _line_width ? (widthSelect - _line_width)/2 : (_line_width - widthSelect)/2;
lineRectX = lineRectX + _margin_left + lastLocation;
lineRect = CGRectMake(lineRectX, self.bounds.size.height - _line_height - 2, _line_width, _line_height);
return lineRect;
}
#pragma mark ========== 计算偏移量 ==========
- (CGFloat)measureContentOffsetX{
CGFloat selfWidth = self.bounds.size.width;
///先计算点击的item中心点
CGFloat selectedCenterX = 0;
for (int i = 0; i < _selectIndex; i ++) {
NSNumber *number = self.widthsArray[i];
selectedCenterX = selectedCenterX + [number floatValue] + _margin_space;
}
CGFloat widthSelect = [self.widthsArray[_selectIndex] floatValue];
selectedCenterX = selectedCenterX + widthSelect/2;
if (_totalContentWidth <= selfWidth) {///充满内部不做偏移
return 0;
}
if (selectedCenterX <= selfWidth/2) {
return 0;
}
else if (selectedCenterX >= _totalContentWidth - selfWidth/2){
return _totalContentWidth - selfWidth;
}
else{
return selectedCenterX - selfWidth/2;
}
}
#pragma mark ========== 代理 ==========
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return _titleArray.count;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UIColle |