dd

IOS学习笔记(四)之UITextField和UITextView控件学习

jerry IOS 2015年11月27日 收藏

   (一)前言:

         上一节我们学习了常用的UIButton按钮使用方法,今天在学习一下可以编辑文本的控件分别为:UITextField和UITextView;(学过android的人知道,这两个我们可以和EditText和TextView进行比较);

    

  (二)UITextField:

        1:看了之前的UIView的类继承层次图之后,我们可以很清楚的看到UITextField是继承自UIControll控件.(查看文档UITextField官方文档)

          一个UITextField一个控件显示可编辑文本的控件,当我们按返回键的时候会同时发送一个消息到该目标对象中。我们经常会通过这个控件来获取一些用户的即时输入的文本信息。例如:搜索操作输入的文本信息;

         除了上面的最基本文本编辑功能之外,UITextField还可以在文本的编辑框的边界显示一个额外的小视图,还可以使用自定义的功能视图例如显示:书签按钮或搜索图标,当然这个对象还内置UITextField按钮清除当前文本。

        UITextField还有自己对应的UITextFieldDelegate委托对象,可以帮助我们去处理一些响应事件.

        

       

      2:  UITextField常用的可进行修改的文本属性:        

text property 文本信息
attributedText property 已经设置属性的文本 
placeholder property 占位符
attributedPlaceholder property 已设置属性的占位符
defaultTextAttributes property 默认文本属性
font property 字体
textColor property 字体颜色
textAlignment property 文本对齐
typingAttributes property 风格属性

     


(三)UITextView:

      1:UITextView是继承自UIScrollView可以进行展示和编辑多行的文本;(查看:UITextView官方文档)

       2:我们可以通过initWithFrame方法来创建UITextView,方法如下:

 

- (instancetype)initWithFrame:(CGRect)frame textContainer:(NSTextContainer *)textContainer


  (四) 下面简单创建一下UITextField与UITextView


       

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UITextField *customField=[[UITextField alloc]initWithFrame:CGRectMake(10, 10, 150, 50)];
    customField.text=@"我是UITextView";
    [customField setBackgroundColor:[UIColor yellowColor]];
    
    UITextView *customView=[[UITextView alloc]initWithFrame:CGRectMake(10, 100, 300, 100)];
    customView.text=@"我是UITextView,我是UITextView,我是UITextView,我是UITextView,我是UITextView,我是UITextView,我是UITextView,我是UITextView,我是UITextView,我是UITextView,";
    [customView setBackgroundColor:[UIColor redColor]];
    
    [self.view addSubview:customField];


 


     


dd