Is there any way to change the title font size in a UIActionSheet?
Asked Answered
Z

8

9

The font is very small and hard for some to read so I'd like to make it closer to the font size used on the buttons below it.

Zwick answered 12/10, 2010 at 23:47 Comment(1)
There's probably no supported way to do this. You could probably get a reference to the actionsheet view somehow, then walk through it to find the title UILabel and edit that view directly, but that might break in the future if the OS implementation of the actionsheet changes.Topee
N
16

You can change font property after show method(like showFromBarButtonItem) as below.

CGRect oldFrame = [(UILabel*)[[sheet subviews] objectAtIndex:0] frame];
UILabel *newTitle = [[[UILabel alloc] initWithFrame:oldFrame] autorelease];
newTitle.font = [UIFont boldSystemFontOfSize:17];
newTitle.textAlignment = UITextAlignmentCenter;
newTitle.backgroundColor = [UIColor clearColor];
newTitle.textColor = [UIColor whiteColor];
newTitle.text = @"My Title";
[sheet addSubview:newTitle];

[sheet release];
Ne answered 6/6, 2011 at 13:40 Comment(2)
Excellent solution. And the reason of doing so AFTER show method is we don't have frame of action sheet and its subviews before showSingband
Great answer with fine explanation !!Selector
E
2

Like Nimrod said you can't do this with a native method. You could check the UIActionSheet subviews, find the good one and change its properties.

BUT

  • This implementation might crash in a futur iOS update
  • Apple may reject your app
  • UIActionSheet is a native element of iPhone GUI and users are familiar with it

SO

You really should don't change the font size used.. If this UIActionSheet title is important you should find another way to show it to the user...

Easterner answered 13/10, 2010 at 9:2 Comment(0)
G
2

@user651909's answer works great for me, although just to add a few more details:

I had to initWithTitle:@" " (note the non-empty string) when creating the UIActionSheet so that the view had some space on top for any text to begin with. Then, after doing a [popupQuery showInView:self.view];, I added his suggestion so that the oldFrame would be initialized. Finally, I added:

[newTitle sizeToFit];
newTitle.frame = CGRectMake(oldFrame.origin.x, 
                            oldFrame.origin.y, 
                            oldFrame.size.width, 
                            newTitle.frame.size.height);

This was necessary since the oldFrame's height was too small for my larger font (size 20), causing some letters to get clipped on the bottom. Also even with this tweak, if you make the text too big, say greater than boldSystemFontOfSize:26, the text will run too close or into the buttons below. Resizing the sheet's frame doesn't help since the buttons appear to be anchored to the top.

Presumably doing

CGRect oldFrame = [(UILabel*)[[sheet subviews] objectAtIndex:0] frame]; 

doesn't violate Apple's policy of not using any internal API, right?

Gladiatorial answered 18/11, 2011 at 2:0 Comment(0)
E
2

From the UIActionSheet Class Reference:

UIActionSheet is not designed to be subclassed, nor should you add views to its hierarchy. If you need to present a sheet with more customization than provided by the UIActionSheet API, you can create your own and present it modally with presentViewController:animated:completion:.

Equanimous answered 27/2, 2014 at 1:42 Comment(0)
D
1
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Share" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Facebook", @"Twitter",nil];
[sheet showInView:self.view];
if (![[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
     CGRect labelFrame = [(UILabel*)[[sheet subviews] objectAtIndex:0] frame];
     [[[sheet subviews] objectAtIndex:0] removeFromSuperview];
     UILabel *newTitle = [[UILabel alloc] initWithFrame:labelFrame];
     newTitle.font = [UIFont boldSystemFontOfSize:17];
     newTitle.textAlignment = UITextAlignmentCenter;
     newTitle.backgroundColor = [UIColor clearColor];
     newTitle.textColor = [UIColor whiteColor];
     newTitle.text = @"Share";
     [sheet addSubview:newTitle];
 }
Deluna answered 10/9, 2013 at 7:23 Comment(0)
D
0

Well, there is a way to do it - I created a UIActionSheet subclass on github named LTActionSheet

As others mentioned, all kinds of dire things may happen if you use it (I have not in shipping code ... yet :-) ) If you use it in an app that gets accepted, please let us all know!

Disincline answered 4/8, 2011 at 15:31 Comment(0)
O
0
UIActionSheet *as = [[UIActionSheet alloc] initWithTitle:@"SpecifySearch" 
                delegate:self 
       cancelButtonTitle:@"Cancel" 
  destructiveButtonTitle:nil 
       otherButtonTitles:@"UncategorizedContacts" , nil];

//as.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[as showFromTabBar:self.tabBarController.tabBar];

if( as.subviews.count > 0 
    && [[as.subviews objectAtIndex:0] isKindOfClass:[UILabel class]] ){
    UILabel* l = (UILabel*) [as.subviews objectAtIndex:0];
    [l setFont:[UIFont boldSystemFontOfSize:20]];
}

[as release];
Ovariectomy answered 10/6, 2012 at 8:44 Comment(0)
L
0

Just as conecon has showed you can get a pointer to it and manipulate it. If I understand correctly what you are trying to do is:

[sheet showInView:[self.view window]];
UILabel *title = [[sheet subviews] objectAtIndex:0];
title.font = [UIFont boldSystemFontOfSize:20];
title.textAlignment = UITextAlignmentCenter;

Hope this helps !

Laughable answered 23/8, 2012 at 15:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.