UISegmentedControl 去边框、去竖线、圆角、字体大小和颜色

话不多说,直接上代码

设置字体大小和颜色

//使用文本属性
UIFont *font = [UIFont fontWithName:fontName_bold size:18];
UIColor *textColor = [UIColor whiteColor];
NSDictionary *attributesDict = @{
                                     NSFontAttributeName:font,
                                     NSForegroundColorAttributeName:textColor
                                     };//还可以添加其他属性
//未选中状态
[segment setTitleTextAttributes:attributesDict
                               forState:UIControlStateNormal];
 //选中状态
 [segment setTitleTextAttributes:attributesDict
                           forState:UIControlStateSelected];

去边框(过期代码)

//    1.去掉整个segment颜色,现在整个segment都看不见
  self.segment.tintColor = [UIColor clearColor];
//设置文字属性其,把两个button的文字显示出来
    NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:14],NSForegroundColorAttributeName: [UIColor whiteColor]};
    [ self.segment setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];
    NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:14],NSForegroundColorAttributeName: [UIColor lightTextColor]};
    [self.segment setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];

去边框(正确姿势)

//通过设置背景图片达到目的
 [segment setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]]
                       forState:UIControlStateNormal
                     barMetrics:UIBarMetricsDefault];
  [segment setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]]
                       forState:UIControlStateSelected
                     barMetrics:UIBarMetricsDefault];
//这个方法是高亮状态的背景图,就是点击其他后闪那一下
//[segment setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]]
//                       forState:UIControlStateHighlighted
//                     barMetrics:UIBarMetricsDefault];

设置圆角、边框

//网上方法很多,我这个前提是去掉了边框
 segment.layer.cornerRadius = 10;
 segment.layer.masksToBounds = YES;
 segment.layer.borderWidth = 1;
 segment.layer.borderColor = [UIColor redColor].CGColor;

去除竖线

//也是通过设置图片达到目的
 [segment setDividerImage:[UIImage imageWithColor:[UIColor clearColor]] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

根据颜色颜色生成图片(Category)

+ (UIImage *)imageWithColor:(UIColor *)color {
    
    CGRect rect =CGRectMake(0.0f,0.0f, 1.0f,1.0f);
    
    UIGraphicsBeginImageContext(rect.size);
    
    CGContextRef context =UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    
    CGContextFillRect(context, rect);
    
    UIImage *theImage =UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return theImage;
   
}

毛玻璃效果(苹果自带的API,有兴趣可以去了解)

UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
    effectview.backgroundColor = [UIColor whiteColor];
    effectview.frame = segment.bounds;
    effectview.alpha = 0.1;
    [segment addSubview:effectview];

综合效果

![效果图][
image.png

]

你可能感兴趣的:(UISegmentedControl 去边框、去竖线、圆角、字体大小和颜色)