UITextView
2015-12-04 09:02
666 查看
1、基本用法:显示文本信息(不可编辑)
UITextView * textView = [[UITextView alloc] init];
textView.frame = self.view.bounds;
textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
textView.editable = NO;//默认可编辑,设置为不可编辑
textView.textColor = [UIColor orangeColor];
textView.backgroundColor = [UIColor lightGrayColor];
textView.font = [UIFont systemFontOfSize:20];
NSMutableString * str = [[NSMutableString alloc] init];
for (int i = 0; i < 100; i++) {
[str appendString:[NSString stringWithFormat:@"这是第%i行\n", i]];
}
textView.text = str;
[self.view addSubview:textView];如果文本太多,超出屏幕,则会有滚动条:
2、可编辑状态
_textView.editable = YES;
当出现键盘时,会遮挡最下面部分:
对于这种,应该在键盘跳出和隐藏的时候,适当调整UITextView的尺寸,具体做法如下:
#import "ViewController.h"
@interface ViewController () <UITextViewDelegate>
{
UITextView * _textView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
_textView = [[UITextView alloc] init];
_textView.frame = self.view.bounds;
_textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
_textView.editable = YES;//设置是否可编辑
_textView.textColor = [UIColor orangeColor];
_textView.backgroundColor = [UIColor lightGrayColor];
_textView.font = [UIFont systemFontOfSize:20];
_textView.delegate = self;
NSMutableString * str = [[NSMutableString alloc] init];
for (int i = 0; i <= 100; i++) {
[str appendString:[NSString stringWithFormat:@"这是第%i行\n", i]];
}
NSRange range = NSMakeRange(str.length - 2, 2);
//去掉最后一行后面的\n,否则会空一行
[str deleteCharactersInRange:range];
_textView.text = str;
[self.view addSubview:_textView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardHide:) name:UIKeyboardDidHideNotification object:nil];
}
- (void) keyBoardShow:(NSNotification *) notification {
NSDictionary* info = [notification userInfo];
//kbSize即為鍵盤尺寸 (有width, height)
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//得到鍵盤的高度
NSLog(@"keyboard_height:%f",kbSize.height);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
CGRect textViewFrame = _textView.frame;
CGRect newTextViewFrame = CGRectMake(textViewFrame.origin.x, textViewFrame.origin.y, textViewFrame.size.width, textViewFrame.size.height - kbSize.height);
_textView.frame = newTextViewFrame;
[UIView commitAnimations];
}
- (void) keyBoardHide:(NSNotification *) notification {
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
//或者在此处改变UITextView的frame
}
3、
- (BOOL)hasText;
判断有无文本
设置文本对齐方式
@property(nonatomic) NSTextAlignment textAlignment; // default is NSLeftTextAlignment
常见的有这三种:(居左,居中,居右)
NSTextAlignmentLeft
NSTextAlignmentCenter
NSTextAlignmentRight
取得文本的选择范围:
- (void)textViewDidChangeSelection:(UITextView *)textView {
//此函数实时监测文本的选择情况
NSLog(@"%@", NSStringFromRange(textView.selectedRange));
}
2015-12-04 10:03:55.778 07-UITextView[1218:27730] {1, 8}
滚动位置控制:
[_textView scrollRangeToVisible:NSMakeRange(1, 1)];
执行此代码后,将立即滚动到NSRange所包含的字符的位置
4、URL与电话号码的链接显示
typedef NS_OPTIONS(NSUInteger, UIDataDetectorTypes) {
//自动检测电话号码
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.editable = NO;然后:
点击网址:
5、 UITextView状态监视:
@protocol UITextViewDelegate <NSObject, UIScrollViewDelegate>
@optional
//编辑开始前被调用,如果返回NO,编辑将不会开始
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
//编辑结束前被调用,如果返回NO,编辑将不会结束
- (BOOL)textViewShouldEndEditing:(UITextView *)textView;
//编辑开始后被调用(UITextView变为第一响应者时调用)
- (void)textViewDidBeginEditing:(UITextView *)textView;
//编辑结束后被调用(UITextView失去第一响应者时被调用)
- (void)textViewDidEndEditing:(UITextView *)textView;
//改变文本前被调用,range中是变化范围,text中是变化后的字符串,如果返回NO,变化将不会被响应
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
//文本变更时被调用
- (void)textViewDidChange:(UITextView *)textView;
//游标移动,选择范围发生变化是被调用
- (void)textViewDidChangeSelection:(UITextView *)textView;
//点击网址时被调用
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0);
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0);
@end
6、图文混排
CGRect rect = CGRectMake(100, 100, 100, 100);
UIView * view1 = [[UIView alloc] initWithFrame:rect];
view1.backgroundColor = [UIColor redColor];
[self.view addSubview:view1];
//设置环绕的路径
UIBezierPath * path = [UIBezierPath bezierPathWithRect:rect];
_textView.textContainer.exclusionPaths = @[path];
红色区域可以放置图片,或者其他UIView
UITextView * textView = [[UITextView alloc] init];
textView.frame = self.view.bounds;
textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
textView.editable = NO;//默认可编辑,设置为不可编辑
textView.textColor = [UIColor orangeColor];
textView.backgroundColor = [UIColor lightGrayColor];
textView.font = [UIFont systemFontOfSize:20];
NSMutableString * str = [[NSMutableString alloc] init];
for (int i = 0; i < 100; i++) {
[str appendString:[NSString stringWithFormat:@"这是第%i行\n", i]];
}
textView.text = str;
[self.view addSubview:textView];如果文本太多,超出屏幕,则会有滚动条:
2、可编辑状态
_textView.editable = YES;
当出现键盘时,会遮挡最下面部分:
对于这种,应该在键盘跳出和隐藏的时候,适当调整UITextView的尺寸,具体做法如下:
#import "ViewController.h"
@interface ViewController () <UITextViewDelegate>
{
UITextView * _textView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
_textView = [[UITextView alloc] init];
_textView.frame = self.view.bounds;
_textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
_textView.editable = YES;//设置是否可编辑
_textView.textColor = [UIColor orangeColor];
_textView.backgroundColor = [UIColor lightGrayColor];
_textView.font = [UIFont systemFontOfSize:20];
_textView.delegate = self;
NSMutableString * str = [[NSMutableString alloc] init];
for (int i = 0; i <= 100; i++) {
[str appendString:[NSString stringWithFormat:@"这是第%i行\n", i]];
}
NSRange range = NSMakeRange(str.length - 2, 2);
//去掉最后一行后面的\n,否则会空一行
[str deleteCharactersInRange:range];
_textView.text = str;
[self.view addSubview:_textView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardHide:) name:UIKeyboardDidHideNotification object:nil];
}
- (void) keyBoardShow:(NSNotification *) notification {
NSDictionary* info = [notification userInfo];
//kbSize即為鍵盤尺寸 (有width, height)
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//得到鍵盤的高度
NSLog(@"keyboard_height:%f",kbSize.height);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
CGRect textViewFrame = _textView.frame;
CGRect newTextViewFrame = CGRectMake(textViewFrame.origin.x, textViewFrame.origin.y, textViewFrame.size.width, textViewFrame.size.height - kbSize.height);
_textView.frame = newTextViewFrame;
[UIView commitAnimations];
}
- (void) keyBoardHide:(NSNotification *) notification {
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
//或者在此处改变UITextView的frame
}
3、
- (BOOL)hasText;
判断有无文本
设置文本对齐方式
@property(nonatomic) NSTextAlignment textAlignment; // default is NSLeftTextAlignment
常见的有这三种:(居左,居中,居右)
NSTextAlignmentLeft
NSTextAlignmentCenter
NSTextAlignmentRight
取得文本的选择范围:
- (void)textViewDidChangeSelection:(UITextView *)textView {
//此函数实时监测文本的选择情况
NSLog(@"%@", NSStringFromRange(textView.selectedRange));
}
2015-12-04 10:03:55.778 07-UITextView[1218:27730] {1, 8}
滚动位置控制:
[_textView scrollRangeToVisible:NSMakeRange(1, 1)];
执行此代码后,将立即滚动到NSRange所包含的字符的位置
4、URL与电话号码的链接显示
typedef NS_OPTIONS(NSUInteger, UIDataDetectorTypes) {
//自动检测电话号码
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.editable = NO;然后:
_textView.dataDetectorTypes = UIDataDetectorTypeAll;
点击网址:
5、 UITextView状态监视:
@protocol UITextViewDelegate <NSObject, UIScrollViewDelegate>
@optional
//编辑开始前被调用,如果返回NO,编辑将不会开始
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
//编辑结束前被调用,如果返回NO,编辑将不会结束
- (BOOL)textViewShouldEndEditing:(UITextView *)textView;
//编辑开始后被调用(UITextView变为第一响应者时调用)
- (void)textViewDidBeginEditing:(UITextView *)textView;
//编辑结束后被调用(UITextView失去第一响应者时被调用)
- (void)textViewDidEndEditing:(UITextView *)textView;
//改变文本前被调用,range中是变化范围,text中是变化后的字符串,如果返回NO,变化将不会被响应
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
//文本变更时被调用
- (void)textViewDidChange:(UITextView *)textView;
//游标移动,选择范围发生变化是被调用
- (void)textViewDidChangeSelection:(UITextView *)textView;
//点击网址时被调用
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0);
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0);
@end
6、图文混排
CGRect rect = CGRectMake(100, 100, 100, 100);
UIView * view1 = [[UIView alloc] initWithFrame:rect];
view1.backgroundColor = [UIColor redColor];
[self.view addSubview:view1];
//设置环绕的路径
UIBezierPath * path = [UIBezierPath bezierPathWithRect:rect];
_textView.textContainer.exclusionPaths = @[path];
红色区域可以放置图片,或者其他UIView
相关文章推荐
- Swift社交应用文本输入优化汇总
- 自定义消息输入框
- IOS中UITextView控件的一些使用方法
- 关于UITextView的textViewDidChange回调没有调用的问题解决
- 关于UITextView的textViewDidChange回调没有调用的问题解决
- UITextView圆角黑色边框 一圈
- IOS基本控件常用属性及方法(二)---UITextView
- IOS之导航栏中添加UITextView控件bug
- iOS —— UITextView实现Placehodler属性
- UITextView
- UITextView属性
- ios学习笔记-Lecture5:UITextView
- iOS中UITextView的默认输入居中,而非顶部的解决办法
- textview的自定义实现
- UITextView
- 一起了解一下 UITextView 的使用
- 对文本框内容进行实时判断
- iOS:去掉UITextView的Padding和Margin
- text view小结
- iOS-UITextView 的收起键盘及动态大小