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

試著忘記壹切 提交于 2019-12-22 04:08:25

问题


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.


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



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




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




回答4:


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




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



回答6:


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!




回答7:


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 !




回答8:


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


来源:https://stackoverflow.com/questions/3919875/is-there-any-way-to-change-the-title-font-size-in-a-uiactionsheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!