2011-01-21 12 views
0

私は現在、私のカスタムUIAlertViewにhttp://kwigbo.com/post/318396305/iphone-sdk-custom-uialertview-background-colorを使用しています。すべて正常に動作していますが、タイトルとメッセージのテキストの色を変更しようとしています。ボタンの色を変更したいと思います。これを行う方法はありますか?私は使用しようとしました: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しかし、ibody 5.1では、_bodyTextLabelの設定が私のためには機能しません。長いテキストがある可能性があります –

+1

これは悪い解決策であり、プライベート変数を(ラウンドアバウトの方法で) –

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に完全に取り組んでいます。何か問題があったらコメントしてください。

関連する問題