UIKit 框架之UITextView

简介:

//
//  ViewController.m
//  UItextView
//
//  Created by City--Online on 15/5/22.
//  Copyright (c) 2015年 XQB. All rights reserved.
//
 
#import "ViewController.h"
 
@interface ViewController ()<UITextViewDelegate>
@property(nonatomic,strong) UITextView *textView;
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    _textView=[[UITextView alloc]initWithFrame:CGRectMake(10, 100, 200, 100)];
    //设置代理
    _textView.delegate=self;
    //设置文本内容
    _textView.text=@"text啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊哈哈哈哈啊哈ahahahhahahahah哈哈哈哈哈哈哈哈哈哈哈哈哈哈text啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊哈哈哈哈啊哈ahahahhahahahah哈哈哈哈哈哈哈哈哈哈哈哈哈哈";
    //设置字体
    _textView.font=[UIFont systemFontOfSize:20];
    //设置字体颜色
    _textView.textColor=[UIColor redColor];
    //设置文本对齐方式
    _textView.textAlignment=NSTextAlignmentRight;
    //设置选择的文本
    _textView.selectedRange=NSMakeRange(0, _textView.text.length-2);
    //设置可编辑
    _textView.editable=YES;
    //设置可选择状态
    _textView.selectable=YES;
    //可以设定使电话号码、网址、电子邮件和符合格式的日期等文字变为链接文字
    _textView.dataDetectorTypes=UIDataDetectorTypePhoneNumber;
    //设置文本是否可编辑
    _textView.allowsEditingTextAttributes=YES;
     
    //设置文本属性样式
    NSMutableAttributedString *attributedString=[[NSMutableAttributedString alloc]initWithString:_textView.text];
    [attributedString addAttributes:@{NSForegroundColorAttributeName:[UIColor blueColor],NSFontAttributeName:[UIFont systemFontOfSize:20]} range:NSMakeRange(0, _textView.text.length)];
    _textView.attributedText=attributedString;
    //设置属性字典
    _textView.typingAttributes=@{NSFontAttributeName:[UIFont systemFontOfSize:30]};
    //设置边框宽度
    _textView.layer.borderWidth=3.0;
    //设置滚动时反弹
    _textView.bounces=NO;
    //设置弹出视图
    _textView.inputView=nil;
    //设置辅助视图
    _textView.inputAccessoryView=nil;
    //设置当插入值时是否清空原有内容  selectedRange范围的
    _textView.clearsOnInsertion=YES;
    //设置链接样式
    _textView.linkTextAttributes=@{NSForegroundColorAttributeName:[UIColor redColor]};
     
    [self.view addSubview:_textView];
    //滚动到指定的Range
//    [_textView scrollRangeToVisible:NSMakeRange(_textView.text.length-10, _textView.text.length)];
     
//    _textView.selectedRange=NSMakeRange(_textView.text.length-10, _textView.text.length);
     
}
// 在TextView获取焦点之前执行
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    NSLog(@"textViewShouldBeginEditing");
    return YES;
}
//在TextView离开焦点之后执行
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    NSLog(@"textViewShouldEndEditing");
    return YES;
}
//在TextView开始编辑时获得焦点
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    NSLog(@"textViewDidBeginEditing");
}
//在TextView结束编辑时
- (void)textViewDidEndEditing:(UITextView *)textView
{
    NSLog(@"textViewDidEndEditing");
}
//每次用户通过键盘输入字符时,在字符显示在text view之 前,textView:shouldChangeCharactersInRange:replacementString方法会被调用。这个方法中可以 方便的定位测试用户输入的字符,并且限制用户输入特定的字符
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSCharacterSet *doneButtonCharacterSet = [NSCharacterSet newlineCharacterSet];
    NSRange replacementTextRange = [text rangeOfCharacterFromSet:doneButtonCharacterSet];
    NSUInteger location = replacementTextRange.location;
    if (textView.text.length + text.length > 140){
        if (location != NSNotFound){
            [textView resignFirstResponder];
        }
        return NO;
    }
    else if (location != NSNotFound){
        [textView resignFirstResponder];
        return NO;
    }  
    return YES;
}
 
- (void)textViewDidChange:(UITextView *)textView
{
    NSLog(@"textViewDidChange");
}
//光标位置改变
- (void)textViewDidChangeSelection:(UITextView *)textView
{
     NSLog(@"textViewDidChangeSelection");
}
 
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
    NSLog(@"shouldInteractWithURL");
 
    return YES;
}
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange
{
    NSLog(@"shouldInteractWithTextAttachment");
    return YES;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
    [super touchesBegan:touches withEvent:event];
    NSLog(@"touchesBegan:withEvent");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
@end


UITextView有几个IOS7的属性没写

相关文章