iOS开发UItextview常用属性方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS开发UItextview常用属性方法相关的知识,希望对你有一定的参考价值。
//
// ViewController.m
// TextViewAll
#import "ViewController.h"
@interface ViewController ()<UITextViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
UITextView *myTextView = [[UITextView alloc]initWithFrame:CGRectMake(10, 50, [UIScreen mainScreen].bounds.size.width - 20, 200)];
//设置背景色
myTextView.backgroundColor = [UIColor brownColor];
//设置初始文本
myTextView.text = @"生活在于折腾,生活在于折腾,生活在于折腾,生活在于折腾,生活在于折腾 www.baidu.com";
//设置文字大小
myTextView.font = [UIFont systemFontOfSize:15.0];
// myTextView.textAlignment = 1;
// NSTextAlignmentLeft = 0, // 左对齐
// NSTextAlignmentCenter = 1, // 居中对齐
// NSTextAlignmentRight = 2, // 右对齐
//是否可以编辑
myTextView.editable = YES; // 默认YES
myTextView.selectable = YES; // 默认YES 当设置为NO时,不能选择
//替换键盘,常用语自定义键盘
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(100, 50, 100, 100)];
view.backgroundColor = [UIColor redColor];
// myTextView.inputView = view;
//在键盘上面添加一个紧贴着键盘的view,常用于 确定 OR 取消 按钮
UIView * viewSecond = [[UIView alloc] initWithFrame:CGRectMake(100, 50, 100, 50)];
viewSecond.backgroundColor = [UIColor cyanColor];
// myTextView.inputAccessoryView = viewSecond;
//获取焦点后,自动全选文本内容
// myTextView.clearsOnInsertion = YES; // 默认为NO
//文本与边框的距离(上,左,下右)
myTextView.textContainerInset = UIEdgeInsetsMake(20, 0, 50, 100);
myTextView.dataDetectorTypes = UIDataDetectorTypeAll;
/*UIDataDetectorTypeAll可以检测检测电话、网址和邮箱。符合条件的文本中的内容就会高亮
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
*/
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"这是一个链接:www.123456.com"];
[attributedString addAttribute:NSLinkAttributeName
value:@"url1://www.baidu.com"
range:NSMakeRange(7, 14)];
NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor],
NSUnderlineColorAttributeName: [UIColor lightGrayColor],
NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};
myTextView.linkTextAttributes = linkAttributes;
myTextView.attributedText = attributedString;
myTextView.delegate = self;
myTextView.editable = NO; // 可编辑状态不能点击链接
[self.view addSubview:myTextView];
}
// 要实现代理
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
if ([[URL scheme] isEqualToString:@"url1"]) {
NSString * url = [URL host];
NSLog(@"%@",url);
return NO;
}
return YES;
}
// 将要开始编辑
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
return YES;
}
// 将要结束编辑
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
return YES;
}
// 开始编辑
- (void)textViewDidBeginEditing:(UITextView *)textView
{
}
// 结束编辑
- (void)textViewDidEndEditing:(UITextView *)textView
{
}
// 文本将要改变
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
return YES;
}
// 文本发生改变
- (void)textViewDidChange:(UITextView *)textView
{
}
// 焦点发生改变
- (void)textViewDidChangeSelection:(UITextView *)textView
{
}
// 是否允许对文本中的富文本进行操作
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0){
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
以上是关于iOS开发UItextview常用属性方法的主要内容,如果未能解决你的问题,请参考以下文章