UITextFeild usage and UITextFeild usage

Source: Internet
Author: User

UITextFeild usage and UITextFeild usage

1. Modify the color of the placeholder string:

= ========

# Import "ViewController. h"
# Import "MyTextField. h"

@ Interface ViewController ()
{
UITextField * _ textF;
}
@ End

@ Implementation ViewController

-(Void) viewDidLoad {
[Super viewDidLoad];

Self. view. backgroundColor = [UIColor whiteColor];

MyTextField * textF = [[MyTextField alloc] initWithFrame: CGRectMake (100,100,200, 40)];
_ TextF = textF;
[Self. view addSubview: textF];

TextF. backgroundColor = [UIColor whiteColor];
TextF. borderStyle = UITextBorderStyleRoundedRect;
TextF. leftViewMode = UITextFieldViewModeWhileEditing;

UIButton * btn = [[UIButton alloc] initWithFrame: CGRectMake (0, 0, 30, 30)];
Btn. backgroundColor = [UIColor greenColor];
TextF. leftView = btn;

TextF. placeholder = @ "Haha ";

UIColor * color = [UIColor colorWithRed: 100/255. 0 green: 200/255. 0 blue: 100/255. 0 alpha: 0.7];
[TextF setValue: color forKeyPath: @ "_ placeholderLabel. textColor"]; // modify the color of the placeholder string "Haha"

}

-(Void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event
{
[_ TextF resignFirstResponder];
}

@ End

 

Running effect:

1. The editing status is not displayed (textF. leftView = btn; hide ):

 

 

2. Enter the editing status (textF. leftView = btn; displayed ):

 

 

 

========= Method 2 ========================================= =

# Import "MyTextField. h"

@ Implementation MyTextField


-(Void) drawPlaceholderInRect :( CGRect) rect {
UIColor * placeholderColor = [UIColor redColor]; // set the color.
[PlaceholderColor setFill];

CGRect placeholderRect = CGRectMake (rect. origin. x + 30, (rect. size. height-self. font. pointSize)/5, rect. size. width, self. font. pointSize); // set the distance

NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init];
Style. lineBreakMode = NSLineBreakByTruncatingMiddle;
Style. alignment = self. textAlignment;
NSDictionary * attr = [NSDictionary attributes: style, NSParagraphStyleAttributeName, self. font, NSFontAttributeName, placeholderColor, NSForegroundColorAttributeName, nil];

[Self. placeholder drawInRect: placeholderRect withAttributes: attr];
}

@ End

 

# Import "ViewController. h"
# Import "MyTextField. h"

@ Interface ViewController ()
{
UITextField * _ textF;
}
@ End

@ Implementation ViewController

-(Void) viewDidLoad {
[Super viewDidLoad];

Self. view. backgroundColor = [UIColor whiteColor];


MyTextField * textF = [[MyTextField alloc] initWithFrame: CGRectMake (100,100,200, 40)];
_ TextF = textF;
[Self. view addSubview: textF];

TextF. backgroundColor = [UIColor whiteColor];
TextF. borderStyle = UITextBorderStyleRoundedRect;
TextF. leftViewMode = UITextFieldViewModeWhileEditing;

UIButton * btn = [[UIButton alloc] initWithFrame: CGRectMake (0, 0, 30, 30)];
Btn. backgroundColor = [UIColor greenColor];
TextF. leftView = btn;

TextF. placeholder = @ "Haha ";

// UIColor * color = [UIColor colorWithRed: 100/255. 0 green: 200/255. 0 blue: 100/255. 0 alpha: 0.7];
// [TextF setValue: color forKeyPath: @ "_ placeholderLabel. textColor"]; // modify the color of the placeholder string "Haha"

}

-(Void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event
{
[_ TextF resignFirstResponder];
}

@ End

 

Running effect:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.