//
// ViewController.m
// UITextView
//
// Created by HarrySun on 16/7/12.
// Copyright © 2016年 Mobby. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UITextViewDelegate>
@property (nonatomic,strong) UITextView *textView;
@end
@implementation ViewController
//动画时间
#define kAnimationDuration 0.2
//view高度
#define kViewHeight 56
- (void)viewWillAppear:(BOOL)animated{
//添加键盘的监听事件
//注册通知,监听键盘弹出事件
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardDidShow:)name:UIKeyboardDidShowNotificationobject:nil];
//注册通知,监听键盘消失事件
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardDidHidden)name:UIKeyboardDidHideNotificationobject:nil];
}
// 键盘弹出时
-(void)keyboardDidShow:(NSNotification *)notification
{
//获取键盘高度
NSValue *keyboardObject = [[notificationuserInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect;
[keyboardObject getValue:&keyboardRect];
//调整放置有textView的view的位置
//设置动画
[UIViewbeginAnimations:nilcontext:nil];
//定义动画时间
[UIViewsetAnimationDuration:kAnimationDuration];
//设置view的frame,往上平移
[(UIView *)[self.viewviewWithTag:1000]setFrame:CGRectMake(0,self.view.frame.size.height-keyboardRect.size.height-100,self.view.bounds.size.width,100)];
[UIViewcommitAnimations];
}
//键盘消失时
-(void)keyboardDidHidden
{
//定义动画
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationDuration:kAnimationDuration];
//设置view的frame,往下平移
[(UIView *)[self.viewviewWithTag:1000]setFrame:CGRectMake(0,self.view.frame.size.height-100,self.view.bounds.size.width,100)];
[UIViewcommitAnimations];
}
- (void)viewDidLoad {
[superviewDidLoad];
self.textView= [[UITextViewalloc] initWithFrame:CGRectMake(0,self.view.bounds.size.height - 140, self.view.bounds.size.width,100)];
self.textView.textColor = [UIColorblackColor];// 设置textView里面的字体颜色
self.textView.font = [UIFontfontWithName:@"Arial"size:18.0]; // 设置字体名称和字体大小
self.textView.delegate =self; //设置它的委托方法
self.textView.backgroundColor = [UIColor grayColor];
self.textView.tag =1000;
self.textView.text = @"Now is the time for all good developers to come to serve their country.\n\nNow is the time for all good developers to come to serve their country.";// 设置它显示的内容
self.textView.returnKeyType = UIReturnKeyDefault; // 返回键的类型
/*
return键有以下几种样式:
enum {
UIReturnKeyDefault, 默认,灰色按钮,标有Return
UIReturnKeyGo, 标有Go的蓝色按钮
UIReturnKeyGoogle, 标有Google的蓝色按钮,用于搜索
UIReturnKeyJoin, 标有Join的蓝色按钮
UIReturnKeyNext, 标有Next的蓝色按钮
UIReturnKeyRoute, 标有Route的蓝色按钮
UIReturnKeySearch, 标有Search的蓝色按钮
UIReturnKeySend, 标有Send的蓝色按钮
UIReturnKeyYahoo, 标有Yahoo的蓝色按钮
UIReturnKeyYahoo, 标有Yahoo的蓝色按钮
UIReturnKeyEmergencyCall, 紧急呼叫按钮
} UIReturnKeyType;
*/
self.textView.keyboardType = UIKeyboardTypeDefault;// 键盘类型
/*
键盘样式有以下几种:
enum {
UIKeyboardTypeDefault, 默认键盘,支持所有字符
UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘
UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符
UIKeyboardTypeURL, 只支持URL字符的URL键盘,支持.com按钮
UIKeyboardTypeNumberPad, 数字键盘
UIKeyboardTypePhonePad, 电话键盘
UIKeyboardTypeNamePhonePad, 支持输入人名的电话键盘
UIKeyboardTypeEmailAddress, 电子邮件键盘
UIKeyboardTypeDecimalPad, 有数字和小数点的数字键盘
UIKeyboardTypeTwitter, 优化的键盘,方便输入@、#字符
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
} UIKeyboardType;
*/
self.textView.scrollEnabled = YES; // 是否可以拖动
self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight; // 自适应高度
self.textView.editable =YES; //是否允许编辑内容
self.textView.textAlignment = NSTextAlignmentLeft; // 文本显示的位置默认为居左
/*
iOS7中文本对齐方式有以下几种:
enum {
NSTextAlignmentLeft = 0, 左对齐,默认
NSTextAlignmentCenter = 1, 居中对齐
NSTextAlignmentRight = 2, 右对齐
NSTextAlignmentJustified = 3, 在一个段落的最后一行自然对齐
NSTextAlignmentNatural = 4, 默认对齐方式
} NSTextAlignment;
*/
self.textView.dataDetectorTypes = UIDataDetectorTypeAll; // 显示数据类型的连接模式(如电话号码、网址、地址等)
self.textView.autocorrectionType = UITextAutocorrectionTypeNo; // 设置自动纠错
/*
// 自动纠错方式有以下几种:
enum {
UITextAutocorrectionTypeDefault, 默认
UITextAutocorrectionTypeNo, 不自动纠错
UITextAutocorrectionTypeYes, 自动纠错
} UITextAutocorrectionType;
*/
self.textView.autocapitalizationType = UITextAutocapitalizationTypeNone; // 设置自动大写方式
/*
自动大写方式有以下几种:
enum {
UITextAutocapitalizationTypeNone, 不自动大写
UITextAutocapitalizationTypeWords, 单词首字母大写
UITextAutocapitalizationTypeSentences, 句子的首字母大写
UITextAutocapitalizationTypeAllCharacters, 所有字母都大写
} UITextAutocapitalizationType;
*/
[self.textView.textStorageaddAttribute:NSForegroundColorAttributeNamevalue:[UIColorredColor] range:NSMakeRange(0,3)]; // 将前三个字符的字体改为红色
self.textView.layer.cornerRadius = 10; // 边框圆角半径
self.textView.layer.masksToBounds = YES; // 是否去除多余部分
self.textView.layer.borderWidth = 1; // 边框
[self.viewaddSubview:self.textView];
//定义一个toolBar
UIToolbar * topView = [[UIToolbaralloc]initWithFrame:CGRectMake(0,0, self.view.bounds.size.width,40)];
//设置style
[topView setBarStyle:UIBarStyleBlack];
//定义两个flexibleSpace的button,放在toolBar上,这样完成按钮就会在最右边
UIBarButtonItem * button1 =[[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpacetarget:selfaction:nil];
UIBarButtonItem * button2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpacetarget:selfaction:nil];
//定义完成按钮
UIBarButtonItem * doneButton = [[UIBarButtonItemalloc]initWithTitle:@"完成"style:UIBarButtonItemStyleDone target:selfaction:@selector(resignKeyboard)];
//在toolBar上加上这些按钮
NSArray * buttonsArray = [NSArrayarrayWithObjects:button1,button2,doneButton,nil];
[topView setItems:buttonsArray];
[_textViewsetInputAccessoryView:topView];
}
// 隐藏键盘
- (void)resignKeyboard{
[self.textViewresignFirstResponder];
}
// 将要开始编辑
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
NSLog(@"将要开始编辑");
returnYES;
}
// 将要结束编辑
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
NSLog(@"将要结束编辑");
returnYES;
}
// 开始编辑
- (void)textViewDidBeginEditing:(UITextView *)textView{
NSLog(@"开始编辑");
}
// 结束编辑
- (void)textViewDidEndEditing:(UITextView *)textView{
NSLog(@"结束编辑");
}
// 内容将要发生改变编辑限制输入文本长度
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
NSLog(@"将要改变内容");
if (range.location <240) {
returnYES;
}
returnNO;
}
// 内容发生改变编辑
- (void)textViewDidChange:(UITextView *)textView{
NSLog(@"内容发生改变编辑");
}
//焦点发生改变
- (void)textViewDidChangeSelection:(UITextView *)textView{
NSLog(@"选中内容");
}
- (void)dealloc{
// [[NSNotificationCenter defaultCenter] removeObserver:self];
// 或者
[[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardDidShowNotificationobject:nil];
[[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardDidHideNotificationobject:nil];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end