Change font type and size of UIActionSheet title string
Asked Answered
G

6

9

I have a UIActionSheet with title string "DO: These tasks". In the title string, the substring "DO:" should be Bold(with a particular font size) and the substring "These tasks" should be Regular. Is it possible? How can I do this?

Goosander answered 24/7, 2012 at 10:41 Comment(0)
S
30

I assume you have a class which implements the UIActionSheetDelegate protocol and your class is a delegate class of the current UIActionSheet, so in that class you can change the whole title of the UIActionSheet like

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
    for (UIView *_currentView in actionSheet.subviews) {
        if ([_currentView isKindOfClass:[UILabel class]]) {
            [((UILabel *)_currentView) setFont:[UIFont boldSystemFontOfSize:15.f]];
        }
    }
}

but as far as you see you have no chance to format the part of the UILabel object's text property.


you could add a new UILabel for your actionsheet now with a different font, if you want to see a text with the bolded DO: string... but from here the limit is you imagination only, you can format the UIActionSheet's elements in this method or inside the -didPresentActionSheet: method as well.

so this would be the idea for this.

UPDATE on 7 Oct 2013

in iOS7 we don't really have direct access to the subviews of an UIAlertView or an UIActionSheet object, so this solution might not be working properly on iOS7 environment.

if you have any issue with it on iOS7, please comment it to me! thank you.

UPDATE on 22 Jun 2014

I have not found any solution to do such thing directly with the new UIAlertController in iOS8, the title label looks to be not visible in the entire view-hierarchy of the UIAlertController, neither visible before it appears or even after.

NOTE: I've also figured out that it is not forbidden to overlap/cover its content, after it appeared on the screen. currently it is impossible to predict that it will work over Beta or whether or not it is AppStore-safe.

NOTE: please bear in your mind the view-lifecycle's schedule, and do not try to present any content in a view or view controler if that is not in the navigation stack already.

anyway, here is a Swift solution how I could reach the layers and I could add my custom view to it.

let alertController: UIAlertController = UIAlertController(title: "", message: "", preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alertController, animated: true, completion: {
    let aboveBlurLayer: UIView = alertController.view.subviews[0] as UIView
    let belowBlurLayer: UIView = (aboveBlurLayer.subviews[0].subviews as Array<AnyObject>)[0] as UIView
    let customView: UIView = UIView(frame: aboveBlurLayer.bounds)
    customView.backgroundColor = UIColor.redColor()
    aboveBlurLayer.addSubview(customView)
    })

NOTE: it might be useful to add custom content to an alert/actionsheet, but if it does not work in the future, I will update my answer.

Spiller answered 24/7, 2012 at 10:57 Comment(0)
B
10

For iOS 7 the following code will work.

Implement UIActionSheetDelegate as follows

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
    [actionSheet.subviews enumerateObjectsUsingBlock:^(id _currentView, NSUInteger idx, BOOL *stop) {
        if ([_currentView isKindOfClass:[UIButton class]]) {
            [((UIButton *)_currentView).titleLabel setFont:[UIFont boldSystemFontOfSize:15.f]];
            // OR
            //[((UIButton *)_currentView).titleLabel setFont:[UIFont fontWithName:@"Exo2-SemiBold" size:17]];
        }
    }];
}

For iOS 6

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
    for (UIView *_currentView in actionSheet.subviews) {
        if ([_currentView isKindOfClass:[UILabel class]]) {
            [(UILabel *)_currentView setFont:[UIFont boldSystemFontOfSize:15.f]];
            // OR
            //[(UILabel *)_currentView setFont:[UIFont fontWithName:@"Exo2-SemiBold" size:17]];
        }
    }
}
Buxom answered 7/1, 2014 at 10:50 Comment(10)
For some reason this won't change the cancel button's font :(Comptometer
Hey @Comptometer the above has been tested and is used in one of my app :) was working fine. If you find any other solution please to update.Buxom
@Buxom your solution works well on iOS 7 for me, thanks.Kelantan
This appears to ONLY adjust the cancel button. I have no idea why the other buttons don't change...Charmainecharmane
@ColinTremblay which version of iOS are you using?Buxom
@Buxom iOS 7. On iOS 8, the font sizes automatically adjust to fit, so I don't need to worry about it.Charmainecharmane
@ColinTremblay try printing _currentView kind of class and check if its giving you UILabelBuxom
iOS7. If there are multiple lines exceeding the height of the action sheet window, then a more complex view is created (table view) and the above code traversing and looking for labels does not work. It is weird.Cattle
@VladimírSlavík never encounter that kind of a issue before :)Buxom
@VladimírSlavík If you find the answer usefull please upvote it:)Buxom
G
6

Based on Holex's answer:

for both iOS7 and iOS6 the following has worked for me to change the title of UIActionSheet properties. Please note that I am actually adding a new subview, since for unknown? reason updating the current UILabel gives only vertically half of the text? Also "title" is apparantly the first UILabel, so we break the loop after one change.

- (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;
        }
    }
}

The above seems to work on iOS6 and iOS7 simulators. I am not sure if this would work with other future versions of iOS though.

Gaze answered 24/10, 2013 at 8:45 Comment(0)
A
4

Following may be useful.

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    [actionSheet.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)obj;
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }
    }];

}
Acevedo answered 19/12, 2013 at 7:7 Comment(0)
R
0

see these link .and use it. ohattributedlabel

http://www.cocoacontrols.com/platforms/ios/controls/ohattributedlabel

Revolving answered 24/7, 2012 at 10:58 Comment(0)
L
0

There is an internal property of UIActionSheet called _titleLabel so it is possible to get a reference and change the attributed string property of this label directly.

Please be aware, that this is a private API in might be rejected in the App store.

Here is how it works:

- (NSAttributedString *) actionSheetAttributedTitle;
{
    NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:@"Test"];
    UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:13.0f];
    [attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, attString.length)];
    [attString addAttribute:NSForegroundColorAttributeName
                  value:[UIColor blackColor]
                  range:NSMakeRange(0, attString.length)];
    return [attString copy];
}

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet;
{
    UILabel *sheetTitleLabel;
    if([actionSheet respondsToSelector:@selector(_titleLabel)]) {
        sheetTitleLabel = objc_msgSend(actionSheet, @selector(_titleLabel));
        sheetTitleLabel.attributedText = [self actionSheetAttributedTitle];

    }
}
Lobule answered 23/6, 2014 at 18:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.