开发者

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

开发者 https://www.devze.com 2023-01-20 10:52 出处:网络
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.You can change font property after show method(like showFro

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.


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];


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...


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:.


@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?


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


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!


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 !


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];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号