iOS基本控件-UITextView 文本视图

/*
                **UITextView
介绍 **
    
     ** UITextView
是文本视图空间,继承自 UIScrollView 用于显示或者输入一行或多行文字
     ** UITextView
UITextField 是有区别的
    
                **UITextView
特点 **
    
     ** UITextView
可以作显示文字或者输入,如果显示内容或者输入内容超过 TextView.frame.size.Width
       
时会自动换行,且当显示内容或输入内容超过 Text.frame.size.height 时空间还可以上下移动
    
                **UITextView
常用属性 **
    
     ** text -
文字内容
    
     ** textColor -
文字颜色
    
     ** textAligment -
文字对齐方式
    
     ** selectdRange -
控制滚动
    
     ** editable -
是否可编辑
    
    
                **UITextView
常用方法 **
    
     ** -(void)scrollRangeToVisible:(NSRange)range;
    
     ** -(BOOL)textViewShouldBeginEditing:(UITextView*)textView;
    
     ** -(BOOL)textViewShouldEndEditing:(UITextView *)textView;
    
     ** -(void)textViewDidBeginEditing:(UITextView *)textView;
    
     ** -(void)textViewDidEndEditing:(UITextView *)textView;
    
     ** -(BOOL)textView:(UITextView *)textViewshouldChangeTextInRange:(NSRange)range
            replacementText:(NSString *)text;
    
     ** -(void)textViewDidChange:(UITextView *)textView;
    
     ** -(void)textViewDidChangeSelection:(UITextView *)textView;
    
     */

   
    
{
   
UITextView *_textView;
   
UITextField *_textField;
    }


   
_textView = [[ UITextView alloc ] init ];
   
_textView . frame = CGRectMake (10, 30, 300, 100);
   
   
// 输入尽量多的文字,把字号变大,来验证超出宽高后的效果
   
_textView . text = @"//  Created by student on 14-10-6.Copyright (c) 2014 wangxuesen. All rights reserved." ;
   
_textView . font = [ UIFont boldSystemFontOfSize :30];
   
   
   
// 设置是否可编辑, NO 时只能 copy ,(比如显示小说的时候)
   
_textView . editable = YES ;
   
// 设置代理
   
_textView . delegate = self ;
    [
self . window addSubview : _textView ];
   
   
   
   
_textField = [[ UITextField alloc ] init ];
   
_textField . frame = CGRectMake (10, 160, 200, 50);
   
_textField . borderStyle = UITextBorderStyleRoundedRect ;
    [
self . window addSubview : _textField ];
   
   
   
UIButton *btn = [ UIButton buttonWithType : UIButtonTypeSystem ];
    btn.
frame = CGRectMake (200, 160, 50, 50);
    [btn
setTitle : @" 发送 " forState : UIControlStateNormal ];
   
// 给按钮设置点击事件
   
// 事件里的功能是把在 textField 里边输入的文字加入到 textView 中的文字后边
    [btn
addTarget : self action : @selector (btnClick) forControlEvents : UIControlEventTouchUpInside ];
    [
self . window addSubview :btn];




// UITextViewDelegate 协议中的三个方法
// textView 中的 text 内容是否允许改变
- (
BOOL )textView:( UITextView *)textView shouldChangeTextInRange:( NSRange )range replacementText:( NSString *)text
{
   
return YES ;
}
// textView 中的 text 发生改变的时候就会被触发
- (
void )textViewDidChange:( UITextView *)textView
{
   
NSLog ( @"DidChange" );
}
// textView 进入编辑模式的时候就会被触发
- (
void )textViewDidChangeSelection:( UITextView *)textView
{
   
NSLog ( @"DidChangeSelection" );
}



//发送按钮点击触发事件
-(
void )btnClick
{
   
// 只写这一条,文字添加后,必须手动滑动到最后去查看
   
_textView . text = [ _textView . text stringByAppendingFormat : @"\n%@" , _textField . text ];
   
   
// 我们让添加后的 textView 中的文字自动滚动到最下边, ...iOS7.0 没效果
   
NSUInteger length = _textView . text . length ;
   
_textView . selectedRange = NSMakeRange (length,0);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值