Adding UItextField and UITextView to an UIAlertView - ios

I have this code for showing alert view with two needed extra objects:
- (void)leaveCommentButtonPressed
{
UIAlertView *leaveCommentAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Leave comment", nil)
message:#""
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Done", nil];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 33)];
[textField setBackgroundColor:[UIColor lightGrayColor]];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 33, 100, 67)];
[textView setBackgroundColor:[UIColor darkGrayColor]];
[view addSubview:textField];
[view addSubview:textView];
CGFloat system_version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (system_version < 7.0) //For Backward compatibility
{
[leaveCommentAlert addSubview:view];
}
else
{
[leaveCommentAlert setValue:view forKey:#"accessoryView"];
}
[leaveCommentAlert show];
}
But my problem is that I can't calculate width of alertView to set width for my text view and text field.
Maybe there are some other answers how to achieve text field and text view. But my idea is to have UIView with appropriate size.

Hic sunt dracones
Subclassing Notes
The UIAlertView class is intended to be used as-is and does not
support subclassing. The view hierarchy for this class is private and must not be modified.
You should use a alert view replacement. There are numerous in the web, for example: CXAlertView, DLAlertView or SDAlertView

If you're just looking to use a default implementation of UIAlertView, the Apple Docs also state:
Optionally, an alert can contain one or two text fields, one of which can be a secure text-input field. You add text fields to an alert after it is created by setting its alertViewStyle property to one of the styles specified by the UIAlertViewStyle constants. The alert view styles can specify no text field (the default style), one plain text field, one secure text field (which displays a bullet character as each character is typed), or two text fields (one plain and one secure) to accommodate a login identifier and password
See Alert Views
But, as mentioned in a different post, UIAlertView is deprecated and replaced by UIAlertController. Fortunately it comes with addTextFieldWithConfigurationHandler: that allows you to do what you're looking for.

Related

How can I embed UIViews (or, more specifically, UITextAreas) programmatically in-line with Text?

For reasons of reusability, I'm interested in finding a way to programmatically add text and custom UITextFields to a view in an efficient manner, similar to how you can combine strings using NSString's stringWithFormat: and then assign the result to a UILabel's text attribute. Ideally, with 2-3 statements I could write text and include my UITextField Objects in a string, and get an automatically text-wrapped, nicely formatted UIView that I can embed directly into my view. Basically, it would function like a UILabel with the ability to add UIView objects. For an example of the output this image would be a combination of both text and underlined UITextFields:
If this exists, it would allow me to reuse a single UITableViewCell subclass rather than having 5-6 xibs and 3-4 subclasses. I've searched about 2 hours with no real luck for a pre-existing solution, so has anyone ever encountered this problem before and used or released a library to handle this, or is there a simple solution I'm overlooking?
Thank you!
you can use CSLinearLayoutView (https://github.com/scalessec/CSLinearLayoutView)
and create a class
#implementation LabledView
+ (UIView*)create :(CGRect) frame view:(UIView*) view labelTitle:(NSString*)labelTitle viewLinearLayoutMakePadding :(CSLinearLayoutItemPadding)viewLinearLayoutMakePadding labelLinearLayoutMakePadding :(CSLinearLayoutItemPadding)labelLinearLayoutMakePadding font:(UIFont*)font textColor:(UIColor*)textColor
{
CSLinearLayoutView *container = [[CSLinearLayoutView alloc] initWithFrame:frame];
container.orientation = CSLinearLayoutViewOrientationHorizontal;
UILabel *label = [[UILabel alloc] init];
label.textColor = textColor;
[label setText:labelTitle];
[label setFont:font];
[label sizeToFit];
CSLinearLayoutItem *itemLabel = [CSLinearLayoutItem layoutItemForView:label];
itemLabel.padding = labelLinearLayoutMakePadding;
CSLinearLayoutItem *itemView = [CSLinearLayoutItem layoutItemForView:view];
itemView.padding = viewLinearLayoutMakePadding;
[container addItem:itemLabel];
[container addItem:itemView];
return container;
}
example :
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 260, 40)];
UIView *customView = [LabledView create:CGRectMake(0, 0, 280, 40) view:textField
labelTitle:#"your label" viewLinearLayoutMakePadding:CSLinearLayoutMakePadding(0, 10, 0, 0)
labelLinearLayoutMakePadding:CSLinearLayoutMakePadding(10, 0, 0, 0)
font:[UIFont fontWithName:#"Helvetica" size:12] textColor:[UIColor blackColor]];
You can underline specific ranges of a string with NSAtttibutedString. You can setAttributedString to UILabel in ios6... So that's the way I'd do it, then it can indeed be in a single label with the desired parts underlined (or in a different font/colour/etc) only. Be careful when you look into attributed string, it's attributes dictionary uses different keys for working with UIKit (these are the ones you need here) to what it uses with CoreText

Can i put multiple text boxes in an alert box in ios?

can I put multiple text input boxes on the alert box in iOS?
The best solution is to write or use a custom alert view that has the text fields that you need. If you only need 1 text field (or username/password) you can still use UIAlertView by setting the alertViewStyle property.
There are custom alert views already written, such as this one.
Yes, that's possible.
Just add UITextField as subviews to the UIAlertView.
Set the tag for each UITextField, in order to retrieve the entered text later.
UIAlertView* alert=[[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 50.0, 260.0, 25.0)];
textField1.tag=0;
textField1.borderStyle=UITextBorderStyleRoundedRect;
textField1.delegate=self;
[alert addSubview:textField1];
UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 85.0, 260.0, 25.0)];
textField2.tag=1;
textField2.delegate=self;
textField2.borderStyle=UITextBorderStyleRoundedRect;
[alert addSubview:textField2];
[alert show];
PLEASE NOTE that this is bad practice as it relies on UIAlertView being a subclass of UIView. As pointed out by MusiGenesis below, it won't work from iOS 7 onwards.
Hope that helps.
if you want 2 text boxes, you can use login style, but set second item secureTextEntry to NO, refer to
http://forums.macrumors.com/threads/uialertviews-with-textfields.1355954/

dim AND lock the background when using UIActionSheet on iPad

I have researched this question for a few hours, sounds pretty simple to me but haven't been able to find a viable solution. I have an iPad application where I'm using a UIActionSheet to confirm a delete. I'm adding a label to increase the font size. Everything looks and works great. I also have a requirement to dim and lock the background while the Action Sheet is visible. I can dim but cannot see how to lock the background so that the user must make a selection on the Action Sheet to dismiss it. I have tried setting UserInteractionEnabled but it doesn't work. Any Ideas?
// dim the background
UIView *dimViewDelete = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
dimViewDelete.backgroundColor = [UIColor blackColor];
dimViewDelete.alpha = 0.3f;
dimViewDelete.tag = 2222;
[self.view addSubview:dimViewDelete];
if ([self.listArray count] > 0)
{
// create Action Sheet
UIActionSheet * action = [[UIActionSheet alloc]
initWithTitle:#" "
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Delete"
otherButtonTitles:nil];
[action addButtonWithTitle:#"Cancel"];
[action setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[action showInView:self.view];
// change the font size of the title
CGRect oldFrame = [(UILabel*)[[action subviews] objectAtIndex:0] frame];
UILabel *addTitle = [[UILabel alloc] initWithFrame:oldFrame];
addTitle.font = [UIFont boldSystemFontOfSize:22];
addTitle.textAlignment = UITextAlignmentCenter;
addTitle.backgroundColor = [UIColor clearColor];
addTitle.textColor = [UIColor whiteColor];
addTitle.text = #"Are You Sure?";
[addTitle sizeToFit];
addTitle.frame = CGRectMake(oldFrame.origin.x, oldFrame.origin.y,
oldFrame.size.width, addTitle.frame.size.height);
[action addSubview:addTitle];
}
Your best option is to implement your own custom action sheet-like control.
You need a simple view controller that has the two buttons and a label (for the title). Show the view controller in a popover. Make the view controller modal so it can only be dismissed by tapping one of the buttons. This also makes the background appear locked.
If you really need to dim the background as well, just before displaying the popover, add a screen sized UIView to the main window. Set this view's background to [UIColor whiteColor:0 alpha:0.7]. Adjust the alpha as needed to get the right dimming effect. You can even animate the alpha of the view so it fades in and out as needed.

UIAlertView with 3 textfields and 3 labels overlap each other

I'm trying to create an UIAlertView with 3 TextFields and 3 Labels.
But with my current code they just go over each other, I've tried to change the frame of the AlertView and I've tried to use the CGAffineTransform but they both didn't work.
Here's my code:
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Sign in" message:#"\n\n\n\n\n" delegate:self cancelButtonTitle:#"Stop" otherButtonTitles:#"Sign in",nil];
UILabel *customerIdLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 55, 65, 21)];
customerIdLabel.tag =1;
customerIdLabel.text = #"Customer id: ";
[myAlertView customerIdLabel];
UITextField *customerIdField = [[UITextField alloc] initWithFrame:CGRectMake(89, 50, 160, 30)];
customerIdField.tag = 2;
customerIdField.placeholder = #"Fill in your customer id";
[myAlertView customerIdField];
And then there's two more combinations of those.
Here's the transform that I've tried:
CGAffineTransform myTransform = CGAffineTransformMakeScale(1.0, 0.5f);
[myAlertView setTransform:myTransform];
And this is what I get:
I'm pretty sure you aren't supposed to use UIAlertViews like that. You're supposed to set the title, message, etc and use the [alertview show]. The documentation seems to be pretty clear that it is an as-is class.The UIAlertViewStyle may be something you can look at, but the options are 1 text field, 1 secure field, or a username and password field.
Your best option is to use a new UIViewController and use the presentViewController:animated:completion: call.
I get around this by not changing UIAlertView but add a clear view on top of it when it is showing (you can pad with spaces) and add the textfields there at the correct location (note when rotation occurs). Then, let the tap go through the view to the UIAlertView. This has gone through Apple in 3 apps in the store.
Before the new SDK in xcode 5 you where able to add subviews to an alert view but that doesn't work anymore. I had to update an app because of that nasty code. Is better to use uipopovercontroller for ipad or show a UIView or even present a viewcontroller for either ipad or iphone.

How do I move down the buttons in UIAlertView?

I have three buttons and a textview in uialertview, and tried to move down the buttons with add "\n" in uialertview's message property. But it's not work. The string will become "..." when it reach the limit of a line. the textview always cover my buttons. Do you have any suggestions? Sorry that I don't have the right to post image.
- (void)addMessage
{
self.addMessageAlertView = [[UIAlertView alloc] initWithTitle:#"Add Title"
message:#"\n\n\n\n function normal function normal function normal function normal function normal "
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",#"Search", nil];
self.addMessageTextView = [[UITextView alloc] initWithFrame:CGRectMake(10.0, 75, 260.0, 25*2)];
[addMessageTextView setBackgroundColor:[UIColor whiteColor]];
addMessageTextView.font = [UIFont systemFontOfSize:20];
addMessageTextView.delegate=self;
addMessageTextView.layer.masksToBounds=YES;
addMessageTextView.layer.cornerRadius=10.0;
addMessageTextView.layer.borderWidth=0.0;
[addMessageAlertView addSubview:addMessageTextView];
[addMessageAlertView show];
[addMessageAlertView release];
[addMessageTextView release];
}
- (void)willPresentAlertView:(UIAlertView *)openURLAlert
{
[openURLAlert setFrame:CGRectMake( 10, 60, 300, 300 )];
[openURLAlert setBounds:CGRectMake(0, 10, 290, 290 )];
}
UIAlert view has maximum height (I've tried this same thing with no success); you won't be able to make it bigger. I suggest that you use a custom pop-over instead. A popover can act like an alert but will give you more flexibility.
This question has some links to tutorials: Are there examples of how to use UIPopoverController on iOS?

Resources