您的位置:首页 > 其它

文章标题

2015-12-29 16:43 295 查看
UITextView详解

今天重新看了一下UITextView,发现很多系统方法不懂,于是便有了下面的内容

创建

UITextView * textView = [[UITextView alloc] initWithFrame:[UIScreen mainScreen].bounds];
/*
1、设置文本属性:text;
2、设置文字对齐方式属性:textAlignment;
3、设置文字颜色属性:textColor;
4、设置文字字体属性:font;
5、设置编辑使能属性:editable;
6、设置背景颜色属性:backgroundColor;
7、圆角边框:layer.cornerRadius、layer.borderWidth。
*/
textView.backgroundColor = [UIColor blueColor];
textView.text = @"aaasddddddddddd";
textView.font = [UIFont systemFontOfSize:50];
textView.textAlignment = NSTextAlignmentCenter;
textView.editable = YES;
textView.selectable = YES;//里面的内容是否可以交互,例如网址
/*
UIDataDetectorTypePhoneNumber     // 手机
UIDataDetectorTypeLink        // URL
UIDataDetectorTypeAddress  // 街道地址
UIDataDetectorTypeCalendarEvent  // 事件
UIDataDetectorTypeNone          = 0,
UIDataDetectorTypeAll
*/
textView.dataDetectorTypes = UIDataDetectorTypeAll;
textView.allowsEditingTextAttributes = YES;//允许编辑文本属性
textView.typingAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:50]};
/*
UIKeyboardTypeDefault,
UIKeyboardTypeASCIICapable,     ASCII
UIKeyboardTypeNumbersAndPunctuation,  //数字和符号
UIKeyboardTypeURL,                    //  URL
UIKeyboardTypeNumberPad,              // 纯数字
UIKeyboardTypePhonePad,               //(1-9, *, 0, #, with letters under the numbers).
UIKeyboardTypeNamePhonePad,           // 数字跟字母
UIKeyboardTypeEmailAddress,           // email
UIKeyboardTypeDecimalPad  // 数字跟.
UIKeyboardTypeTwitter // A type optimized for twitter text entry (easy access to @ #)
UIKeyboardTypeWebSearch // A default keyboard type with URL-oriented addition (shows space . prominently).
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated
*/
textView.keyboardType = UIKeyboardTypeWebSearch;
/*
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
UIReturnKeyContinue NS_ENUM_AVAILABLE_IOS(9_0),
*/
textView.returnKeyType = UIReturnKeyGo;
textView.layer.cornerRadius = 10;//圆角
textView.layer.borderWidth = 3;//边框
// 7、圆角边框:layer.cornerRadius、layer.borderWidth。
[self.view addSubview:textView];


这里基本所有的系统方法都有了,欢迎补充

这里是代理方法

/*
1、将要开始编辑:textViewShouldBeginEditing;
2、将要完成编辑:textViewShouldEndEditing;
3、开始编辑:textViewDidBeginEditing;
4、完成编辑:textViewDidEndEditing;
3、将要改变内容:shouldChangeTextInRange;
4、内容完成改变:textViewDidChange;
5、内容被选中:textViewDidChangeSelection。
*/
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{

return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{

return YES;
}

- (void)textViewDidBeginEditing:(UITextView *)textView{

// 3、开始编辑:textViewDidBeginEditing;

}
- (void)textViewDidEndEditing:(UITextView *)textView{

// 4、完成编辑:textViewDidEndEditing;

}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{

// 3、将要改变内容:shouldChangeTextInRange;
return YES;
}
- (void)textViewDidChange:(UITextView *)textView{

// 4、内容完成改变:textViewDidChange;

}

- (void)textViewDidChangeSelection:(UITextView *)textView{

//5、内容被选中:textViewDidChangeSelection。

}

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0){

return YES;//url

}
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0){

return YES;//文本副本
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: