2011-01-21 30 views
0

我目前使用http://kwigbo.com/post/318396305/iphone-sdk-custom-uialertview-background-color作爲我的自定義UIAlertView。一切工作正常,但我試圖改變標題和消息文本顏色,我想改變按鈕的顏色。有沒有辦法做到這一點?我已經嘗試使用:如何更改UIAlertView文字顏色?

UILabel *theTitle = [AlertView valueForKey:@"_titleLabel"]; 
    [theTitle setTextColor:[UIColor redColor]]; 

,但我無法得到它的工作。任何幫助,將不勝感激。謝謝。

回答

0

檢查setTextColor是否被刪除。

我認爲這是

theTitle.text.color = [UIColor redColor]; 

或者你可以使用

[theTitle.text setColor:[UIColor redColor]]; 

不能完全確定的第二個,第一個例子中,雖然應該做的伎倆。

1

您只需在您的.h文件中包含UIAlertViewDelegate並在您的.m文件中覆蓋此方法。

-(void)willPresentAlertView:(UIAlertView *)alertView{ 
    UILabel *theTitle = [alertView valueForKey:@"_titleLabel"]; 
    theTitle.font = [UIFont fontWithName:@"Copperplate" size:18]; 
    [theTitle setTextColor:[UIColor whiteColor]]; 

    UILabel *theBody = [alertView valueForKey:@"_bodyTextLabel"]; 
    theBody.font = [UIFont fontWithName:@"Copperplate" size:15]; 
    [theBody setTextColor:[UIColor whiteColor]]; 
} 
+1

+1,但在ios 5.1中,_bodyTextLabel的設置對我不起作用,可能是因爲我有更長的文本 – 2012-09-05 07:23:43

0

默認功能,你不能改變警報視圖標題和消息,但與附屬視圖的幫助下,就可以實現這一功能。 嘗試以下方法:

-(void)customAlertTitle:(NSString*)title message:(NSString*)message{ 
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil]; 
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 80)]; 

UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 270, 50)]; 
titleLabel.text = title; 
titleLabel.font = [UIFont boldSystemFontOfSize:20]; 
titleLabel.numberOfLines = 2; 
titleLabel.textColor = [UIColor redColor]; 
titleLabel.textAlignment = NSTextAlignmentCenter; 

[subView addSubview:titleLabel]; 

UILabel *messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, 270, 50)]; 
messageLabel.text = message; 
messageLabel.font = [UIFont systemFontOfSize:18]; 
messageLabel.numberOfLines = 2; 
messageLabel.textColor = [UIColor redColor]; 
messageLabel.textAlignment = NSTextAlignmentCenter; 

[subView addSubview:messageLabel]; 

[alertView setValue:subView forKey:@"accessoryView"]; 
[alertView show]; 
} 

上面的代碼可以正常使用上最新的XCode 8.3.1。如果遇到任何問題,請發表評論。