Does anyone know how to change the font size of a UITextField within a UIAlertView? The following is my code...
- (void) editTitle
{
NSString *string = kLocalizedString(#"Edit Title");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:string
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *textField = [alert textFieldAtIndex:0];
if (!self.title) {
textField.text = nil;
}
else {
textField.text = self.title;
}
textField.clearsOnBeginEditing = NO;
textField.clearButtonMode = UITextFieldViewModeAlways;
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
textField.clearsContextBeforeDrawing = NO;
// These statements have no effect on the size of the text field's font
textField.font = [UIFont systemFontOfSize:16.0];
NSDictionary *attributes = #{
NSFontAttributeName: [UIFont systemFontOfSize:16.0]};
textField.typingAttributes = attributes;
[alert show];
}
After iOS 7.x you cannot customize the appearance of alert views, Why? Because its view hierarchy is private.
It is mentioned clearly in UIAlertView Class Reference:
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.
So unfortunately it is impossible to change the textField font, buttons text color .. etc.
The only solution is using one of the custom UIAlertView's.
You have to use custom alertview. Just check below link.
DTAlertView
It has good animation and textfield can be added too.
Once you use this, you don't have to write such big code.
Hope it helps.
You can create a custom UIAlertView using one of this:
http://code.tutsplus.com/tutorials/ios-sdk-uialertview-custom-graphics--mobile-8886
https://github.com/eaigner/CODialog
and apply your custom textField.
For the second option, in CODialog.m (addTextFieldWithPlaceholder function) you can change the font size, or modify kCODialogTextFieldHeight constant.
field.font = [UIFont systemFontOfSize:kCODialogTextFieldHeight - 8.0];
Related
Here is a screenshot of a UIAlertController. I was just playing around custom fonts and textfield properties but I was unable to accomplish the following:
clear background of the UITextField
no ugly border (black box) as shown below
As I dived more into the code and iOS runtime headers, I was able to modify border and background color but the above issue still remains as those properties belong to a container UITextView. Changing background to clearColor doesn't help.
Has anybody ever played around with this? Not sure if I would ever take my app into production with such ugly text fields.
EDIT (May 13, 15) The answer below by Rory McKinnel is tested for iOS 8 - 8.3 and works just fine. The result is below:
Had some fun with this. The following seems to work. Obviously judging by what was required, it has no future proofing and is a patch away from not working.
I figured this out by walking the view hierarchy in the debugger, from which I noticed a UIVisualEffectView. Removing that seems to give you what you want along with setting the containing view to a clear background. Without removing the visual effect, a clear background shows what is behind the alert view itself for some reason.
UIAlertController *alertController =
[UIAlertController alertControllerWithTitle:#"Its Not Pretty!"
message:#"Some times things get ugly!"
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
textField.text = #"Text: No border and clear 8^)";
}];
[self presentViewController:alertController animated:TRUE completion:^{
}];
for (UIView* textfield in alertController.textfields) {
UIView *container = textField.superview;
UIView *effectView = container.superview.subviews[0];
if (effectView && [effectView class] == [UIVisualEffectView class]){
container.backgroundColor = [UIColor clearColor];
[effectView removeFromSuperview];
}
}
here is the important part in swift:
for textfield: UIView in alertController.textfields {
var container: UIView = textField.superview
var effectView: UIView = container.superview.subviews[0]
container.backgroundColor = UIColor.clearColor()
effectView.removeFromSuperview()
}
Swift 3 clear version
alertController.textFields?.forEach {
$0.superview?.backgroundColor = .clear
$0.superview?.superview?.subviews[0].removeFromSuperview()
}
You can try this.
As you need only clear color to textfield of your alertview.
simply add lines of code after your alertview is created.
UITextField *textField = [alertView textFieldAtIndex:0];
textField.backgroundColor=[UIColor clearColor];
textField.superview.backgroundColor=[UIColor clearColor];
EDIT
for alertviewCoontroller you can add
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.backgroundColor=[UIColor clearColor];
textField.superview.backgroundColor=[UIColor clearColor];
}];
Thanks, revert if any confusion.
You can change the border and background color like this:
let subview = alertController!.view.subviews.first! as UIView
let alertContentView = subview.subviews.first! as UIView
alertContentView.backgroundColor = UIColor.lightGrayColor()
alertContentView.layer.cornerRadius = 10;
alertContentView.layer.borderWidth = 2;
Swift 2.0 version:
for textField in alert.textFields! {
if let container = textField.superview, let effectView = container.superview?.subviews.first where effectView is UIVisualEffectView {
container.backgroundColor = UIColor.clearColor()
effectView.removeFromSuperview()
}
}
To address the situation as discussed in #Rory McKinnel and #Matthew where the superview are NULL and address modifying presented view:
extension UIAlertController {
override open func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.textFields?.forEach {
$0.superview?.backgroundColor = .color
$0.superview?.superview?.subviews[0].removeFromSuperview()
}
}
}
This is very hacky, so examine it well before using (tested on iOS 8.3):
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#"My Alert"
message:#"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"This is my placeholder";
textField.backgroundColor = [UIColor colorWithRed:246.0/255.0 green:246.0/255.0 blue:246.0/255.0 alpha:1.0]; // You can change it to whatever color you want
[textField superview].backgroundColor = textField.backgroundColor;
[[textField superview] superview].backgroundColor = [UIColor whiteColor];
}];
I have a UIAlertView with a UITextField. I want the user to type in something and tap OK. But when the keyboard appears, it hides the buttons of the UIAlertView. Is there a way to make the UIAlertView move when the keyboard is shown. Or just move it upward so the buttons will be seen by the user?
This is the code I use:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"title" message:#"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Other", nil] ;
alertView.tag = 2;
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alertView textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypePhonePad;
CGRect frame = alertView.frame;
frame.origin.y = -140;
alertView.frame = frame;
alertTextField.text = #"some text";
[alertView show];
I dont know if alert view can be positioned itself, but maybe you can put it in to the scroll view and when user tap textfield it just scroll to the visible area for you.
How to achieve textfieldname.text and textfield.placeholder functionality for ios 7? I had used following code.
UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:#"Title"];
[dialog setMessage:#" "];
[dialog addButtonWithTitle:#"OK"];
[dialog addButtonWithTitle:#"Cancel"];
[dialog setAlertViewStyle:UIAlertViewStylePlainTextInput];
[dialog show];
At sometime i want to load some data into textfield like "Welcome" is loaded in textfield if user need then they can modify test into "Welcome to stackoverflow". Is it possible to achieve that functionality? Advance thanks for any help.
UITextField *textField = [dialog textFieldAtIndex:0];
textField.text = #"Some Text";
textField.placeholder = #"Some Placeholder";
More info: https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html#//apple_ref/occ/instm/UIAlertView/textFieldAtIndex:
in another way
The UIALertView has a textFieldAtIndex: method that returns the UITextField object you want.
For a UIAlertViewStylePlainTextInput, the index of the textfield is 0.
You can then set the placeholder (or text) property of the textfield:
UIAlertView * dialog = ....
UITextField *textField = [dialog textFieldAtIndex:0];
textField.placeholder = #"your text";
reference : UIAlertView Class Reference
You will need to setting up the delegate for the UIAlert as well as UITextfield. Can be worked as:
[[dialog textFieldAtIndex:0] setDelegate:self];
UITextField *alertTextField = [dialog textFieldAtIndex:0];
By becoming the delegate for the input text field you can find out when the return key on the keyboard is pressed. Then in the .m file for your class you implement the delegate method below and tell the alert to disappear:
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[self.dialog dismissWithClickedButtonIndex:self.scanCode.firstOtherButtonIndex animated:YES];
return YES;
}
I have an iOS app that I recently updated to deal with the UIAlertView / SubView issue that causes the textboxes to render as clear or white (or not render at all, not sure which). In any case, this is a relatively simple question as I'm kind of new to Obj-C, but how do I get the value of the new textbox from another call in the app?
Here is my UIAlertView:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Password"
message:#"Enter your Password\n\n\n"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Login", nil];
alert.frame = CGRectMake( 0, 30, 300, 260);
It used to be stored as a UITextField, and then added to the UIAlertView as a subview:
psswdField = [[UITextField alloc] initWithFrame:CGRectMake(32.0, 65.0, 220.0, 25.0)];
psswdField.placeholder = #"Password";
psswdField.secureTextEntry = YES;
psswdField.delegate = self;
psswdField.tag = 1;
[psswdField becomeFirstResponder];
[alert addSubview:psswdField];
[alert show];
[alert release];
This is all commented out now, and instead I have it rewritten as:
alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
This is how I used to retrieve the value:
[psswdField resignFirstResponder];
[psswdField removeFromSuperview];
activBkgrndView.hidden = NO;
[activInd startAnimating];
[psswdField resignFirstResponder];
[self performSelectorInBackground:#selector(loadData:) withObject:psswdField.text];
Now I'm a bit confused as to how I get the value from that textbox to send to loadData.
You don't want to add your own text field to the alert view. You're not supposed to directly add subviews to a UIAlertView. There is an alertViewStyle property on UIAlertView that you want to set to UIAlertViewStyleSecureTextInput, which will add a text field for you. So you would set it with a line like this:
alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
Then you will retrieve the value in this text field using the delegate method - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex, which you must add to the class that you're setting as your UIAlertView delegate. Here is an example implementation of that delegate method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
// Make sure the button they clicked wasn't Cancel
if (buttonIndex == alertView.firstOtherButtonIndex) {
UITextField *textField = [alertView textFieldAtIndex:0];
NSLog(#"%#", textField.text);
}
}
I use the following code to show a UIAlertView with a text field:
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Rename", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField* textField = [alertView textFieldAtIndex:0];
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
[alertView show];
The result looks like this on iOS 5.1:
and on iOS 6.1:
As you can see, the clear button is a bit higher than it should be on iOS 6.1. Any idea how to properly center the button? Adding textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; does not help either.
(Apparently, this is a bug in iOS 6.1 but maybe someone knows a workaround. Besides, the height of the text field in alert views also seem to differ in different OS versions but that is another issue.)
This looks like a bug from Apple, but you can always add your own clear button in the textField, doing something like this :
First create a UITextField property:
#property (nonatomic, strong) UITextField *textField;
And then assign it to the alertView's textField
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Rename", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
self.textField = [alertView textFieldAtIndex:0];
self.textField.delegate = self;
//Create a custom clear button
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 19, 19)];
[button setImage:[UIImage imageNamed:#"clearButton.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(clearText) forControlEvents:UIControlEventAllTouchEvents];
[self.textField setRightView:button];
self.textField.rightViewMode = UITextFieldViewModeAlways;
And then in the clearText method,
- (void)clearText {
[self.textField setText:#""];
}
I tried this out and it works
Hope this helps
Another way to do this is by overriding UITextField's clearButtonRectForBounds:.