本文转载至 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 ...
随机推荐
- hadoop2.2.0集群安装和配置
hadoop2.0已经发布了稳定版本了,增加了很多特性,比如HDFS HA.YARN等. 注意:apache提供的hadoop-2.2.0的安装包是在32位操作系统编译的,因为hadoop依赖一些C+ ...
- stat,查看文件属性
shell命令,查看文件详细属性 别再跟我回答ls -l了
- JavaScript 数字与字符串 比较大小
总结一下JS中经常遇到纯数字和各种各样的字符串进行比较: 纯数字之间的比较 alert(1<3);//true 数字字符串比较,会将其先转成数字 alert("1"<& ...
- Amixer 控制声音
amixer set Master XXXX 就可以直接控制主声卡属性 amixer set Master 20 #设置主声卡声音为 20 amixer set Master off #关闭主声卡(静 ...
- spring中使用 @value 简化配置文件的读取
1.在applicationContext.xml文件中配置properties文件 <bean id="propertyConfigurer" class="or ...
- 通过内存映射文件来颠倒文本内容(暂没有处理Unicode和换行符)
// ReverseFileDemo.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <windows.h> ...
- eclipse日志
注意包别引入错: import org.slf4j.Logger; import org.slf4j.LoggerFactory; private static final Logger log = ...
- linux 使用fdisk分区扩容,看介绍命令(未完)
https://www.cnblogs.com/chenmh/p/5096592.html LVM 逻辑磁盘的一些命令 http://man.linuxde.net/vgcreate
- unity, iOS下画面错乱解法
unity版本号为5.1.1f1 Personal 在ipod5,系统为iOS7.1上测试.发现下面两种出现画面错乱的问题: 一,退后台在返回前台时画面发生错乱(错乱持续一两秒,然后变为正常). ...
- Java里的并发容器与安全共享策略总结
一.并发容器 ArrayList --> CopyOnWriteArrayList 概念 : 简单的讲就是写操作时赋值,当有新元素添加到CopyOnWriteArrayList时,它先从原有的数 ...