最近有一個需求
希望可以客製化alertView
大概就是可以改改字體大小,顏色之類的~
原來的是用UIAlertView 但iOS 8 都整合進UIAlertController 不管是alert 還是actionsheet 都可以用
先來Demo 結果
直接上程式碼
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"客製化AlertController" message:@"" preferredStyle:UIAlertControllerStyleAlert];
NSMutableAttributedString *alertTitle = [[NSMutableAttributedString alloc] initWithString:@"各種顏色AlertController"];
[alertTitle addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:30.0]
range:NSMakeRange(0, 4)];
[alertVC setValue:alertTitle forKey:@"attributedTitle"];
NSMutableAttributedString *alertmessage = [[NSMutableAttributedString alloc] initWithString:@"\n字體大小\n顏色\n\n可自己控制"];
[alertmessage addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:12.0]
range:[alertmessage.string rangeOfString:@"字體大小"]];
[alertmessage addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:[alertmessage.string rangeOfString:@"顏色"]];
[alertmessage addAttribute:NSForegroundColorAttributeName
value:[UIColor blueColor]
range:[alertmessage.string rangeOfString:@"可自己控制"]];
[alertVC setValue:alertmessage forKey:@"attributedMessage"];
UIAlertAction *button = [UIAlertAction actionWithTitle:@"確定"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action){
//add code to make something happen once tapped
}];
[alertVC addAction:button];
[self presentViewController:alertVC animated:YES completion:nil];
不過....有點不確定
因為其實有點用到private api
程式碼中間是我覺得用到的地方
不過效果應該還算OK~
有人可以試試看是否可以上架成功~~~
我就不試了
另外也可以加上view 直接刻View 其實也是不錯的選擇~