标签:
效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController<UITextFieldDelegate> 4 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 - (void)layoutUI; 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 12 [self layoutUI]; 13 } 14 15 - (void)didReceiveMemoryWarning { 16 [super didReceiveMemoryWarning]; 17 // Dispose of any resources that can be recreated. 18 } 19 20 #pragma mark - Private Methods 21 - (void)layoutUI { 22 UITextField *txtFMessage = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 320, 50)]; 23 txtFMessage.center = self.view.center; 24 txtFMessage.borderStyle = UITextBorderStyleNone; //默认值是UITextBorderStyleNone 25 txtFMessage.text = @"背景图片,编辑下回车键进入禁用状态"; 26 txtFMessage.textColor = [UIColor colorWithRed:0.263 green:1.000 blue:0.246 alpha:1.000]; 27 txtFMessage.textAlignment = NSTextAlignmentLeft; 28 txtFMessage.contentHorizontalAlignment = UIControlContentVerticalAlignmentCenter; 29 txtFMessage.font = [UIFont fontWithName:@"Times New Roman" size:17]; 30 31 //设置正常状态和禁用状态下的背景图片,在txtFMessage.borderStyle = UITextBorderStyleRoundedRect时就失效 32 txtFMessage.background = [[UIImage imageNamed:@"Paper"] stretchableImageWithLeftCapWidth:20 topCapHeight:20]; 33 txtFMessage.disabledBackground = [[UIImage imageNamed:@"PaperDisabled"] stretchableImageWithLeftCapWidth:20 topCapHeight:20]; 34 txtFMessage.delegate = self; 35 36 [self.view addSubview:txtFMessage]; 37 } 38 39 #pragma mark - TextField 40 - (BOOL)textFieldShouldReturn:(UITextField *)textField { 41 textField.enabled = NO; 42 textField.text = @"文本输入框禁用了"; 43 textField.textColor = [UIColor colorWithRed:0.734 green:0.698 blue:0.698 alpha:1.000]; 44 return YES; 45 } 46 47 @end
标签:
原文地址:http://www.cnblogs.com/huangjianwu/p/4578629.html