2010-10-12 114 views
9

字体很小,很难阅读,所以我想让它更接近它下面的按钮上使用的字体大小。有没有办法在UIActionSheet中更改标题字体大小?

+0

有可能没有支持的方式来做到这一点。您可能会以某种方式获得对操作表视图的引用,然后遍历它以找到标题UILabel并直接编辑该视图,但如果操作表的操作系统实现发生更改,则未来可能会中断该视图。 – Nimrod 2010-10-13 00:04:08

回答

2

就像Nimrod说你不能用本地方法做到这一点。您可以检查UIActionSheet子视图,找到好的并更改它的属性。

  • 此实现可能会在 崩溃即将成为iOS的更新
  • 苹果可能会拒绝你的应用程序
  • UIActionSheet是 iPhone GUI的本土元素,用户用它熟悉

SO

你真的应该不改变使用的字体大小。如果这UIActionSheet标题是很重要的,你应该找到另一种方式展示给用户...

16

后,可以更改字体属性show方法(如showFromBarButtonItem)如下。

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

优秀的解决方案。之所以这么做是因为show后的方法是我们在show之前没有动作表框及其子视图 – Philip007 2013-02-10 18:31:14

+0

这种方法不再适用于iOS 7 – Keab42 2013-09-25 10:23:35

+0

很好的解答! – sac 2014-02-24 12:02:51

0

嗯,是有办法做到这一点 - 我创建了一个名为LTActionSheet

正如其他人所提到的,如果你使用它的各种可怕的事情可能发生github上一个UIActionSheet子类(我有没有运输代码...尚未:-))如果您在一个被接受的应用程序中使用它,请让我们都知道!

2

@ user651909的回答伟大工程,对我来说,虽然只是增加了几个细节:

我不得不initWithTitle:@" "(注意非空字符串)创建UIActionSheet时,这样的观点已经在上面的一些空间为任何文字开头。然后,在做了[popupQuery showInView:self.view];后,我添加了他的建议,以便旧的框架将被初始化。最后,我说:

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

这是必要的,因为oldFrame的身高对我的更大的字体(大小20)过小,导致一些字母即可在底部裁剪。此外,即使进行了这种调整,如果文本太大,比如说大于boldSystemFontOfSize:26,文本将运行得太近或进入下面的按钮。由于按钮似乎锚定在顶部,因此调整表单框架的大小并不会有帮助。

大概做

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

不违反苹果的不使用任何内部API的政策,对不对?

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

就像conecon所示,你可以得到一个指向它的指针并操作它。如果我理解正确,你要做的是:

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

希望这有助于!

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

UIActionSheet Class Reference

UIActionSheet的目的不是要被继承,也不应该你加意见,其层次结构。如果您需要提供比UIActionSheet API提供更多定制的工作表,您可以创建自己的模型并以presentViewController:animated:completion:的模式呈现。

相关问题