IOS 修改UIAlertController的按鈕標題的字體顏色,字號,內容

這裏寫圖片描述
現在只是實現了功能,原理是通過runtime獲取對應的可以修改的key。具體還後續的學習中進行補充

主要的代碼:

//按鈕點擊

- (IBAction)buttonClick:(UIButton *)sender {

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"提示內容" preferredStyle:UIAlertControllerStyleAlert];


    //修改標題的內容,字號,顏色。使用的key值是“attributedTitle”
    NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"heihei"];
    [hogan addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:50] range:NSMakeRange(0, [[hogan string] length])];
    [hogan addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, [[hogan string] length])];
    [alertController setValue:hogan forKey:@"attributedTitle"];

    //修改按鈕的顏色,同上可以使用同樣的方法修改內容,樣式
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Default" style:UIAlertActionStyleDefault handler:nil];
     [defaultAction setValue:[UIColor blueColor] forKey:@"_titleTextColor"];

     UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];

     [cancelAction setValue:[UIColor greenColor] forKey:@"_titleTextColor"];

    [alertController addAction:defaultAction];
    [alertController addAction:cancelAction];

    [self presentViewController:alertController animated:YES completion:nil];


}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章