label textField textview

使用UILabel显示静态文本
想要给用户显示静态文本,并且控制文本的字体和颜色。

各位亲 有时间可以去看看我的   “金骏家居淘宝店” http://jinjun1688.taobao.com/shop/view_shop.htm?tracelog=twddp  买时说明在我的博客看到有优惠哦 还有意外礼品赠送  真正的程序员淘宝店
@property(nonatomic,strong)UILabel *myLabel;
@synthesize myLabel;
-(void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    CGRect labelFrame = CGRectMake(0.0f,0.0f,100.0f,23.0f);
    self.myLabel = [ [UILabel alloc]initWithFrame:labelFrame];
    self.myLabel.text = @"hello world";
    self.myLabel.font = [UIFont boldSystemFontOfSize:14.0f];
    self.myLabel.numberOfLines = 3;//文本行数
    self.myLabel.adjustsFontSizeToFitWidth = YES; //标签视图保持静态,并且标签里的字体能自动调整到适合标签的边界。
    self.myLabel.center = self.view.center;
    [self.view addSubview:self.myLabel];
}
使用UITextField接受用户文本输入(默认高度31像素)
@property( nonatomic,strong)UITextField *myTextField;
@synthesize myTextField;
-(void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    CGRect textFieldFrame = CGRectMake(0.0f,0.0f,200.0f,31.0f);
    self.myTextField = [[UITextField alloc] initWithFrame:textFieldFrame];
    self.myTextField.borderStyle = UITextBorderStyleRoundedRect;//文本视图边框
    self.myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;//文本视图纵向居中
    self.myTextField.textAlignment = UITextAlignmentCenter;//文本水平对齐方式
    self.myTextField.text = @"hello world";
    self.myTextField.center = self.view.center;
    [self.view addSubview:self.myTextField];
}
UITextFieldDelegate协议
textFieldShouldBeginEditing://BOOL 设置文本视图是否可编辑
textFieldDidBeginin: //当用户开始编辑这个文本视图时这个方法将会被调用。
textFieldShouldEndEditing: //这个方法返回一个BOOL值,它将告诉文本视图是否结束当前的编辑进程。假如返回NO,用户将不能终止文本的编辑。
textFieldDidEndEditing: 当文本视图的编辑进程终止时将会被调用。
textField:shouldChangeCharacterInRange:replacementString: 任何时候文本视图里的文本被修改都会调用这个方法。返回是一个布尔值,NO,文本视图里的文本的修改将不会被通知和发生,YES,说明允许修改文本。
textFieldShouldClear:每个文本视图都有一个clear按钮,通常是一个圆形X按钮。
textFieldShouldReturn:当用户在键盘上按下return 或 enter 键时将会调用这个方法,尝试隐藏键盘,你需要将这个文本视图注册为这个方法的第一个响应者。
@property(nonatomic,strong)UITextField *myTextField;
@property(nonatomic,strong)UILabel *labelCounter;
 
@synthesize myTextField;
@synthesize labelCounter;
-(void)calculateAndDisplayTextFieldLengthWithText:(NSString *)paramText{
    NSString *characterOrCharacters = @"Characters";
    if([paramText length] == 1){
        characterOrCharacters = @"Character";
    }
    self.labelCounter.text = [NSString stringWithFormat:@"%lu %@",(unsigned long)[paramText length],characterOrCharacters];
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    BOOL result = YES;
    if([textField isEqual:self.myTextField]){
        NSString *wholeText = [textField.text stringByReplacingCharactersInRange:range withString:string];
    [self calculateAndDisplayTextFieldLengthWithText:wholeText];
    }
    return result;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
-(void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whileColor];
    CGRect textFieldFrame = CGRectMake(38.0f,30.0f,220.0f,31.0f);
    self.myTextField = [[UITextField alloc]initWithFrame:textFieldFrame];
    self.myTextField.delegate = self;
    self.myTextField.borderStyle = UITextBorderStyleRounderRect;
    self.myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    self.myTextField.text = @"hello world";
    self.myTextField.placeholder = @"Enter text here … ";
    self.view addSubview:self.myTextField];
    CGRect labelCounterFrame = self.myTextField.frame;
    labelCounterFrame.origin.y += textFieldFrame.size.height + 10;
    self.labelCounter = [ [UILabel alloc]initWithFrame:labelCounterFrame];
    [self.view addSubview:self.labelCounter];
    [self calculateAndDisplayTextFieldLengthWithText];
    [self calculateAndDisplayTextFieldLengthWithText:selfTextField.text];
}
文本视图有两个相对属性,它们分别是leftView和rightView。
UILabel *currencyLabel = [ [UILabel alloc]initWithFrame:CGRectZero];
currencyLabel.text = [ [ [NSNumberFormatter alloc]init]currencySymbol];
currencyLabel.font = self.myTextField.font;
[currencyLabel sizeToFit];
self.myTextField.leftView = currencyLabel;
self.myTextField.leftViewMode = UITextFieldViewModeAlways;
typedef enum{
    UITextfieldViewModeNever,
    UITextFieldViewModeWhileEditing,//假如已经声明一个值给它,当用户编辑这个文本视图时左视图将会显示。
    UITextFieldViewModeUnlessEditing,//当用户没有在文本视图里编辑文本时左视图将会显示,但是一旦开始编辑,做视图将会消失。
    UItextFieldViewModeAlways
}UITextFieldViewMode;
 
使用UITextView显示文本域
@property( nonatomic,strong)  UItextView  *myTextView;
@synthesize myTextView;
-(void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whileColor];
    self.myTextView = [[UITextView alloc]initWithFrame:self.view.bounds];
    self.myTextView.text = @"hello world";
    self.myTextView.font = [UIFont systemFontOfSize:16.0f];
    self.view addSubView:self.myTextView];
}
 
现假如你点击文本视图,虚拟键盘遮盖了文本视图的区域。怎么办呢?为了能弥补这个缺陷,我们需要监听一些待定通知:
UIKeyboardWillShowNotification //当键盘正准备显示子屏幕上时这个通知将会被发送给任何部件,比如文本视图。
UIKeyboardDidShowNotification //当键盘已经显示在屏幕上时将会发送这个通知。
UIKeyboardWIllHideNotification //当键盘即将被隐藏时将会发送通知
UIKeyboardDidHideNotification //当键盘完全隐藏时将会发送这个通知
所以我们的策略就是去发现键盘什么时候准备在屏幕上显示然后以何种方式调整视图。
-(void)handleKeyBoardDidShow:(NSNotification *)paramNotification{
    NSValue *keyboardRectAsObject = [[paramNotification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];/*get the frame of the keyboard*/
    CGRect keyboardRect;
[keyboardRectAsObject getValue:&keyboardRect];/*place it in a CGRect*/
    self.myTextView.contentInset = UIEdgeInsetsMake(0.0f,0.0f,  keyboarddRect.size.height,0.0f);
}
-(void)handleKeyboardWillHide:(NSNotification *)paramNotification{
    self.myTextView.contentInset = UIEdgeInsetsZero;/*make the text view as the whole view again*/
}
-(void)viewWillAppear:(BOOL)paramAnimated{
    [super viewWillAppear:paramAnimated];
    [ [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    [ [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    self.view.backgroundColor = [UIColor whiteColor];
    self.myTextView = [ [UITextView alloc] initWithFrame:self.view.bounds];
    self.myTextView.text = @""hello world……;
    self.myTextView.font = [UIFont systemFontOfSize:16.0f];
    [self.view addSubview: self.myTextView];
}
-(void)viewWillDisappear:(BOOL)paramAnimated{
    [super viewWillDisappear:paramAnimated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
在这段代码中,在方法 viewWillAppear:开始建通键盘通知,在方法viewWillDisappear 结束监听。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值