UISegmentedControl 去邊框、去豎線、圓角、字體大小和顏色

話不多說(shuō),直接上代碼

設(shè)置字體大小和顏色

//使用文本屬性
UIFont *font = [UIFont fontWithName:fontName_bold size:18];
UIColor *textColor = [UIColor whiteColor];
NSDictionary *attributesDict = @{
                                     NSFontAttributeName:font,
                                     NSForegroundColorAttributeName:textColor
                                     };//還可以添加其他屬性
//未選中狀態(tài)
[segment setTitleTextAttributes:attributesDict
                               forState:UIControlStateNormal];
 //選中狀態(tài)
 [segment setTitleTextAttributes:attributesDict
                           forState:UIControlStateSelected];

去邊框(過(guò)期代碼)

//    1.去掉整個(gè)segment顏色,現(xiàn)在整個(gè)segment都看不見(jiàn)
  self.segment.tintColor = [UIColor clearColor];
//設(shè)置文字屬性其,把兩個(gè)button的文字顯示出來(lái)
    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];

去邊框(正確姿勢(shì))

//通過(guò)設(shè)置背景圖片達(dá)到目的
 [segment setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]]
                       forState:UIControlStateNormal
                     barMetrics:UIBarMetricsDefault];
  [segment setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]]
                       forState:UIControlStateSelected
                     barMetrics:UIBarMetricsDefault];
//這個(gè)方法是高亮狀態(tài)的背景圖,就是點(diǎn)擊其他后閃那一下
//[segment setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]]
//                       forState:UIControlStateHighlighted
//                     barMetrics:UIBarMetricsDefault];

設(shè)置圓角、邊框

//網(wǎng)上方法很多,我這個(gè)前提是去掉了邊框
 segment.layer.cornerRadius = 10;
 segment.layer.masksToBounds = YES;
 segment.layer.borderWidth = 1;
 segment.layer.borderColor = [UIColor redColor].CGColor;

去除豎線

//也是通過(guò)設(shè)置圖片達(dá)到目的
 [segment setDividerImage:[UIImage imageWithColor:[UIColor clearColor]] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

根據(jù)顏色顏色生成圖片(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;
   
}

毛玻璃效果(蘋(píng)果自帶的API,有興趣可以去了解)

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

綜合效果

![效果圖][
image.png

]

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 萬(wàn)事萬(wàn)物有時(shí)、有運(yùn)、有勢(shì)。 ‘’時(shí)”是時(shí)機(jī),有天時(shí)而運(yùn)氣未至,也難免落空;“運(yùn)”是天時(shí)、地利、人和的和合,三者沒(méi)和...
    方亞舟閱讀 929評(píng)論 0 1
  • 點(diǎn)絳唇 奉天轉(zhuǎn)涼 暮深城里暖衫薄 對(duì)燭孤酌 只影空酬酢 鑒憐經(jīng)往 稚女求學(xué)索 北風(fēng)惡 漫漫長(zhǎng)路 陣陣蕭葉落 ...
    宋韻遺風(fēng)閱讀 194評(píng)論 1 7
  • 只看看的旅游是沒(méi)有意義的,但帶著目的去旅游,或許這就是未來(lái)需要的,看一些美的東西,即能賞心悅目也能完全學(xué)習(xí)到想要學(xué)...
    劉小盜閱讀 113評(píng)論 0 0