HMSegmentControl (iOS10)

在iOS10,Xcode8beta上运行HMSegmentControl时会出现,title不显示的情况,如图:


HMSegmentControl (iOS10)_第1张图片

在源码中做如下修改即可正常显示,添加方法

- (UIFont *)attributedFontAtIndex:(NSInteger)index

{

BOOL selected = (index == self.selectedSegmentIndex) ? YES : NO;

NSDictionary *titleAttrs = selected ? [self resultingSelectedTitleTextAttributes] : [self resultingTitleTextAttributes];

UIFont *font = [titleAttrs objectForKey:NSFontAttributeName];

if (!font) {

font = [UIFont systemFontOfSize:18];

}

return font;

}


- (UIColor *)attributedColorAtIndex:(NSInteger)index

{

BOOL selected = (index == self.selectedSegmentIndex) ? YES : NO;

NSDictionary *titleAttrs = selected ? [self resultingSelectedTitleTextAttributes] : [self resultingTitleTextAttributes];

UIColor *color = [titleAttrs objectForKey:NSForegroundColorAttributeName];

if (!color) {

color = [UIColor blackColor];

}

return color;

}

在drawRect:方法中:

CATextLayer *titleLayer = [CATextLayer layer];

titleLayer.frame = rect;

titleLayer.alignmentMode = kCAAlignmentCenter;

titleLayer.truncationMode = kCATruncationEnd;

titleLayer.string = [self attributedTitleAtIndex:idx];

titleLayer.contentsScale = [[UIScreen mainScreen] scale];

修改成:

 UIFont *font = [self attributedFontAtIndex:idx];

 UIColor *color = [self attributedColorAtIndex:idx];

CATextLayer *titleLayer = [[CATextLayer alloc] init];

titleLayer.frame = rect;

titleLayer.alignmentMode = kCAAlignmentCenter;

titleLayer.truncationMode = kCATruncationEnd;

titleLayer.string = [self attributedTitleAtIndex:idx];

 titleLayer.fontSize = font.pointSize;

 titleLayer.font = (__bridge CFTypeRef _Nullable)(font.fontName);

titleLayer.string = (NSString *)titleString;

 titleLayer.foregroundColor = color.CGColor;

titleLayer.contentsScale = [[UIScreen mainScreen] scale];


在修改过程中并未对源码或者多余部分进行删除,修改 只是简单让title显示出来,这里只简单介绍一种思路。

你可能感兴趣的:(HMSegmentControl (iOS10))