I want exact this placeholder place
I try all alignments it's not work. This is my placeholder program to implement the white color:
[searchtextfield setValue:[UIColor whitecolor] forKeyPath:#"_placeholderLabel.textColor"];
For Objective c
For padding only placeholder this code will work
usernameTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#" Your text"];
For padding both placeholder and text of the UITextField this below code will work
usernameTF.layer.sublayerTransform = CATransform3DMakeTranslation(40, 0, 30);
For Swift 3.0
For padding only placeholder this code will work
self.usernameTF.attributedPlaceholder = NSAttributedString(string: " YourText")
For padding both placeholder and text of the UITextField this below code will work
usernameTF.layer.sublayerTransform = CATransform3DMakeTranslation(40, 0, 30)
Easy way to implement this and you also use this to multiple text fields :
First make a method of this :
create new file (nsobject) file name : Alert
alert.m
+(void) setLeftPaddingTextField:(UITextField *)textField paddingValue:(int) paddingValue
{
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, paddingValue, textField.frame.size.height)];
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;
}
+(void) setRightPaddingTextField:(UITextField *)textField paddingValue:(int) paddingValue
{
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, paddingValue, textField.frame.size.height)];
textField.rightView = paddingView;
textField.rightViewMode = UITextFieldViewModeAlways;
}
alert.h
//left padding
+(void) setLeftPaddingTextField:(UITextField *)textField paddingValue:(int) paddingValue;
//right padding
+(void) setRightPaddingTextField:(UITextField *)textField paddingValue:(int) paddingValue;
your controller
[Alert setLeftPaddingTextField:yourTextField paddingValue:20];
you can use this to many text fields with one defined method.
I'm having trouble implementing a UISearchBar whose UITextField has a rightview. I tried on a normal UITextField the following:
self.textField = [[UITextField alloc] initWithFrame: CGRectMake(10, 30, 300, 30)];
self.textField.borderStyle = UITextBorderStyleRoundedRect;
self.textField.clearButtonMode = UITextFieldViewModeNever;
self.textField.delegate = self;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, 150, 44)];
[button setBackgroundColor: [UIColor redColor]];
[self.textField setRightViewMode:UITextFieldViewModeAlways];
[self.textField setRightView:button];
[self.view addSubview:self.textField];
And as expected, it generates the following:
I need the UISearchBar's UITextField to be have the same -- so I decided to subclass UISearchBar.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// This is simply here to make sure that we are able to get the textfield
[self setSearchTextPositionAdjustment:UIOffsetMake(0, 0)];
// Function that helps us get the textfield
UITextField *textField = [self textFieldGetter];
// This will be true if we call setSearchTextFieldPositionAdjust
if (textField) {
// Verify that the textfield exists...
NSLog(#"The textField does exist");
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, 150, 44)];
[button setBackgroundColor: [UIColor redColor]];
// Set the clear button to be off
textField.clearButtonMode = UITextFieldViewModeNever;
// Make the button the rightview of the textfield
[textField setRightView:button];
[textField setRightViewMode:UITextFieldViewModeAlways];
}
}
return self;
}
But it does not work and the UISearchBar simply looks like:
The UITextFieldGetter is in essence something very similar to this answer.
Why isn't the UITextField the same in the UISearchBar as it is normally? Is there a way that I can get the same effect programmatically? Or if it can't be done, is there a way that I can change the UITextField's width once I know the button is there?
So when the user wants to recover their password they tap a button and a subview appears with aUITextField and a UIButton. After the user enters their e-mail I want the UIButton to send the email. I am missing the referral to the UITextField Here is my code:
- (IBAction)recoverPassword:(id)sender {
CGRect frame = CGRectMake(12, 51, 298, 497);
UIView *infoView = [[UIView alloc] initWithFrame:frame];
infoView.opaque = NO;
infoView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8f];
infoView.userInteractionEnabled = YES;
infoView.tag = 01;
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(8, 200, 280, 30)];
textField.textAlignment = NSTextAlignmentLeft;
textField.textColor = [UIColor blackColor];
UIButton *getPassword = [[UIButton alloc] initWithFrame:CGRectMake(8, 265, 280, 36)];
getPassword.backgroundColor = [UIColor blackColor];
[getPassword addTarget:self
action:#selector(getUserPassword:)
forControlEvents:UIControlEventTouchUpInside];
[infoView addSubview:textField];
[infoView addSubview:getPassword];
[self.view addSubview:infoView];
}
- (void)getUserPassword:(UITapGestureRecognizer *)sendPassword {
[PFUser requestPasswordResetForEmailInBackground:**Missing Part**];
[[self.view viewWithTag:01] removeFromSuperview];
NSLog(#"Dissmissed Info SubView");
}
It looks like you are creating subviews (UITextField, UIButton, etc) only on the local scope (inside the method scope), thus they won't be accessible to other methods of the same class.
Try creating an ivar for the class you have these methods in, and assigning the values to the ivar, so the same variables will be accessible in your -getUserPassword: method.
P.S. Same approach, but applied to infoView, would eliminate the need to access it via viewWithTag, because you will be able to access it directly.
I have a UIView, with lots of UITexfields,
UITextField *field1 = [[UITextField alloc] initWithFrame:CGRectMake(135, 292, 50, 20)];
field1.backgroundColor = [UIColor clearColor];
field1.borderStyle = UITextBorderStyleNone;
field1.returnKeyType = UIReturnKeyNext;
field1.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
field1.font = [UIFont fontWithName:inputFont size:fontSize];
field1.placeholder = #"size";
[self addSubview:field1];
Can I create a UITextField Class and create an instance of this class with these parameters preset? -so as to reduce the code: (If I have lots of fields the code gets very long and repetitive? -
Can i get some help on how about to set up this class! -thank you
Yes You Can do this by making a BaseUITextFeild .h and .m file as shown in the following image.
And then Just add the following code regarding your design in BaseUITextFeild.m file
- (void)awakeFromNib {
[self setupUI];
[self setKeyboardAppearance:UIKeyboardAppearanceAlert];
}
// Function to setup the common UI throught the app.
- (void)setupUI {
[self setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[self setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[self setTextAlignment:NSTextAlignmentCenter];
[self setValue:[UIColor colorWithRed:120.0/225.0 green:120.0/225.0 blue:120.0/225.0 alpha:1.0] forKeyPath:#"_placeholderLabel.textColor"];
[self setFont:[UIFont boldSystemFontOfSize:15.0f]];
}
Generating a custom UITextField programmatically
And then just import "BaseUITextFeild.h" in the ViewController Where you want to use it.
BaseUITextFeild *field1 = [[BaseUITextFeild alloc] initWithFrame:CGRectMake(135, 292, 50, 20)];
[self addSubview:field1];
Generating a custom UITextField From Interface Builder(Storyboard/.XIB)
Add a UITextfield and then just Change the Class to your Custom TextView Class.
By Applying Following technique you can use this same UITextFeild through out the Application, Just need to import BaseUITextFeild.h.
You can also use this UITextFieldCategory
Follow this Two Example UITextField+withFrame.h and UITextField+ColoredPlaceholder.h.
You can also customize you UITextField as per your need.
Create a method
-(UITextField*)createTextFieldWithFrame:(CGRect)frame{
UITextField *textField = [[UITextField alloc] initWithFrame:frame];
textField.backgroundColor = [UIColor clearColor];
textField.borderStyle = UITextBorderStyleNone;
textField.returnKeyType = UIReturnKeyNext;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
textField.placeholder = #"size";
[self.view addSubview:textField];
return textField;
}
for creating multiple textfields call this method
UITextField *text1 = [self createTextFieldWithFrame:CGRectZero];
UITextField *text2 = [self createTextFieldWithFrame:CGRectZero];
You can create a custom xib with the UITextField created as you want and reuse that every time you need.
Or you can subclass UITextField and do the additional setup inside.
I am using a text view as a comment composer.
In the properties inspector I can't find anything like a border style property so that I can make use a rounded rect, something like UITextField.
So, the question is: How can I style a UITextView like a UITextField with a rounded rect?
There is no implicit style that you have to choose, it involves writing a bit of code using the QuartzCore framework:
//first, you
#import <QuartzCore/QuartzCore.h>
//.....
//Here I add a UITextView in code, it will work if it's added in IB too
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 220, 200, 100)];
//To make the border look very close to a UITextField
[textView.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[textView.layer setBorderWidth:2.0];
//The rounded corner part, where you specify your view's corner radius:
textView.layer.cornerRadius = 5;
textView.clipsToBounds = YES;
It only works on OS 3.0 and above, but I guess now it's the de facto platform anyway.
this code worked well for me:
[yourTextView.layer setBackgroundColor: [[UIColor whiteColor] CGColor]];
[yourTextView.layer setBorderColor: [[UIColor grayColor] CGColor]];
[yourTextView.layer setBorderWidth: 1.0];
[yourTextView.layer setCornerRadius:8.0f];
[yourTextView.layer setMasksToBounds:YES];
Swift 3 Version
After setting up the text view in interface builder.
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textView.layer.cornerRadius = 5
textView.layer.borderColor = UIColor.gray.withAlphaComponent(0.5).cgColor
textView.layer.borderWidth = 0.5
textView.clipsToBounds = true
}
Swift 2.2 Version
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textView.layer.cornerRadius = 5
textView.layer.borderColor = UIColor.grayColor().colorWithAlphaComponent(0.5).CGColor
textView.layer.borderWidth = 0.5
textView.clipsToBounds = true
}
Edit: You have to import
#import <QuartzCore/QuartzCore.h>
for using corner radius.
Try this it will work for sure
UITextView* txtView = [[UITextView alloc] initWithFrame:CGRectMake(50, 50, 300, 100)];
txtView.layer.cornerRadius = 5.0;
txtView.clipsToBounds = YES;
As
Rob figured it out setting the if you want the border color to be similar as UITextField then you need to change the border width to 2.0 and color to gray by adding the following line
[textView.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[textView.layer setBorderWidth:2.0];
I wanted the real deal, so I add UIImageView as a subview of the UITextView. This matches the native border on a UITextField, including the gradient from top to bottom:
textView.backgroundColor = [UIColor clearColor];
UIImageView *borderView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, textView.frame.size.width, textView.frame.size.height)];
borderView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
UIImage *textFieldImage = [[UIImage imageNamed:#"TextField.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 8, 15, 8)];
borderView.image = textFieldImage;
[textField addSubview: borderView];
[textField sendSubviewToBack: borderView];
These are the images I use:
One solution is to add a UITextField below the UITextView, make the UITextView background transparent and disable any user interaction on the UITextField. Then in code change the UITextField frame with something like that
self.textField.frame = CGRectInset(self.textView.frame, 0, -2);
You will have exactly the same look as a text field.
And as suggested by Jon, you should put this piece of code inside [UIViewController viewDidLayoutSubviews] on iOS 5.0+.
For the best effect you have to use a custom (stretchable) background image. This is also how the UITextField's rounded border is drawn.
One way I found to do it without programming is to make the textfield background transparent, then place a Round Rect Button behind it. Make sure to change the button settings to disable it and uncheck the Disable adjusts image checkbox.
You may want to check out my library called DCKit.
You'd be able to make a rounded corner text view (as well as text field/button/plain UIView) from the Interface Builder directly:
It also has many other useful features, such as text fields with validation, controls with borders, dashed borders, circle and hairline views etc.
I know there are already a lot of answers to this one, but I didn't really find any of them sufficient (at least in Swift). I wanted a solution that provided the same exact border as a UITextField (not an approximated one that looks sort of like it looks right now, but one that looks exactly like it and will always look exactly like it). I needed to use a UITextField to back the UITextView for the background, but didn't want to have to create that separately every time.
The solution below is a UITextView that supplies it's own UITextField for the border. This is a trimmed down version of my full solution (which adds "placeholder" support to the UITextView in a similar way) and was posted here: https://stackoverflow.com/a/36561236/1227119
// This class implements a UITextView that has a UITextField behind it, where the
// UITextField provides the border.
//
class TextView : UITextView, UITextViewDelegate
{
var textField = TextField();
required init?(coder: NSCoder)
{
fatalError("This class doesn't support NSCoding.")
}
override init(frame: CGRect, textContainer: NSTextContainer?)
{
super.init(frame: frame, textContainer: textContainer);
self.delegate = self;
// Create a background TextField with clear (invisible) text and disabled
self.textField.borderStyle = UITextBorderStyle.RoundedRect;
self.textField.textColor = UIColor.clearColor();
self.textField.userInteractionEnabled = false;
self.addSubview(textField);
self.sendSubviewToBack(textField);
}
convenience init()
{
self.init(frame: CGRectZero, textContainer: nil)
}
override func layoutSubviews()
{
super.layoutSubviews()
// Do not scroll the background textView
self.textField.frame = CGRectMake(0, self.contentOffset.y, self.frame.width, self.frame.height);
}
// UITextViewDelegate - Note: If you replace delegate, your delegate must call this
func scrollViewDidScroll(scrollView: UIScrollView)
{
// Do not scroll the background textView
self.textField.frame = CGRectMake(0, self.contentOffset.y, self.frame.width, self.frame.height);
}
}
One way I found to do it without programming is to make the textfield background transparent, then place a Round Rect Button behind it. Make sure to change the button settings to disable it and uncheck the Disable adjusts image checkbox.
Tried the Quartzcore code and found it caused lag on my old 3G (I use for testing). Not a big issue but if you want to be as inclusive as possible for different ios and hardware I recommend Andrew_L's answer above - or make your own images and apply accordingly.
There is a great background image that is identical to the UITextView used for sending text messages in iPhone's Messages app. You'll need Adobe Illustrator to get & modify it.
iphone ui vector elements
#import <QuartzCore/QuartzCore.h>
- (void)viewDidLoad{
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 220, 200, 100)];
textView.layer.cornerRadius = 5;
textView.clipsToBounds = YES;
[textView.layer setBackgroundColor: [[UIColor whiteColor] CGColor]];
[textView.layer setBorderColor: [[UIColor grayColor] CGColor]];
[textView.layer setBorderWidth: 1.0];
[textView.layer setCornerRadius:8.0f];
[textView.layer setMasksToBounds:YES];
[self.view addSubView:textview];
}
You can create a Text Field that doesn't accept any events on top of a Text View like this:
CGRect frameRect = descriptionTextField.frame;
frameRect.size.height = 50;
descriptionTextField.frame = frameRect;
descriptionTextView.frame = frameRect;
descriptionTextField.backgroundColor = [UIColor clearColor];
descriptionTextField.enabled = NO;
descriptionTextView.layer.cornerRadius = 5;
descriptionTextView.clipsToBounds = YES;
If you want to keep your controller code clean, you can subclass UITextView like below, and change the class name in the Interface Builder.
RoundTextView.h
#import <UIKit/UIKit.h>
#interface RoundTextView : UITextView
#end
RoundTextView.m
#import "RoundTextView.h"
#import <QuartzCore/QuartzCore.h>
#implementation RoundTextView
-(id) initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.333] CGColor]];
[self.layer setBorderWidth:1.0];
self.layer.cornerRadius = 5;
self.clipsToBounds = YES;
}
return self;
}
#end
Here is my solution:
- (void)viewDidLoad {
[super viewDidLoad];
self.textView.text = self.messagePlaceholderText;
self.textView.layer.backgroundColor = [[UIColor whiteColor] CGColor];
self.textView.layer.borderColor = [[[UIColor grayColor] colorWithAlphaComponent:0.3] CGColor];
self.textView.layer.borderWidth = 0.5;
self.textView.layer.cornerRadius = 5.5f;
self.textView.layer.masksToBounds = YES;
self.textView.textColor = [[UIColor grayColor] colorWithAlphaComponent:0.4];
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
if (textView == self.tvMessage) {
// Delete placeholder text
if ([self.textView.text isEqualToString:self.messagePlaceholderText]) {
self.textView.text = #"";
self.textView.textColor = [UIColor blackColor];
}
}
}
- (void)textViewDidEndEditing:(UITextView *)textView {
if (textView == self.tvMessage) {
// Write placeholder text
if (self.textView.text.length == 0) {
self.textView.text = self.messagePlaceholderText;
self.textView.textColor = [[UIColor grayColor] colorWithAlphaComponent:0.4];
}
}
}
I don't think that it is possible. but you can do UITableView(grouped) with 1 section and 1 empty cell and use it as a container for your UITextView.
This is an old question, and I was also searched for this questions answer. luvieeres' answer is 100% correct and later Rob added some code. That is excellent, but I found a third party in another questions answer which seems very helpful to me. I was not only searched for similar look of UITextField over UITextView, I was also searched for multiline support. ChatInputSample satisfied both. Thats why I think this third party might be helpful to others. Also thanks to Timur, he mentioned this open source in here.
In iOS7 the following matches UITextField border perfectly (to my eye at least):
textField.layer.borderColor = [[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor];
textField.layer.borderWidth = 0.5;
textField.layer.cornerRadius = 5;
textField.clipsToBounds = YES;
There is no need to import anything special.
Thanks to #uvieere and #hanumanDev whose answers go me almost there :)
How about just:
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, 280, 32)];
textField.borderStyle = UITextBorderStyleRoundedRect;
[self addSubview:textField];