码迷,mamicode.com
首页 > 其他好文 > 详细

UITextView

时间:2018-11-13 02:33:38      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:last   ack   efault   phone   代码   内容   rect   objective   beginedit   

简介

  • UITextView 具有 label 大部分属性,以及 textField 的属性。

1、UITextView 的创建

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 30, self.view.frame.size.width - 20, 400)];

// 将 textView 添加到 view
[self.view addSubview:textView];

2、UITextView 的设置

// 设置背景颜色
textView.backgroundColor = [UIColor lightGrayColor];

// 设置文本
/*
由于 UITextView 具有滚动功能,当 text 文本内容较多时可以实现滚动阅读和编辑
*/
textView.text = @"写代码快乐\n15911110524\n写代码快乐\nhttps://www.baidu.com\n写代码快乐";

// 设置对齐方式
/*
NSTextAlignmentLeft      = 0,    // Visually left aligned
NSTextAlignmentCenter    = 1,    // Visually centered
NSTextAlignmentRight     = 2,    // Visually right aligned
NSTextAlignmentJustified = 3,    // Fully-justified. The last line in a paragraph is natural-aligned.
NSTextAlignmentNatural   = 4,    // Indicates the default alignment for script
*/
textView.textAlignment = NSTextAlignmentLeft;

// 设置文本颜色
textView.textColor = [UIColor redColor];

// 设置字体
textView.font = [UIFont boldSystemFontOfSize:20];

// 对文本中的电话和网址自动加链接 
/*
UIDataDetectorTypePhoneNumber                              = 1 << 0,  // Phone number detection
UIDataDetectorTypeLink                                     = 1 << 1,  // URL detection
UIDataDetectorTypeAddress NS_ENUM_AVAILABLE_IOS(4_0)       = 1 << 2,  // Street address detection
UIDataDetectorTypeCalendarEvent NS_ENUM_AVAILABLE_IOS(4_0) = 1 << 3,  // Event detection
UIDataDetectorTypeNone                                     = 0,       // No detection at all
UIDataDetectorTypeAll                                      = NSUIntegerMax    // All types
*/
textView.dataDetectorTypes = UIDataDetectorTypeAll;

// 禁止编辑
/*
设置为只读,不再能输入内容
*/
textView.editable = NO;

// 禁止选择
/*
禁止选中文本,此时文本也禁止编辑
*/
textView.selectable = NO;

// 禁止滚动
textView.scrollEnabled = NO;

// 获取当前选择的范围
NSRange range = textView.selectedRange;

// 设置可以对选中的文字加粗
/*
选中文字时可以对选中的文字加粗
*/
textView.allowsEditingTextAttributes = YES;

// 设置 textView 的代理,需遵守协议 <UITextViewDelegate>
textView.delegate = self;

3、UITextViewDelegate 的协议方法

  • 需遵守协议 UITextViewDelegate,并设置代理
// 将要开始编辑,编辑开始前被调用
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {

    return YES;
}

// 已经开始编辑,编辑开始后被调用
- (void)textViewDidBeginEditing:(UITextView *)textView {

}

// 将要结束编辑,编辑结束前被调用
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {

    return YES;
}

// 已经结束编辑,编辑结束后被调用
- (void)textViewDidEndEditing:(UITextView *)textView {

    NSLog(@"编辑的内容是 : %@", textView.text);
}

// 文本修改,文本修改前被调用
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    return YES;
}

// 文本变更,文本变更时被调用,每输入一个字符时都会被调用
- (void)textViewDidChange:(UITextView *)textView {

}

// 游标移动,选择范围发生变化时被调用
- (void)textViewDidChangeSelection:(UITextView *)textView {

}

UITextView

标签:last   ack   efault   phone   代码   内容   rect   objective   beginedit   

原文地址:https://www.cnblogs.com/CH520/p/9413469.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!