本文转载至 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. DevExpress.Build.targets

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <UsingTask ...

  2. MPTCP 源码分析(三) 子路径选择

    简述:      支持MPTCP的链路中存在多条子路径,因此在发送数据的时候需要选择最优路径来进行操作. MPTCP利用内核通知链对MPTCP中各子路径进行增加路径.删除路径.修改路径优先级的操作.M ...

  3. CentOS 6.4 图文安装教程(有些设置大部分教程没出现过)

    http://www.jb51.net/os/78318.html CentOS 6.4 下载地址: http://www.jb51.net/softs/78243.html 1.首先,要有一张Cen ...

  4. git 基于某个分支创建分支

    1.拷贝源代码 git clone git@git地址 cd 项目目录 2.根据已有分支创建新的分支 git checkout -b yourbranchname origin/oldbranchna ...

  5. Android - 错误:Unable to instantiate application

    错误:Unable to instantiate application 本文地址: http://blog.csdn.net/caroline_wendy 错误:java.lang.RuntimeE ...

  6. iOS即时通讯输入框随字数自适应高度

    代码地址如下:http://www.demodashi.com/demo/13210.html 前言 本人最近在研究socket与聊天界面的UI,在写聊天界面UI的时候是模仿微信的界面其中的文字输入框 ...

  7. iOS开发之实现半透明蒙层背景效果[用于下拉菜单页和分享页]

    郝萌主倾心贡献.尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助.欢迎给作者捐赠.支持郝萌主,捐赠数额任意.重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 游戏官方下 ...

  8. HTML学习笔记(四)

    1.       css行内样式要注意的问题: a)     stylekeyword要作为标签的一个属性写在标签内: b)     等号后面是一对双引號包括的内容. c)      Style属性和 ...

  9. 论C++STL源代码中关于堆算法的那些事

    关于堆,我们肯定熟知的就是它排序的时间复杂度在几个排序算法里面算是比較靠上的O(nlogn)常常会拿来和高速排序和归并排序讨论,并且它还有个长处是它的空间复杂度为O(1), 可是STL中没有给我们提供 ...

  10. 网页webbrowser

    http://www.codeproject.com/Articles/50544/Using-the-WebBrowser-Control-in-ASP-NET/