How to change UIAlertView text color? - iphone

I'm currently using http://kwigbo.com/post/318396305/iphone-sdk-custom-uialertview-background-color for my custom UIAlertView. Everything is working fine but I'm trying to change the title and message text colors and I would like to change the button color. Is there any way to do this? I've tried using:
UILabel *theTitle = [AlertView valueForKey:#"_titleLabel"];
[theTitle setTextColor:[UIColor redColor]];
but I can't get it to work. Any help would be appreciated. Thanks.

You just need to include UIAlertViewDelegate in your .h file and override this method in your .m file.
-(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]];
}

Check to see if setTextColor is depricated.
I thought it was
theTitle.text.color = [UIColor redColor];
or you could use
[theTitle.text setColor:[UIColor redColor]];
Not totally sure on the second one, the first example should do the trick though.

By default functionality, you could not change alert view title and message but with the help of accessory view, you can achieve this functionality.
Try below method :
-(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];
}
The above code is working perfectly on latest XCode 8.3.1. Kindly comment if facing any problem.

Related

Getting content from custom UITextfields in an UIAlertview

Got the following code:
-(void)addGrade {
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:#"Text" message:#"\n \n \n" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Add", nil];
// Textfield1
UITextField *utextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 50.0, 260.0, 25.0)];
[utextfield becomeFirstResponder];
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
utextfield.leftView = paddingView;
utextfield.leftViewMode = UITextFieldViewModeAlways;
utextfield.placeholder = #"Subject";
[utextfield setBackgroundColor:[UIColor whiteColor]];
utextfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[alertview addSubview:utextfield];
// Textfield2
UITextField *ptextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 85.0, 90.0, 25.0)];
ptextfield.placeholder = #"Another Subject";
ptextfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
ptextfield.textAlignment = NSTextAlignmentCenter;
[ptextfield setBackgroundColor:[UIColor whiteColor]];
[alertview addSubview:ptextfield];
// Textfield3
UITextField *ctextfield = [[UITextField alloc] initWithFrame:CGRectMake(161.0, 85.0, 27.0, 25.0)];
ctextfield.placeholder = #"3";
[ctextfield setBackgroundColor:[UIColor whiteColor]];
ctextfield.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
ctextfield.textAlignment = NSTextAlignmentCenter;
[alertview addSubview:ctextfield];
// Label1
UILabel *telt = [[UILabel alloc] initWithFrame:CGRectMake(121.0, 85.0, 40.0, 25.0)];
telt.text = #"Text";
telt.textColor = [UIColor whiteColor];
telt.backgroundColor = [UIColor clearColor];
[alertview addSubview:telt];
// Label2
UILabel *keermee = [[UILabel alloc] initWithFrame:CGRectMake(199.0, 85.0, 100.0, 25.0)];
keermee.text = #"more text";
keermee.textColor = [UIColor whiteColor];
keermee.backgroundColor = [UIColor clearColor];
[alertview addSubview:keermee];
[alertview show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if ([title isEqualToString:#"Add"])
{
UITextField *field = (UITextField *)[[alertView subviews] lastObject];
NSLog (#"%#", field.text);
}
}
This code makes 3 Textfields and 2 labels in an UIAlertView.
With UITextField *field = (UITextField *)[[alertView subviews] lastObject]; I got back "more text" which is true because you are asking about the last object.
Now is my question how to get the content of all the UITextfield not only the label?
for (UIView *view in [alertView subviews]){
if ([view isMemberOfClass:[UITextField class]]){
UITextField *textField = (UITextField *)view;
NSLog(#"%#", textField.text);
}
}
Assign tags and retrieve them with those.
myTextField.tag = 100;
Then to find it use this function:
myTextField = [view subviewWithTag: 100];

Adding a UILabel onto a UINavigationController

I want to draw a long string of text underneath the Title on a UINaviagtionController.
I tried adding a UILabel to by doing :
[appDelegate.NavControll.view addSubview:testLabel];
And the Label appears but doesn't animate/move with the navbar and just seems independent of it.
Is there a way to achieve this or should I leave it alone and come up with another idea for displaying this string somewhere else.
Many Thanks,
Code
Looks a little bit ugly, but it works
You can easily customize appearance by yourself:
self.navigationItem.title = #"Needed title"; // for back button on pushed view
UIView *customTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = #"Title";
titleLabel.font = [UIFont fontWithName:#"Helvetica" size:18];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.center = CGPointMake(30, 10);
[titleLabel sizeToFit];
[customTitleView addSubview:titleLabel];
[titleLabel release];
UILabel *detailsLabel = [[UILabel alloc] init];
detailsLabel.text = #"details";
detailsLabel.font = [UIFont fontWithName:#"Helvetica" size:9];
detailsLabel.backgroundColor = [UIColor clearColor];
[detailsLabel sizeToFit];
detailsLabel.center = CGPointMake(50, 35);
[customTitleView addSubview:detailsLabel];
[detailsLabel release];
self.navigationItem.titleView = customTitleView;
[customTitleView release];
you can add a label in your title bar and then on that label you can show your title and your long string both.
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[titleLabel setBackgroundColor:[UIColor clearColor]];
// here's where you can customize the font size
[titleLabel setFont:[UIFont boldSystemFontOfSize:16.0]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setText:#"hello\nhii"];
[titleLabel setNumberOfLines:2];
[titleLabel setLineBreakMode:UILineBreakModeTailTruncation];
[titleLabel sizeToFit];
[titleLabel setCenter:[self.navigationItem.titleView center]];
[self.navigationItem setTitleView:titleLabel];
[titleLabel release];

UINavigationBar title

I am trying to use a UILabel to replace the title at the UINavigationBar, the code is as follows:
UINavigationBar *bar = [self.navigationController navigationBar];
[bar setBackgroundColor:[UIColor blackColor]];
UILabel * nav_title = [[UILabel alloc] initWithFrame:CGRectMake(80, 2, 220, 25)];
nav_title.font = [UIFont fontWithName:#"Arial-BoldMT" size:18];
nav_title.textColor = [UIColor whiteColor];
nav_title.adjustsFontSizeToFitWidth = YES;
nav_title.text = title;
nav_title.backgroundColor = [UIColor clearColor];
[bar addSubview:nav_title];
[nav_title release];
The problem is that, how do I remove the original title of the bar? I didn't declare any self.title = #"title", but it always shows it there:
If I do self.title = nil, then everything is gone... How do eliminate this mysterious title from the navbar and just use the UILabel I created.
Why don't you just do self.title = #""?
EDIT: Try this?
UINavigationBar *bar = [self.navigationController navigationBar];
[bar setBackgroundColor:[UIColor blackColor]];
UILabel * nav_title = [[UILabel alloc] initWithFrame:CGRectMake(80, 2, 220, 25)];
nav_title.font = [UIFont fontWithName:#"Arial-BoldMT" size:18];
nav_title.textColor = [UIColor whiteColor];
nav_title.adjustsFontSizeToFitWidth = YES;
nav_title.text = title;
self.title = #"";
nav_title.backgroundColor = [UIColor clearColor];
[bar addSubview:nav_title];
[nav_title release];
Use self.navigationItem.titleView = nav_title; instead of adding your label as a subview.
Use this:
UILabel *label = [[UILabel alloc]init];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setText:self.title];
label.adjustsFontSizeToFitWidth=YES;
label.lineBreakMode=UILineBreakModeWordWrap;
label.numberOfLines=0;
[label setFont:[UIFont boldSystemFontOfSize:16.0]];
[self.navigationController.navigationBar.topItem setTitleView:label];
[label release];
Hope this will help u..!

Comparing two strings in objective-c causes app to quit

I am attempting to compare two strings using "isEqualToString" and i cant seem to get it to work. Once it begins to run the if statement it quits the app. I'm quite sure that the strings are in fact equal to each other and despite my best attempts I'm out of ideas. Any help would be appreciated.
-(void)createTextfields{
name = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 140, 25)];
name.borderStyle = UITextBorderStyleRoundedRect;
name.textColor = [UIColor blackColor];
name.placeholder = #"Password";
name.textAlignment = UITextAlignmentCenter;
[self addSubview:name];
entry = [[UITextField alloc] initWithFrame:CGRectMake(170, 0, 140, 25)];
entry.borderStyle = UITextBorderStyleLine;
entry.textColor = [UIColor blueColor];
entry.textAlignment = UITextAlignmentCenter;
[self addSubview:entry];
}
-(void)createSubmitButton{
UIButton *submit = [UIButton buttonWithType:UIButtonTypeRoundedRect];
submit.titleLabel.textAlignment = UITextAlignmentCenter;
[submit setTitle:#"Submit" forState:UIControlStateNormal];
submit.frame = CGRectMake(90, 50, 60, 30);
[submit addTarget:self action:#selector(runSubmit) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:submit];
UIButton *savePass = [UIButton buttonWithType:UIButtonTypeRoundedRect];
savePass.titleLabel.textAlignment = UITextAlignmentCenter;
[savePass setTitle:#"Save" forState:UIControlStateNormal];
savePass.frame = CGRectMake(20, 50, 60, 30);
[savePass addTarget:self action:#selector(save) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:savePass];
}
-(void)save{
tempPass = name.text;
name.text = #"";
entry.text = tempPass;
}
-(void)runSubmit{
// password = name.text;
if ([tempPass isEqualToString:name.text]) {
//[viewController displayAlertFromViewControl];
}else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Correct" message:#"Alert" delegate: self cancelButtonTitle:#"Close" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
Probably one of your strings (pretty sure that it is tempPass) is autoreleased
Where is tempPass being declared? Is it possible that it isn't being retained long enough to be referenced in the if?
If you run your program with this menu item: Run -> Run with Performance Tool -> Zombies
then Instruments will do all the hard work of tracking down the problem for you. It will show you which object was released early and where it was allocated.
It is probably tempPass. Once you determine if that is the case, it would be best to make tempPass a property with a copy attribute and then use self.tempPass=name.text instead of just tempPass=name.text.

actionSheet in iphone application

I am new to iphone.Is it possible to keep 3 textfields, 3 labels and 2 uibuttons in an actionsheet or alertview. Kindly give information.
Thanks in advance.
You can try this as:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"\n\n\n\n\n\n\n\n\n\n" message:#"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
UITextField *textfield1 = [[UITextField alloc] init];
textfield1.frame = CGRectMake(10.0, 30.0,260.0, 15.0);//set your frame as you required
textfield1.text=#"";
[textfield1 setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:textfield1];
[textfield2 release];
UITextField *textfield2 = [[UITextField alloc] init];
textfield2.frame = CGRectMake(10.0, 48.0,260.0, 15.0);//set your frame as you required
textfield2.text=#"";
[textfield2 setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:textfield2];
[textfield2 release];
UITextField *textfield3 = [[UITextField alloc] init];
textfield3.frame = CGRectMake(10.0, 65.0,260.0, 155.0);//set your frame as you required
textfield3.text=#"";
[textfield3 setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:textfield3];
[textfield3 release];
UIView *tempView = [[UIView alloc]initWithFrame:CGRectMake(10.0, 80.0, 260.0, 100.0)];//set your frame as you required
tempView.backgroundColor = [UIColor clearColor];
[alert addSubview:tempView];
UILabel *label1 = [[UILabel alloc]init];
label1.frame = CGRectMake(0, 5, 50, 15);//set your label frame here
label1.text = #"Enter text";
[tempView addSubView:label1];
[label1 release];
UILabel *label2 = [[UILabel alloc]init];
label2.frame = CGRectMake(0, 25, 50, 15);//set your label frame here
label2.text = #"Enter text";
[tempView addSubView:label2];
[label2 release];
UILabel *label3 = [[UILabel alloc]init];
label3.frame = CGRectMake(0, 45, 50, 15);//set your label frame here
label3.text = #"Enter text";
[tempView addSubView:label3];
[label3 release];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = //set your button frame here
[tempView addSubview:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = //set your button frame here
[tempView addSubview:button2];
[tempView release];
[alert show];
[alert release];