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

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

    // nil titles break alert interface on iOS 8.0, so we'll be using empty strings
UIAlertController *alert = [UIAlertController alertControllerWithTitle: title == nil ? @"": title
message: message
preferredStyle: UIAlertControllerStyleAlert]; UIAlertAction *defaultAction = [UIAlertAction actionWithTitle: cancelButtonTitle
style: UIAlertActionStyleCancel
handler: nil]; [alert addAction: defaultAction]; UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
[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.

@implementation UIView (AlertCompatibility)

+( void )showSimpleAlertWithTitle:( NSString * )title
message:( NSString * )message
cancelButtonTitle:( NSString * )cancelButtonTitle
{
float iOSVersion = [[UIDevice currentDevice].systemVersion floatValue];
if (iOSVersion < 8.0f)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: title
message: message
delegate: nil
cancelButtonTitle: cancelButtonTitle
otherButtonTitles: nil];
[alert show];
}
else
{
// nil titles break alert interface on iOS 8.0, so we'll be using empty strings
UIAlertController *alert = [UIAlertController alertControllerWithTitle: title == nil ? @"": title
message: message
preferredStyle: UIAlertControllerStyleAlert]; UIAlertAction *defaultAction = [UIAlertAction actionWithTitle: cancelButtonTitle
style: UIAlertActionStyleCancel
handler: nil]; [alert addAction: defaultAction]; UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootViewController presentViewController:alert animated:YES completion:nil];
}
}
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

UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Dont care what goes here, since we're about to change below" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Presenting the great... Hulk Hogan!"];
[hogan addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:50.0]
range:NSMakeRange(24, 11)];
[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:

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
for (UIView *_currentView in actionSheet.subviews) {
if ([_currentView isKindOfClass:[UILabel class]]) {
UILabel *l = [[UILabel alloc] initWithFrame:_currentView.frame];
l.text = [(UILabel *)_currentView text];
[l setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20]];
l.textColor = [UIColor darkGrayColor];
l.backgroundColor = [UIColor clearColor];
[l sizeToFit];
[l setCenter:CGPointMake(actionSheet.center.x, 25)];
[l setFrame:CGRectIntegral(l.frame)];
[actionSheet addSubview:l];
_currentView.hidden = YES;
break;
}
}
}

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:

@interface UILabel (FontAppearance)
@property (nonatomic, copy) UIFont * appearanceFont UI_APPEARANCE_SELECTOR;
@end @implementation UILabel (FontAppearance) -(void)setAppearanceFont:(UIFont *)font {
if (font)
[self setFont:font];
} -(UIFont *)appearanceFont {
return self.font;
} @end

And its usage:

UILabel * appearanceLabel = [UILabel appearanceWhenContainedIn:UIAlertController.class, nil];
[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:

if ([UIAlertController class]) {
// UIAlertController code (iOS 8)
} else {
// UIAlertView code (pre iOS 8)
}
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.

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. Docker解析及轻量级PaaS平台演练(四)--Fig相关介绍

    本篇中将会使用开源工具Fig Fig是什么? 简单的说就是对Docker的封装,从而方便我们构建应用的运行环境 它所做的事情是协调Docker上的各个Container之间的联系,并通过服务发现的方式 ...

  2. 数据写入到TXT文档中

    public class FileWrite { public File file; public FileOutputStream stream = null; //每次写入都会覆盖之前的内容 pu ...

  3. POJ 2942 Knights of the Round Table 黑白着色+点双连通分量

    题目来源:POJ 2942 Knights of the Round Table 题意:统计多个个骑士不能參加随意一场会议 每场会议必须至少三个人 排成一个圈 而且相邻的人不能有矛盾 题目给出若干个条 ...

  4. .Net——实现IConfigurationSectionHandler接口定义处理程序处理自己定义节点

    除了使用.net里面提供的内置处理程序来处理我们的自己定义节点外,我们还能够通过多种方法,来自己定义处理类处理我们的自己定义节点,本文主要介绍通过实现IConfigurationSectionHand ...

  5. app接口开发(php)

    1.JSON方式封装通信接口: 封装: response.php <?php // JSON方式封装通信接口 // 定义 response类 class Response { // 定义一个静态 ...

  6. Cocos2dx 3.6源代码编译错误:syntax error : missing &#39;)&#39; before &#39;{&#39;

    在编译Cocos2dx 3.6版本号时.发现编译错误: 定位代码行: debugForNormalSprite->drawPoints(positions, 4, 8, Color4F{0.0, ...

  7. Java 内部类种类及使用解析【转】

    内部类Inner Class 将相关的类组织在一起,从而降低了命名空间的混乱. 一个内部类可以定义在另一个类里,可以定义在函数里,甚至可以作为一个表达式的一部分. Java中的内部类共分为四种: 静态 ...

  8. Eclipse没有 web Project 选项的解决办法

    装下插件即可.步骤如下: 选择 Help >Software Updates >Find and Install.这个选项会让您可以下载和安装 Web 工具,且无需转到 Web 站点. 选 ...

  9. CentOS设置sendmail发件人,让sendmail不显示通过XXX代发

    0.有一个十分快速的方法 命令:hostname itzhanzhang.com,但是重启后会失效,于是请接着往下看一劳永逸的方法: 1.设置你的主机名 默认的主机名是类似于“VM_24_76_cen ...

  10. int 与 string::length()

    今天在代码中遇到这样的问题 ; while (nStart < strTemp.length()) { ... } 感觉自己写的逻辑没有错误,但是,代码执行结果就是不对,结果单步调试到该处发现, ...