2010-10-12 103 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:的模式呈現。

相關問題