記錄
//初始化一個(gè)UISegmentedControl控件test
UISegmentedControl *test = [[UISegmentedControl alloc]initWithItems:@[@"一",@"二",@"三",@"四",@"五"]];
test.frame = CGRectMake(0, 200, 400, 50);
//設(shè)置無邊框分段控制器
//設(shè)置test控件的顏色為透明
test.tintColor = [UIColor clearColor];
//定義選中狀態(tài)的樣式selected介粘,類型為字典
NSDictionary *selected = @{NSFontAttributeName:[UIFont systemFontOfSize:20],
NSForegroundColorAttributeName:[UIColor redColor]};
//定義未選中狀態(tài)下的樣式normal,類型為字典
NSDictionary *normal = @{NSFontAttributeName:[UIFont systemFontOfSize:10],
NSForegroundColorAttributeName:[UIColor blackColor]};
/*
NSFontAttributeName -> 系統(tǒng)宏定義的特殊鍵偎谁,用來給格式字典中的字體賦值
NSForegroundColorAttributeName -> 系統(tǒng)宏定義的特殊鍵巡雨,用來給格式字典中的字體顏色賦值
*/
//通過setTitleTextAttributes: forState: 方法來給test控件設(shè)置文字內(nèi)容的格式
[test setTitleTextAttributes:normal forState:UIControlStateNormal];
[test setTitleTextAttributes:selected forState:UIControlStateSelected];
//設(shè)置test初始狀態(tài)下的選中下標(biāo)
test.selectedSegmentIndex = 0;
//將test添加到某個(gè)View上
[self.view addSubview:test];
屏幕快照 2017-11-07 上午10.57.01.png