当前位置:编程学习 > wap >>

IOS开发(15)之UITextView控件

1 前言
UITextView可以在一个滑动视图里面显示多行文本。

2 代码实例
ZYViewController.h:


[plain]  #import <UIKit/UIKit.h> 
 
@interface ZYViewController : UIViewController 
 
@property(nonatomic,strong) UITextView *myTextView; 
 
@end 

#import <UIKit/UIKit.h>

@interface ZYViewController : UIViewController

@property(nonatomic,strong) UITextView *myTextView;

@end
ZYViewController.m:


[plain]  @synthesize myTextView; 
 
- (void)viewDidLoad 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.view.backgroundColor = [UIColor whiteColor]; 
    myTextView = [[UITextView alloc] initWithFrame:self.view.bounds];//设置该文行文本框边框与整个手机视图相匹配 
    myTextView.text = @"Some text here...";//内容 
    myTextView.font = [UIFont systemFontOfSize:16.0f];//字体样式 
    [self.view addSubview:myTextView];//添加视图 

@synthesize myTextView;

- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    myTextView = [[UITextView alloc] initWithFrame:self.view.bounds];//设置该文行文本框边框与整个手机视图相匹配
    myTextView.text = @"Some text here...";//内容
    myTextView.font = [UIFont systemFontOfSize:16.0f];//字体样式
    [self.view addSubview:myTextView];//添加视图
}
ZYUITextViewViewController.h:

 

[plain]  #import <UIKit/UIKit.h> 
 
@interface ZYUITextViewViewController : UIViewController 
 
@property(nonatomic,strong) UITextView *myTextView; 
 
@end 

#import <UIKit/UIKit.h>

@interface ZYUITextViewViewController : UIViewController

@property(nonatomic,strong) UITextView *myTextView;

@end
ZYUITextViewViewController.m:


[plain]  (void)viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear:animated]; 
    [[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]; 
    myTextView = [[UITextView alloc] initWithFrame:self.view.bounds];//设置该文行文本框边框与整个手机视图相匹配 
    myTextView.text = @"handleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHide";//内容 
    myTextView.font = [UIFont systemFontOfSize:16.0f];//字体样式 
    [self.view addSubview:myTextView];//添加视图 

 
-(void)handleKeyboardDidShow:(NSNotification *)paramNotifation{ 
    //paramNotifation userInfo 返回接受的用户信息字典。UIKeyboardFrameEndUserInfoKey 获得键盘结束时候的位置 
    NSValue *keyboardRectAsObject = [[paramNotifation userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]; 
    CGRect keyboardRect; 
    [keyboardRectAsObject getValue:&keyboardRect];//获取键盘控件大小 
    myTextView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, keyboardRect.size.height, 0.0f); 

 
-(void)handleKeyboardWillHide:(NSNotification *)paramNotification{ 
    self.myTextView.contentInset = UIEdgeInsetsZero; 

 
- (void)viewDidLoad 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

 
-(void)viewWillDisappear:(BOOL)animated{ 
    [super viewWillDisappear:animated]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self];//删除通知中心所有的信息 

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[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];
    myTextView = [[UITextView alloc] initWithFrame:self.view.bounds];//设置该文行文本框边框与整个手机视图相匹配
    myTextView.text = @"handleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHidehandleKeyboardWillHide";//内容
    myTextView.font = [UIFont systemFontOfSize:16.0f];//字体样式
    [self.view addSubview:myTextView];//添加视图
}

-(void)handleKeyboardDidShow:(NSNotification *)paramNotifation{
    //paramNotifation userInfo 返回接受的用户信息字典。UIKeyboardFrameEndUserInfoKey 获得键盘结束时候的位置
    NSValue *keyboardRectAsObject = [[paramNotifation userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect;
    [keyboardRectAsObject getValue:&keyboardRect];//获取键盘控件大小
    myTextView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, keyboardRect.size.height, 0.0f);
}

-(void)handleKeyboardWillHide:(NSNotification *)paramNotification{
    self.myTextView.contentInset = UIEdgeInsetsZero;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view.
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];//删除通知中心所有的信息
}
运行结果:

 


 

\
补充:移动开发 , IOS ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,