UITextView

这篇文章只涉及到基本的使用,日后会写一些关于结合TextKit的备忘

基本属性

        let screenSize = UIScreen.mainScreen().bounds.size
let textView = UITextView(frame: CGRectMake(0, 20, screenSize.width, 200))
textView.font = UIFont.systemFontOfSize(20)
textView.selectable = false
textView.scrollEnabled = true
textView.editable = true
textView.textColor = UIColor.whiteColor()
textView.backgroundColor = UIColor.blackColor()
textView.text = "The UITextView class implements the behavior for a scrollable, multiline text region. The class supports the display of text using custom style information and also supports text editing. You typically use a text view to display multiple lines of text, such as when displaying the body of a large text document."
textView.textAlignment = .Center
textView.textContainerInset = UIEdgeInsetsMake(60, 0, 0, 0)
textView.keyboardType = .Default
textView.returnKeyType = .Default
view.addSubview(textView) self.textView = textView
  • font:字体
  • selectable:是否可以选中。
  • scrollEnabled:是否可以滚动。
  • editable:是否可以编辑。
  • textColor:文字颜色。
  • backgroundColor:背景色。
  • text:要显示的文字。
  • textAlignment:文字排版样式。
  • textContainerInset:文字的距离textview的内边距。
  • keyboardType:键盘样式。
  • returnKeyType:return键的样式。

监听通知

        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.textViewDidBeginEdit(_:)), name: UITextViewTextDidBeginEditingNotification, object: textView)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.textViewTextDidChange(_:)), name: UITextViewTextDidChangeNotification, object: textView)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.textViewDidEndEdit(_:)), name: UITextViewTextDidEndEditingNotification, object: textView) func textViewDidBeginEdit(notification: NSNotification) {
print(notification.name)
} func textViewTextDidChange(notification: NSNotification) {
print(notification.object)
} func textViewDidEndEdit(notification: NSNotification) {
print(notification.name)
} deinit{
NSNotificationCenter.defaultCenter().removeObserver(self, name: UITextViewTextDidChangeNotification, object: textView)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UITextViewTextDidEndEditingNotification, object: textView)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UITextViewTextDidBeginEditingNotification, object: textView)
}

代理方法

extension ViewController: UITextViewDelegate {
// 是否应该开始编辑
func textViewShouldBeginEditing(textView: UITextView) -> Bool {
print("textViewShouldBeginEditing")
return true
} // 是否应该停止编辑
func textViewShouldEndEditing(textView: UITextView) -> Bool {
print("textViewShouldEndEditing")
return true
} // 文字视图已经开始编辑
func textViewDidBeginEditing(textView: UITextView) {
print("textViewDidBeginEditing")
} // 文字视图已经停止编辑
func textViewDidEndEditing(textView: UITextView) {
print("textViewDidEndEditing")
} // 文字视图是否允许替换文字,每当有文字要被输入或删除都会先调用这个方法
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if text == "\n" {
textView.resignFirstResponder()
return false
}
return true
} // 文字视图文字已经被替换
func textViewDidChange(textView: UITextView) {
print("textViewDidChange")
} // 每当有一组文字被选中或删除输入、放大镜的移动,都会调用此方法
func textViewDidChangeSelection(textView: UITextView) {
print("textViewDidChangeSelection")
}

UITextView -- 基础备忘的更多相关文章

  1. scala基础备忘

    声明一个变量 声明一个常量 显式指定类型 定义一个main函数 package org.admln.scala class HelloScala { } object HelloScala { def ...

  2. Java Socket基础[备忘]

    1.服务端----Server.java import javax.swing.*; import java.io.*; import java.net.*; import java.awt.*; i ...

  3. ajax基础------备忘

    1:register.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  4. javaweb基础备忘

    Request对象的主要方法有哪些 setAttribute(String name,Object):设置名字为name的request 的参数值 getAttribute(String name): ...

  5. Linux基础之常用基本命令备忘

    Linux基础之常用基本命令备忘 PWD   查询当前所在Linux上的位置 /         根目录 CD(change directory)切换目录  语法 CD /(注意添加空格)   LS ...

  6. Nmap备忘单:从探索到漏洞利用(Part 4)

    这是我们的Nmap备忘单的第四部分(Part 1. Part 2. Part 3).本文中我们将讨论更多东西关于扫描防火墙,IDS / IPS 逃逸,Web服务器渗透测试等.在此之前,我们应该了解一下 ...

  7. HTML5终极备忘大全

    二.文字备忘之标签 HTML5中新增的标签 <article> 定义文章 <aside> 定义页面内容旁边的内容 <audio> 定义声音内容 <canvas ...

  8. [转] HTML5终极备忘大全(图片版+文字版)---张鑫旭

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=1544 一.前言兼图片 ...

  9. Table view 备忘

    Table view 备忘 本篇会以备忘为主,主要是一些基础的代理方法和数据源方法具体的优化好点子会后续跟上. Table view的数据源方法 必须实现的数据源方法 // 返回每一行的cell,可以 ...

随机推荐

  1. 【Linq递归查找系列】

    Linq递归查找: public IEnumerable<MenuInfo> GetTree(int id, IEnumerable<MenuInfo> lst) { var ...

  2. Unicode编码及其实现:UTF-16、UTF-8,and more

    http://blog.csdn.net/thl789/article/details/7506133

  3. HDU4570:Multi-bit Trie(区间DP)

    Problem Description IP lookup is one of the key functions of routers for packets forwarding and clas ...

  4. CVPR 2013 录用论文【待更新】

    完整录用论文官方链接:http://www.pamitc.org/cvpr13/program.php 过段时间CvPaper上面应该会有正文链接 今年有关RGB-D摄像机应用和研究的论文渐多起来了. ...

  5. C#泛型集合—Dictionary<K,V>使用技巧

    转载:http://blog.csdn.net/a125138/article/details/7742022 1.要使用Dictionary集合,需要导入C#泛型命名空间 System.Collec ...

  6. 20160329javaweb之JSP -session入门

    3.Session Session 是一个域 !!作用范围:当前会话范围 !!生命周期: 当程序第一次调用到request.getSession()方法时说明客户端明确的需要用到session此时创建 ...

  7. 深入分析 Java 中的中文编码问题 (文章来自网络)

    许令波,developerWorks 中国网站最佳作者,现就职于淘宝网,是一名 Java 开发工程师.对大型互联网架构设计颇感兴趣,喜欢钻研开源框架的设计原理.有时间将学到的知识整理成文章,也喜欢记录 ...

  8. 多个线程访问url

    多个线程访问url package com.aig.ecompassios.ecard; import java.io.BufferedReader; import java.io.InputStre ...

  9. web页面的生命周期

    1.先上几张原理图着重理解: 现在针对第四副图原理进行解析: 流程: 1.浏览器发送请求 2.服务器软件(IIS)接收,它最终的目的就是为了向客户输出它请求的动态页面生成的html代码. 3.服务器不 ...

  10. 深入理解Java的接口和抽象类 _摘抄

    http://www.cnblogs.com/dolphin0520/p/3811437.html 原文 深入理解Java的接口和抽象类 对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可 ...