标签:
/**
* 用户名
*/
@property(strong,nonatomic) UITextField *txtName;
@property(strong,nonatomic) UITextView *txtView;
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 初始化
self.txtName=[[UITextField alloc] initWithFrame:CGRectMake(100, 50, 200, 44)];
// 添加背景色
//self.txtName.backgroundColor=[UIColor redColor];
// 键盘
self.txtName.borderStyle=UITextBorderStyleLine;
self.txtName.placeholder=@"请输入姓名";
[self.view addSubview:self.txtName];
// 不用占位符赋的字符实的存在。
self.txtName.text=@"asdf";
self.txtName.delegate=self;
NSMutableAttributedString *str=[[NSMutableAttributedString alloc] initWithString:@"lamco" attributes:@{NSBackgroundColorAttributeName:[UIColor redColor]
}];
[str addAttributes:@{NSBackgroundColorAttributeName:[UIColor greenColor],NSFontAttributeName:[UIFont systemFontOfSize:40]} range:NSMakeRange(2, 1)];
[str addAttributes:@{NSForegroundColorAttributeName:[UIColor yellowColor]} range:NSMakeRange(1, 3)];
//
//
self.txtName.attributedText=str;
//
// self.txtName.returnKeyType=UIReturnKeySend;
//
// self.txtName.keyboardType=UIKeyboardTypePhonePad;
//
// self.txtView=[[UITextView alloc]
// initWithFrame:self.view.frame];
// self.txtView.text=@"新华网北京3月7日电 3月7日上午,来到十二届全国人大四次会议黑龙江代表团参加审议。";
// [self.view addSubview:self.txtView];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
}
return YES;
}
@property(strong,nonatomic) UIButton *btnTest;
@property(strong,nonatomic) UITextField *txtName;
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.btnTest=[UIButton buttonWithType:UIButtonTypeRoundedRect];
self.btnTest.frame=CGRectMake(100, 100, 150, 44);
[self.btnTest setTitle:@"按钮" forState:UIControlStateNormal];
[self.btnTest setBackgroundColor:[UIColor yellowColor]];
[self.btnTest addTarget:self action:@selector(testBtn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.btnTest];
self.txtName=[[UITextField alloc] initWithFrame:CGRectMake(100, 200, 200, 44)];
self.txtName.borderStyle=1;
[self.view addSubview:self.txtName];
}
-(void)testBtn
{
NSString *name=self.txtName.text;
NSLog(@"%@",name);
int len=(int)self.txtName.text.length;
if (len==0) {
NSLog(@"用户名不能为空");
}
else if (len>0&&len<8)
{
NSLog(@"长度不够");
}
else {
NSLog(@"继续");
}
}
标签:
原文地址:http://www.cnblogs.com/tianlianghong/p/5256199.html