UITextView large text in UIAlertView - iphone

I have an UIAlertView with UITextView.
on ViewDidAppear I do [textView setText:] with a large text, but the alert shows an empty textView, and only after I touch the textView to scroll, the text appears.
What should I do in order to make the text appear in the textView in the alert, without scrolling it to "refresh" it?
Thanks!
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
av = [[UIAlertView alloc]initWithTitle:#"Terms of Service" message:#"\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:#"Disagree" otherButtonTitles:#"Agree",nil];
UITextView *myTextView = [[UITextView alloc] initWithFrame:CGRectMake(12, 50, 260, 142)];
[myTextView setTextAlignment:UITextAlignmentCenter];
[myTextView setEditable:NO];
myTextView.layer.borderWidth = 2.0f;
myTextView.layer.borderColor = [[UIColor darkGrayColor] CGColor];
myTextView.layer.cornerRadius = 13;
myTextView.clipsToBounds = YES ;
[myTextView setText:#"LONG LONG TEXT"];
[av addSubview:myTextView];
[myTextView release];
[av setTag:1];
[av show];
}

This is because you have initially set message as #"\n\n\n\n\n\n\n" for UIAlertView. Set your UITextView first and then set UIAlertView's message as textView's text.

Add \n characters in place of message in alert view . Then create a UILabel add add to alert view. Like this
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:#"%#\n\n\n", #"Title"] message:#"\n" delegate:self cancelButtonTitle:NEVER_DISPLAY_BUTTON_TEXT otherButtonTitles:nil];
[alert addButtonWithTitle:#"Text"];
[alert addButtonWithTitle:#"Text"];
[alert addButtonWithTitle:#"Text"];
[alert addButtonWithTitle:#"Text"];
[alert show];
newTitle = [[UILabel alloc] initWithFrame:CGRectMake(10,-55,252,230)];
newTitle.numberOfLines = 0;
newTitle.font = [UIFont systemFontOfSize:15];
newTitle.textAlignment = UITextAlignmentCenter;
newTitle.backgroundColor = [UIColor clearColor];
newTitle.textColor = [UIColor whiteColor];
newTitle.text = [NSString stringWithFormat:#"%#",self.message];
[alert addSubview:newTitle];
You might need to adjust size of Uilable to match in your alert view.

Try with this:
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"Terms of Service"
message:[NSString stringWithFormat:#"%# \n\n\n",myTextView.text]
delegate:self
cancelButtonTitle:#"Disagree"
otherButtonTitles:#"Agree",nil
];

Related

Adding TextView to UIAlertView

when I add textview on alertview, the scollview is shown on top and behind of it the text is drawn,how to overcome this problem. Here is the code
question updated:
If the set textView.editable = NO; it work's fine but in my case i need to type the text in textview.
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(12, 50, 260, 50)];
[textView setText:#"lashdasjh asdasjdas asdlajsdl adsjajadsd aslj daj sdjasdjasjdlasjd as dlasj d"];
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"Type Your Message" message:#"\n\n\n\n\n\n" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Send",nil];
av.alertViewStyle = UIAlertViewStyleDefault;
[av addSubview:textView];
[av show];
Resolved mySelf. Remove the #"\n\n\n\n\n\n" in message and add #"\n\n\n\n\n\n" in title
This doesn't work anymore on IOS7, but using https://github.com/wimagguc/ios-custom-alertview will help you get it done pretty well
replace your code with this code its work fine.
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(12, 50, 260, 100)];
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"Type Your Message" message:#"\n\n\n\n\n" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Send",nil];
av.alertViewStyle = UIAlertViewStyleDefault;
[av addSubview:textView];
[av show];

How to put uitextfield for user id and password in UIAlertView?

I am creating an application where i need to create two UITextField or TextBox for User id and password inside a UIAlertView for User login.I have tried something but here the textfield created becomes very big and almost cover the whole alert view.Please somebody help me how to organize the textfield and labels etc inside the alert view. I need like - there should be a small textfield against the label "User Id" and "Password". So how to do that? please help me. Thanx in advance.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"User Login" message:#"Please Enter the Following \n\n\n\n\n\n\n" delegate:self cancelButtonTitle:#"Close" otherButtonTitles:#"Sign In", nil];
UILabel *lblUserName = [[UILabel alloc] initWithFrame:CGRectMake(20, 70, 85, 21)];
lblUserName.tag =2;
//lblUserName.backgroundColor = [UIColor darkGrayColor];
lblUserName.font = [UIFont systemFontOfSize:15.0];
lblUserName.text = #"User Name:";
lblUserName.textColor=[UIColor whiteColor];
//[lblUserName setFont:[UIFont fontWithName:#"TimesNewRomanPS-boldMT" size:8]];
lblUserName.backgroundColor = [UIColor clearColor];
[alert addSubview:lblUserName];
UITextField *txtUserName;
//if(txtUserName==nil)
txtUserName = [[UITextField alloc] initWithFrame:CGRectMake(89, 50, 160, 30)];
txtUserName.tag = 3;
txtUserName.borderStyle = UITextBorderStyleBezel;
txtUserName.textColor = [UIColor whiteColor];
txtUserName.font = [UIFont systemFontOfSize:12.0];
txtUserName.placeholder = #"User ID";
[alert addSubview:txtUserName];
alert.tag=3;
[alert show];
[alert release];
As of iOS5, UIAlertView supports a login screen using the Alert Style UIAlertViewStyleLoginAndPasswordInput.
More information in the UIAlertView Class Reference
UIAlertView *alert =[UIAlertView alloc] init];
alert.delegate =self //make sure to add the UIAertViewDelegate in the .h file
alert.alertViewStyle =UIAlertViewStyleLoginAndPasswordInput;
alert.message =#"Please login";
[alert show];
[alert release];
Then in the clickedButtonAtIndex delegate method:
{
UITextField *username = [alertView textFieldAtIndex:0];
UITextfield *password = [alertView textFieldAtIndex:1];
NSLog(#"Username %"# password %"#,username.text, password.text);
}

How do you format more than two buttons inside an UIAlertView with the UIAlertStylePlainTextInput style?

I've added a UIAlertView in my application that grabs user input but I'm unsure as to how I can add a third button. Ideally the three buttons would be across the alert horizontally or two would be above the "cancel" button. The code snippet below is what I'm using to add the UIAlertView.
- (IBAction)initiateSave{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Archive"
message:#"Enter a name to save this as:"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Save Session",#"Save",nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeDefault;
alertTextField.placeholder = #"eg. My awesome file...";
alert.tag = 1;
[alert show];
[alert release];
self.name = [[alert textFieldAtIndex:0]text];
}
Apple really doesn't want you messing with UIAlertView. If the way it naturally formats itself doesn't meet your needs, consider putting up a custom presented ("modal") view instead.
This can definitely be achieved, first of all you'll need to set up something like this.
// Create Alert
UIAlertView* av = [UIAlertView new];
av.title = #"Find";
// Add Buttons
[av addButtonWithTitle:#"Cancel"];
[av addButtonWithTitle:#"Find & Bring"];
[av addButtonWithTitle:#"Find & Go"];
[av addButtonWithTitle:#"Go to Next"];
// Make Space for Text View
av.message = #"\n";
// Have Alert View create its view heirarchy, set its frame and begin bounce animation
[av show];
// Adjust the frame
CGRect frame = av.frame;
frame.origin.y -= 100.0f;
av.frame = frame;
// Add Text Field
UITextField* text = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
text.borderStyle = UITextBorderStyleRoundedRect;
[av addSubview:text];
[text becomeFirstResponder];
QUOTED FROM...
https://stackoverflow.com/a/412618/716216
Then you'll want to animate moving the UIAlertView up when the keyboard is called up... something like this...
-(void)keyboardWillShow: (id) notification {
if(showAlert==YES)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[createNewAlert setTransform:CGAffineTransformMakeTranslation(0,-60)];
[createNewAlert show];
[UIView commitAnimations];
}
}
-(void)keyboardWillHide: (id) notification {
if(showAlert==YES)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[createNewAlert setTransform:CGAffineTransformMakeTranslation(0,+60)];
[UIView commitAnimations];
}
}
QUOTED FROM... https://stackoverflow.com/a/3844956/716216
Of course you can find additional info on custom UIAlertViews in the following Apple sample code!
https://developer.apple.com/library/ios/#samplecode/UICatalog/Introduction/Intro.html
Good Luck!!!!
It's works fine to me. After showing lot of thread finally i found solution
UIAlertView* getWebUrl = [[UIAlertView alloc] initWithTitle:#"Enter URL"
message:nil
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Save",
#"Save Url", nil];
getWebUrl.transform=CGAffineTransformMakeScale(1.0, 0.75);
getWebUrl.alertViewStyle=UIAlertViewStylePlainTextInput;
[getWebUrl show];
and align buttons and textfield after present alertview
-(void)willPresentAlertView:(UIAlertView *)alertView {
for (UIView *view in alertView.subviews) {
if ([view isKindOfClass:[UITextField class]]||
[view isKindOfClass:[UIButton class]] || view.frame.size.height==31) {
CGRect rect=view.frame;
rect.origin.y += 65;
view.frame = rect;
}
}
}
I don't think you can manually resize a UIAlertView, but a trick I use for including a UIActivityIndicatorView is to use "/n" in the message string to make the "message" larger (one extra line per each "/n"), therefore making enough space for everything else.
This is somewhat tricky, Here is the solution for this.You need to use \n in the message string to increase the height of the UIAlertView.
- (IBAction)initiateSave{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Archive"
message:#"\n\n\n\n\n" // Trick to increase the height
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Save Session",#"Save",nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
After increasing the height of Alert, now you can add a label to set the message in the label : "Enter a name to save this as:" at appropriate position.
[alert addSubView: messageLbl];
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeDefault;
alertTextField.placeholder = #"eg. My awesome file...";
alert.tag = 1;
[alert show];
[alert release];
self.name = [[alert textFieldAtIndex:0]text];
}
Here, a bit late but should do the trick. (Work for me).
Create UIAlertView
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"TITLE"
message:#"BODY"
delegate:self
cancelButtonTitle:#"CANCEL"
otherButtonTitles:#"OK",#"SEARCH",nil];
alert.tag = kAlertTag;
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *alertTextField = [alert textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeDefault;
alertTextField.placeholder = #"ENTER STH";
[alert show];
[alert release];
And implement delegate method.
-(void)willPresentAlertView:(UIAlertView *)alertView {
if (alertView.tag == kAlertTag) {
[alertView setFrame:CGRectMake(17, 30, 286, 188)];
NSArray *subviewArray = [alertView subviews];
UILabel *messageLB = (UILabel *)[subviewArray objectAtIndex:2];
[messageLB setFrame:CGRectMake(10, 46, 260, 20)];
UIButton *cancelBT = (UIButton *)[subviewArray objectAtIndex:3];
[cancelBT setFrame:CGRectMake(10, 130, 100, 42)];
UIButton *okBT = (UIButton *)[subviewArray objectAtIndex:4];
[okBT setFrame:CGRectMake(194, 130, 80, 42)];
UIButton *searchBT = (UIButton *)[subviewArray objectAtIndex:5];
[searchBT setFrame:CGRectMake(112, 130, 80, 42)];
UITextField *plateTF = (UITextField *)[subviewArray objectAtIndex:6];
[plateTF setFrame:CGRectMake(10, 80, 266, 50)];
UITextField *placeTF = (UITextField *)[subviewArray objectAtIndex:7];
[placeTF setFrame:CGRectMake(15, 70, 256, 50)];
}
}
FYI [subviewArray objectAtIndex:1] is for title of alertview.

No Keyboard with UIAlertViewStylePlainTextInput ?

I have a problem with the UIAlertViewStylePlainTextInput. I get my Alert with the textfield but there is no Keyboard coming up..? Here is my code:
UIAlertView *enter = [[UIAlertView alloc] initWithTitle:#"Highscores" message:#"Please Enter Your Name" delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:#"Abbrechen" nil];
enter.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *theTextField = [enter textFieldAtIndex:0];
theTextField.placeholder = #"Your Name";
theTextField.keyboardAppearance = UIKeyboardAppearanceAlert;
theTextField.selected = NO;
[enter show];
I have tried without the
UITextField *theTextField = [enter textFieldAtIndex:0];
theTextField.placeholder = #"Your Name";
theTextField.keyboardAppearance = UIKeyboardAppearanceAlert;
theTextField.selected = NO;
part but still there is no keyboard coming up.
(I've tested it on my device and on the simulator)
Do you have any ideas about that?
thank you in advance.
I had the problem because I was triggering the alert when a UITextField delegate method textFieldDidBeginEditing was called in my view and then I would fill this with the content of the Alert view text field. That meant that the UITextField in my view became first responder and I couldn't find a way to resign. So if you have the problem it is because something else is becoming first responder before the UIAlert is displayed. Think about how you trigger the alert.
I resigned to using a tap gesture on my UITextField which then triggers the UIAlertView with UIAlertViewStylePlainTextInput and it works fine.
Some code below:
-(void)addTextFieldToView{
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 31)];
textField.backgroundColor = [UIColor whiteColor];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.textAlignment = UITextAlignmentCenter;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showAlertWithTextField:)];
[textField addGestureRecognizer:tapGestureRecognizer];
[self.view addSubview:textField];
}
-(void)showAlertWithTextField:(UITapGestureRecognizer *)gesture{
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK",nil];
[dialog setAlertViewStyle:UIAlertViewStylePlainTextInput];
[dialog show];
}
Try below code:
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Your name?"
message:nil
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[message setAlertViewStyle:UIAlertViewStylePlainTextInput];
[message show];
Works on iOS5
Might fix your problem as it solved mine:
[enter resignFirstResponder];

how to call a function as user click on search in alert view iphone

//---------Searchoing----- //
- (IBAction) Search_hadit
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Search"
message:#" "
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Search",
nil];
CGRect frame = CGRectMake(14, 35, 255, 23);
myTextField = [[UITextField alloc] initWithFrame:frame];
myTextField.borderStyle = UITextBorderStyleBezel;
myTextField.textColor = [UIColor blackColor];
myTextField.textAlignment = UITextAlignmentCenter;
myTextField.font = [UIFont systemFontOfSize:14.0];
myTextField.placeholder = #"Enter Query";
myTextField.backgroundColor = [UIColor whiteColor];
myTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
myTextField.keyboardType = UIKeyboardTypeDefault;// use the default type input method (entire keyboard)
myTextField.returnKeyType = UIReturnKeyDone;
myTextField.delegate = self;
myTextField.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
[alert addSubview:myTextField];
[alert show];
[alert release];
}
When I click search button in my app alert view show...
I want when user click search button I get the text which he enter and send this data to another function on as he click search button where I can perform search. Any idea?
I guess (acording to the title) you'll need an UIAlertViewDelegate or at least the function:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString* buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle equalToString:#"Search"]) {
[do something];
}
}
But the text in your description describes a different problem than your title. I can't provide a solution to that, though.