码迷,mamicode.com
首页 > 其他好文 > 详细

UIAlertController custom font, size, color

时间:2015-01-26 19:00:50      阅读:1830      评论:0      收藏:0      [点我收藏+]

标签:

本文转载至 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];
    }
}
shareimprove this question
 

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"];
shareimprove this answer
 
    
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.

shareimprove this answer
 
    
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)
}
shareimprove this answer
 
    
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

标签:

原文地址:http://www.cnblogs.com/Camier-myNiuer/p/4250718.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!