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

相关文章
|
10月前
|
SQL 存储 算法
比 SQL 快出数量级的大数据计算技术
SQL 是大数据计算中最常用的工具,但在实际应用中,SQL 经常跑得很慢,浪费大量硬件资源。例如,某银行的反洗钱计算在 11 节点的 Vertica 集群上跑了 1.5 小时,而用 SPL 重写后,单机只需 26 秒。类似地,电商漏斗运算和时空碰撞任务在使用 SPL 后,性能也大幅提升。这是因为 SQL 无法写出低复杂度的算法,而 SPL 提供了更强大的数据类型和基础运算,能够实现高效计算。
|
Java 开发者
Java变量命名规则
Java变量命名规则
390 0
|
11月前
|
存储 安全 API
源码解密协程队列和线程队列的实现原理(二)
源码解密协程队列和线程队列的实现原理(二)
103 1
|
算法 搜索推荐 Python
探索Python中的推荐系统:混合推荐模型
探索Python中的推荐系统:混合推荐模型
941 1
|
C++ 计算机视觉 Python
Qt+C++自定义控件仪表盘动画仿真
这篇博客针对< Qt+C++自定义控件仪表盘动画仿真>编写代码,代码整洁,规则,易读。 应用推荐首选。
237 0
|
存储 开发工具 git
Git工作流程:如何在团队中协作?
Git工作流程:如何在团队中协作?
|
C语言
C语言笔试题训练【第一天】
C语言笔试题训练【第一天】
|
算法 数据安全/隐私保护
计算机考研408每日一题 day38
计算机考研408每日一题 day38
150 0
计算机考研408每日一题 day38
|
网络安全
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
253 0
|
4天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1103 0