本文转载至 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 ...
随机推荐
- 设置Spark日志级别
编辑Spark中conf中配置文件log4j.properties 设置日志级别为WARN,即:log4j.rootCategory=WARN, console
- Centos 7 进入单用户模式图文详解
由于昨晚做了一个很傻X的事情,所以有幸进入了CentOS 7 的单用户模式. CentOS 7 在进入单用户的时候和6.x做了很多的改变, 下面让我们来看看如何进入单用户模式. 如何进入CentOS ...
- iOS适配整理
iOS12适配问题 1.StatusBar内部结构改变 现象:crash crash log: -[_UIStatusBarIdentifier isEqualToString:]: unrecogn ...
- TP框架中模糊查询实现
TP框架中模糊查询实现 $where['g.name'] = array('like','%'.$groupname.'%'); 表达式查询 上面的查询条件仅仅是一个简单的相等判断,可以使用查询表达式 ...
- mod_tile编译出错 -std=c++11 or -std=gnu++11
make[1]: 正在进入文件夹 /home/wml/src/mod_tile-master' depbase=echo src/gen_tile.o | sed 's|[^/]*$|.deps/&a ...
- Fiddler4插件开发实践
Fiddler4 是一款 巴拉巴拉..... 连接在这:http://www.telerik.com/fiddler 开发文档在这:http://docs.telerik.com/fiddler/Ex ...
- 转:VLC搭建RTSP直播流,图文介绍
将一个视频转成rtsp流,通过vlc播放器,搭建一个rtsp服务器,让rtsp客户端去访问这个视频的rtsp流 1 需要有vlc播放器,我的版本如下 2 媒体 --> 流 3 添加视频文件 ...
- Filter介绍,运行顺序,实例
Filter介绍 Filter可觉得是Servlet的一种"变种",它主要用于对用户请求进行预处理,也能够对HttpServletResponse进行后处理,是个典型的处理链. 它 ...
- win10 VS code 编译运行 C/C++的方法
具体配置过程如下链接: https://zhuanlan.zhihu.com/p/35178331 但中间出了点问题:CTRL+ALT+n 运行后: PS D:\C++> cd "d: ...
- NIO之Channel聚集(gather)写入与分散(scatter)读取
Channel聚集(gather)写入 聚集写入( Gathering Writes)是指将多个 Buffer 中的数据“聚集”到 Channel. 特别注意:按照缓冲区的顺序,写入 position ...