HMSegmentedControl源代码解析(9)
- UID
- 1066743
|
HMSegmentedControl源代码解析(9)
- (void)drawRectCGRect)rect {
// 设置背景填充色
[self.backgroundColor setFill];
UIRectFill([self bounds]);
// 箭头layer指示器的颜色
self.selectionIndicatorArrowLayer.backgroundColor = self.selectionIndicatorColor.CGColor;
self.selectionIndicatorStripLayer.backgroundColor = self.selectionIndicatorColor.CGColor;
// boxlayer背景颜色与线框颜色
self.selectionIndicatorBoxLayer.backgroundColor = self.selectionIndicatorBoxColor.CGColor;
self.selectionIndicatorBoxLayer.borderColor = self.selectionIndicatorBoxColor.CGColor;
// Remove all sublayers to avoid drawing images over existing ones
// 移除scrollview的所有的layer防止重绘
self.scrollView.layer.sublayers = nil;
CGRect oldRect = rect;
if (self.type == HMSegmentedControlTypeText) {
//当segment的type 等于文字的时候
[self.sectionTitles enumerateObjectsUsingBlock:^(id titleString, NSUInteger idx, BOOL *stop) {
// 初始化文字高度宽度
CGFloat stringWidth = 0;
CGFloat stringHeight = 0;
// 计算文字在有无富文本的高度
CGSize size = [self measureTitleAtIndex:idx];
stringWidth = size.width;
stringHeight = size.height;
CGRect rectDiv = CGRectZero;
CGRect fullRect = CGRectZero;
// Text inside the CATextLayer will appear blurry unless the rect values are rounded
// 判断指示器位置是否在上方
BOOL locationUp = (self.selectionIndicatorLocation == HMSegmentedControlSelectionIndicatorLocationUp);
// 判断指示器是不是box类型
BOOL selectionStyleNotBox = (self.selectionStyle != HMSegmentedControlSelectionStyleBox);
// y =(frame的高度 - 是否是box类型 * 指示器的高度) / 2 - 文字高度 / 2 + 指示器的高度 * 是否在上方
CGFloat y = roundf((CGRectGetHeight(self.frame) - selectionStyleNotBox * self.selectionIndicatorHeight) / 2 - stringHeight / 2 + self.selectionIndicatorHeight * locationUp);
CGRect rect;
if (self.segmentWidthStyle == HMSegmentedControlSegmentWidthStyleFixed) {
// 当width 的类型是 fixed的时候
rect = CGRectMake((self.segmentWidth * idx) + (self.segmentWidth - stringWidth) / 2, y, stringWidth, stringHeight);
rectDiv = CGRectMake((self.segmentWidth * idx) - (self.verticalDividerWidth / 2), self.selectionIndicatorHeight * 2, self.verticalDividerWidth, self.frame.size.height - (self.selectionIndicatorHeight * 4));
fullRect = CGRectMake(self.segmentWidth * idx, 0, self.segmentWidth, oldRect.size.height);
} else {
// When we are drawing dynamic widths, we need to loop the widths array to calculate the xOffset
CGFloat xOffset = 0;
NSInteger i = 0;
for (NSNumber *width in self.segmentWidthsArray) {
if (idx == i)
break;
xOffset = xOffset + [width floatValue];
i++;
}
CGFloat widthForIndex = [[self.segmentWidthsArray objectAtIndex:idx] floatValue];
rect = CGRectMake(xOffset, y, widthForIndex, stringHeight);
fullRect = CGRectMake(self.segmentWidth * idx, 0, widthForIndex, oldRect.size.height);
rectDiv = CGRectMake(xOffset - (self.verticalDividerWidth / 2), self.selectionIndicatorHeight * 2, self.verticalDividerWidth, self.frame.size.height - (self.selectionIndicatorHeight * 4));
}
// Fix rect position/size to avoid blurry labels |
|
|
|
|
|