码迷,mamicode.com
首页 > 其他好文 > 详细

063为文本输入框设置背景图片

时间:2015-06-15 18:32:19      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:

效果如下:

技术分享

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

 

063为文本输入框设置背景图片

标签:

原文地址:http://www.cnblogs.com/huangjianwu/p/4578629.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!