本文转载至 http://stackoverflow.com/questions/26460706/uialertcontroller-custom-font-size-color

I am using new UIAlertController for showing alerts. I have this code:

  1. // nil titles break alert interface on iOS 8.0, so we'll be using empty strings
  2. UIAlertController *alert = [UIAlertController alertControllerWithTitle: title == nil ? @"": title
  3. message: message
  4. preferredStyle: UIAlertControllerStyleAlert];
  5. UIAlertAction *defaultAction = [UIAlertAction actionWithTitle: cancelButtonTitle
  6. style: UIAlertActionStyleCancel
  7. handler: nil];
  8. [alert addAction: defaultAction];
  9. UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
  10. [rootViewController presentViewController:alert animated:YES completion:nil];

Now I want to change title and message font, color, size and so. What's best way to do this?

Edit: I should insert whole code. I created category for UIView that I could show right alert for iOS version.

  1. @implementation UIView (AlertCompatibility)
  2. +( void )showSimpleAlertWithTitle:( NSString * )title
  3. message:( NSString * )message
  4. cancelButtonTitle:( NSString * )cancelButtonTitle
  5. {
  6. float iOSVersion = [[UIDevice currentDevice].systemVersion floatValue];
  7. if (iOSVersion < 8.0f)
  8. {
  9. UIAlertView *alert = [[UIAlertView alloc] initWithTitle: title
  10. message: message
  11. delegate: nil
  12. cancelButtonTitle: cancelButtonTitle
  13. otherButtonTitles: nil];
  14. [alert show];
  15. }
  16. else
  17. {
  18. // nil titles break alert interface on iOS 8.0, so we'll be using empty strings
  19. UIAlertController *alert = [UIAlertController alertControllerWithTitle: title == nil ? @"": title
  20. message: message
  21. preferredStyle: UIAlertControllerStyleAlert];
  22. UIAlertAction *defaultAction = [UIAlertAction actionWithTitle: cancelButtonTitle
  23. style: UIAlertActionStyleCancel
  24. handler: nil];
  25. [alert addAction: defaultAction];
  26. UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
  27. [rootViewController presentViewController:alert animated:YES completion:nil];
  28. }
  29. }
asked Oct 20 '14 at 7:45
Libor Zapletal
1,14442676
 

4 Answers

Not sure if this is against private APIs/properties but using KVC works for me on ios8

  1. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Dont care what goes here, since we're about to change below" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
  2. NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Presenting the great... Hulk Hogan!"];
  3. [hogan addAttribute:NSFontAttributeName
  4. value:[UIFont systemFontOfSize:50.0]
  5. range:NSMakeRange(24, 11)];
  6. [alertVC setValue:hogan forKey:@"attributedTitle"];
answered Oct 23 '14 at 14:34
 
    
It's working. attributedTitle for title and attributedMessage for message. Not sure if it's best solution but for now it's good enough for me. –  Libor Zapletal Oct 24 '14 at 9:18

This works fine and help to fix you're problem:

  1. - (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
  2. for (UIView *_currentView in actionSheet.subviews) {
  3. if ([_currentView isKindOfClass:[UILabel class]]) {
  4. UILabel *l = [[UILabel alloc] initWithFrame:_currentView.frame];
  5. l.text = [(UILabel *)_currentView text];
  6. [l setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20]];
  7. l.textColor = [UIColor darkGrayColor];
  8. l.backgroundColor = [UIColor clearColor];
  9. [l sizeToFit];
  10. [l setCenter:CGPointMake(actionSheet.center.x, 25)];
  11. [l setFrame:CGRectIntegral(l.frame)];
  12. [actionSheet addSubview:l];
  13. _currentView.hidden = YES;
  14. break;
  15. }
  16. }
  17. }

Also consider this tutorial about customization of the Alerts.

answered Oct 20 '14 at 7:48
 
    
I get the idea that I should iterate through subviews. But - (void)willPresentActionSheet:(UIActionSheet *)actionSheet will not help me, will it? It uses UIActionSheetDelegate and UIAlertController not, right? –  Libor Zapletal Oct 20 '14 at 8:01
    
Implement UIActionSheetDelegate and than use this method to override parameters you want – Oleg Gordiichuk Oct 20 '14 at 8:05
    
But how can I set delegate on UIAlertController? –  Libor Zapletal Oct 20 '14 at 8:57
    
I edit my answer please take a look at tutorial it will help you –  Oleg Gordiichuk Oct 20 '14 at 9:02
    
I guess I am stupid but I read the article but didn't find out anything about changing style for message and for title. –  Libor Zapletal Oct 20 '14 at 11:25

Use UIAppearance protocol. Example for setting a font - create a category to extend  UILabel:

  1. @interface UILabel (FontAppearance)
  2. @property (nonatomic, copy) UIFont * appearanceFont UI_APPEARANCE_SELECTOR;
  3. @end
  4. @implementation UILabel (FontAppearance)
  5. -(void)setAppearanceFont:(UIFont *)font {
  6. if (font)
  7. [self setFont:font];
  8. }
  9. -(UIFont *)appearanceFont {
  10. return self.font;
  11. }
  12. @end

And its usage:

  1. UILabel * appearanceLabel = [UILabel appearanceWhenContainedIn:UIAlertController.class, nil];
  2. [appearanceLabel setAppearanceFont:[UIFont boldSystemFontOfSize:10]]; //for example

Tested and working with style UIAlertControllerStyleActionSheet, but I guess it will work with UIAlertControllerStyleAlert too.

P.S. Better check for class availability instead of iOS version:

  1. if ([UIAlertController class]) {
  2. // UIAlertController code (iOS 8)
  3. } else {
  4. // UIAlertView code (pre iOS 8)
  5. }
answered Oct 20 '14 at 10:53
Ivan
213
 
    
It's working but this way I can't have different size for message and for title. –  Libor Zapletal Oct 20 '14 at 11:20

I know how to change button colour. but didn't know how to change font. you can change color of all button using below code.

  1. alertC.view.tintColor = your color;

Maybe this will help you.

UIAlertController custom font, size, color的更多相关文章

  1. 如何用Unity制作自定义字体——Custom Font

    一.效果图 二.步骤 将美术做好的字体分块导入BMFont,使用BMFont工具生成艺术字库: 将上面的数据导入unity资源目录下:*.fnt文件中记录每个文字的状态信息: 导入*.png图片并设置 ...

  2. hiho #1288 微软2016.4校招笔试题 Font Size

    #1288 : Font Size 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Steven loves reading book on his phone. The ...

  3. LaTeX :font size 修改字体大小的几种方式

    调整字体大小的几种方式,大小依次增大,具体如下: \tiny \scriptsize \footnotesize \small \normalsize \large \Large \LARGE \hu ...

  4. Expo大作战(十二)--expo中的自定义样式Custom font,以及expo中的路由Route&Navigation

    简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...

  5. GetPropInfo Font Size

    设置font size,遍历所有控件,有的控件没有font属性,所以要用GetPropInfo判断 if (GetPropInfo(cmp, "font")) function G ...

  6. unity UGUI text font size对性能影响较大

    Font Size对ugui text的性能影响非常大. <Cube Duck Run>在itouch5上测试是很流畅的,但是在iphone5上测试,在game over后显示历史最高分时 ...

  7. XE6 c++builder 设置 font size GetPropInfo SetOrdProp

    PPropInfo ppi; PTypeInfo pti; TTypeKinds ttk; TRttiContext context; TRttiType *rttiType TObject* obj ...

  8. Phone Font Size

    This table lists and describes the various font sizes that can be applied. Attribute = FontSize   Na ...

  9. iOS - UITableViewCell Custom Selection Style Color

    Customize UITextView selection color in UITableView Link : http://derekneely.com/2010/01/uitableview ...

随机推荐

  1. Excel 对应.xml/.ftl 配置(中爆导出范文)

    <?xml version="1.0"?><Workbook xmlns="urn:schemas-microsoft-com:office:sprea ...

  2. (转)spring boot实战(第六篇)加载application资源文件源码分析

    原文:http://blog.csdn.net/liaokailin/article/details/48878447

  3. REOBJECT structure

    REOBJECT structure   包含丰富编辑控件中的OLE或图像对象的信息. Syntax 语法 typedef struct _reobject { DWORD cbStruct; LON ...

  4. java动态载入指定的类或者jar包反射调用其方法

    序言 有时候.项目中会用到java动态载入指定的类或者jar包反射调用其方法来达到模块的分离,使各个功能之间耦合性大大减少,更加的模块化.代码利用率更高.模式中的代理模式就用到java的这一机制. 下 ...

  5. css处理超出文本截断问题的两种情况(多行或者单行)

    1.非多行的简单处理方式: css代码: .words{ width:400px; overflow:hidden; /*超过部分不显示*/ text-overflow:ellipsis; /*超过部 ...

  6. JAVA 的IO操作实例

    实例要求: 1,加法操作: 键盘输入两个数字,完成加法操作.因为从键盘接收过来的内容都是通过字符串形式存放的,所以此时直接通过包装类 Integer将字符串变为基本数据类型. 2,菜单显示: 采用的知 ...

  7. Centos6.2上做nginx和tomcat的集成及负载均衡(已实践)

    Centos6.2上做nginx和tomcat的集成及负载均衡 ---------------------------------------------------------Jdk-------- ...

  8. WP8简单的计算器

    <Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinition ...

  9. Atitit.js跨域解决方案attilax大总结 后台java php c#.net的CORS支持

    Atitit.js跨域解决方案attilax大总结 后台java php c#.net的CORS支持 1. 设置 document.domain为一致  推荐1 2. Apache 反向代理 推荐1 ...

  10. 无序列表li横向排列

    一.横向两列方式排列: 在网页中,很多地方都会用到无序列表横向排列的形式,通常的写法都是使得li的css样式设置为:float:left的形式即可,li会依次从最左边开始并列对齐, 例如: HTML中 ...