本文转载至 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];
}
}
|
| |
|
|
|
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
|
|
| |
|
|
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.
|
- 如何用Unity制作自定义字体——Custom Font
一.效果图 二.步骤 将美术做好的字体分块导入BMFont,使用BMFont工具生成艺术字库: 将上面的数据导入unity资源目录下:*.fnt文件中记录每个文字的状态信息: 导入*.png图片并设置 ...
- hiho #1288 微软2016.4校招笔试题 Font Size
#1288 : Font Size 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Steven loves reading book on his phone. The ...
- LaTeX :font size 修改字体大小的几种方式
调整字体大小的几种方式,大小依次增大,具体如下: \tiny \scriptsize \footnotesize \small \normalsize \large \Large \LARGE \hu ...
- Expo大作战(十二)--expo中的自定义样式Custom font,以及expo中的路由Route&Navigation
简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...
- GetPropInfo Font Size
设置font size,遍历所有控件,有的控件没有font属性,所以要用GetPropInfo判断 if (GetPropInfo(cmp, "font")) function G ...
- unity UGUI text font size对性能影响较大
Font Size对ugui text的性能影响非常大. <Cube Duck Run>在itouch5上测试是很流畅的,但是在iphone5上测试,在game over后显示历史最高分时 ...
- XE6 c++builder 设置 font size GetPropInfo SetOrdProp
PPropInfo ppi; PTypeInfo pti; TTypeKinds ttk; TRttiContext context; TRttiType *rttiType TObject* obj ...
- Phone Font Size
This table lists and describes the various font sizes that can be applied. Attribute = FontSize Na ...
- iOS - UITableViewCell Custom Selection Style Color
Customize UITextView selection color in UITableView Link : http://derekneely.com/2010/01/uitableview ...
随机推荐
- orchard project 本地化
http://orchardproject.net/localization 本地化 果园的本地化管理是托管在一个外部服务( Crowdin), 的项目.公众和贡献是受欢迎的! 如何做出贡献 注册上 ...
- 菜鸟调错(五)——jetty执行时无法保存文件
背景交代: 上一篇博客写的是用jetty和Maven做开发.測试.在使用的过程中遇到一个小问题.就是在jetty启动以后,改动了jsp.xml等文件无法保存. 错误信息: 解决方式: 到Maven库( ...
- 微信小程序 - 回到自己位置(map)
演示效果: 图片资源 index.js /** * 回到自己位置,在cover-image上绑定点击事件即可. */ clickcontrol(e) { let mpCtx = wx.createMa ...
- HDU1157 Who's in the Middle
Who's in the Middle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- 经验总结54--搭建linux虚拟机环境
好久没写博客了.写一篇环境搭建吧. 自己做java,有时会接触linux环境,自己能够搭建一个,方便自己做实验和学习. 1.下载VM11. 下载并依照VM:http://rj.baidu.com/so ...
- 【Python3 爬虫】15_Fiddler抓包分析
我们要抓取一些网页源码看不到的信息,例如:淘宝的评论等 我们可以使用工具Fiddler进行抓取 软件下载地址:https://pan.baidu.com/s/1nPKPwrdfXM62LlTZsoiD ...
- 在线激活Pycharm(亲测有效)
(1)在激活界面的License server输入:http://idea.liyang.io:或者:点击help→Register→License sever ,输入http://idea.liya ...
- SSH——增删改的实现二
二.批量删除 逻辑删除取派员,将取派员的deltag改为“1” 1. 为“作废”按钮绑定事件 //批量删除取派员 function doDelete(){ //获得选中的行 var rows = $( ...
- silverlight计时器
引用using System.Windows.Threading; DispatcherTimer dispatchertimer = new DispatcherTimer();//创建一个新的计时 ...
- android-异步消息处理机制初步
Android的异步消息处理主要由4个部分组成,Message.Handler.MessageQueue和Looper Message:在线程之间传递的消息,它可以在内部携带少量的信息,用于在不同线程 ...