Is there any way to change the title font size in a UIActionSheet?
Solution 1
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];
Solution 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?
Solution 3
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:
.
Solution 4
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...
Solution 5
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];
}
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on July 23, 2022Comments
-
Admin almost 2 years
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.
-
Philip007 over 11 yearsExcellent solution. And the reason of doing so AFTER
show
method is we don't have frame of action sheet and its subviews beforeshow
-
sac over 10 yearsGreat answer with fine explanation !!