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. INSTALL_FAILED_VERSION_DOWNGRADE报错

    error: INSTALL_FAILED_VERSION_DOWNGRADE 原因:模拟器或者实际的device中已经安装了同名的app,且本次的版本并不高 解决:在模拟器中卸载此同名应用,之后再运 ...

  2. 【转】C++里定义全局变量和函数常用方法

    http://blog.csdn.net/niying/article/details/637084 1:在头文件是声明变量,然后在使用的文件中用exten标识. ".h": in ...

  3. Android_life,Intent_note

    生命周期: 从出生到死亡 Activity生命周期的7个方法和3个循环 onCreate() 创建时调用onRestart() 不可见到可见时调用onStart() 用户可见时调用onResume() ...

  4. Linq to SQL只支持SQL Server(所选对象使用不支持的数据提供程序)

  5. android实现倒计时

    前言  在打开爱奇艺等app的欢迎界面的时候,右上角有一个倒计时的控件.倒计时完了以后进入主界面.现在我们来实现这个功能. 方法一 利用java的类Timer,TimerTask还有android的H ...

  6. C语言study一

    之前看了CPP,敲过些代码,但总觉得学得不够系统,书后面的习题比较无聊,不想去写.C主要在学数据结构和理解底层上面很有帮助. 于是又来mooc学习C语言,看了下视频虽然很繁琐啰嗦,但是有些东西我确实以 ...

  7. SQL Server 阻止了对组件 'xp_cmdshell' 的 过程'sys.xp_cmdshell' 的访问

    sql server 2005: EXEC sp_configure N'show advanced options', N'1' RECONFIGURE WITH OVERRIDEEXEC sp_c ...

  8. JavaScript 计算两个颜色叠加值

    function multiply(rgb1, rgb2) { var result = [], i = 0; for( ; i < rgb1.length; i++ ) { result.pu ...

  9. 0基础学习ios开发笔记第二天

    C语言的基本结构 c语言的入口函数是main函数. main函数的返回值行业标准是int return 数字:返回值 每条语句最后以分号结尾 注释:行注释.块注释 int main(void) { / ...

  10. tableview刷新某个区域(section)或者某一行(row)

    //一个section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2]; [tableview reloadSections:in ...