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的属性没写

时间: 2025-01-08 05:46:47

UIKit 框架之UITextView的相关文章

iOS UIKit 框架 346 篇文档分类整理 - 预告

iOS UIKit 框架 346 篇文档分类整理 - 预告 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 当前正在进行的是 "iOS Foundation 框架 224 篇相关文档分

UIKit 框架之UIView二

下面这些都是UIView一些基本的东西,具体的可以参考UIKit 框架之UIView一博客 一.自定义一个View // // MyView.m // UIView // // Created by cyw on 15-5-17. // Copyright (c) 2015年 cyw. All rights reserved. // #import "MyView.h" @implementation MyView - (id)initWithFrame:(CGRect)frame {

UIKit 框架之Bar、Controller

UIKit框架中有各种Bar,UITabBar.UINavigationBar.UIToolbar.Bar对应的就有一些Item,tabBarItem.navigationItem.toolbarItems,再加上UIViewController.UINavigationController.UITabBarController很容易搞糊涂.我看了好久,没看明白.动手敲了下才有一点感觉. 一.联系 一个UINavigationController对应着一个UINavigationBar.UITo

UIKit框架类层次图

学习UIKit应该首选了解UIKit类的层次图,从根类一层一层的拨.

UIKit 框架之UICollectionViewController

1.自定义单元格 #import <UIKit/UIKit.h> @interface myCollectionViewCell : UICollectionViewCell @property(nonatomic,strong) UIImageView *myImageView; @property(nonatomic,strong) UILabel *nameLabel; @end #import "myCollectionViewCell.h" @implementa

UIKit 框架之UIView一

- (id)initWithFrame:(CGRect)aRect //通过一个矩形对象初始化 Configuring a View's Visual Appearance //配置视觉展示 @property(nonatomic, copy) UIColor *backgroundColor //设置背景色 @property(nonatomic, getter=isHidden) BOOL hidden //隐藏view,默认为NO @property(nonatomic) CGFloat

UIKit 框架之UICollectionView

1.自定义UICollectionViewCell 在myCollectionViewCell.h中声明两个属性 // // myCollectionViewCell.h // UICollectionView // // Created by City--Online on 15/5/25. // Copyright (c) 2015年 XQB. All rights reserved. // #import <UIKit/UIKit.h> @interface myCollectionVi

UIKit 框架之UITextField

// // ViewController.m // UITextField // // Created by City--Online on 15/5/20. // Copyright (c) 2015年 XQB. All rights reserved. // #import "ViewController.h" @interface ViewController ()<UITextFieldDelegate> @property(nonatomic,strong) UI

UIKit框架之NSObject

首先学习NSObject  // // ViewController.m // localization // // Created by City--Online on 15/5/15. // Copyright (c) 2015年 XQB. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (voi