zoukankan      html  css  js  c++  java
  • iOS

    前言

    	NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextView : UIScrollView <UITextInput>
    	@available(iOS 2.0, *)		 public class UITextView : UIScrollView, UITextInput
    
    • UITextView 具有 label 大部分属性,以及 textField 的属性。

    1、UITextView 的创建

    • Objective-C

      	UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 30, self.view.frame.size.width - 20, 400)];
          
          // 将 textView 添加到 view
      	[self.view addSubview:textView];
      
    • Swift

      	let textView:UITextView = UITextView(frame: CGRectMake(10, 30, self.view.frame.size.width - 20, 400))
      	    
      	// 将 textView 添加到 view
      	self.view.addSubview(textView)
      

    2、UITextView 的设置

    • Objective-C

      	// 设置背景颜色
      	textView.backgroundColor = [UIColor lightGrayColor];
      	    
      	// 设置文本
      	/*
      		由于 UITextView 具有滚动功能,当 text 文本内容较多时可以实现滚动阅读和编辑
      	*/
      	textView.text = @"写代码快乐
      15911110524
      写代码快乐
      https://www.baidu.com
      写代码快乐";
      	    
      	// 设置对齐方式
      	/*
      		NSTextAlignmentLeft      = 0,    // Visually left aligned
      		NSTextAlignmentCenter    = 1,    // Visually centered
      		NSTextAlignmentRight     = 2,    // Visually right aligned
      		NSTextAlignmentJustified = 3,    // Fully-justified. The last line in a paragraph is natural-aligned.
      		NSTextAlignmentNatural   = 4,    // Indicates the default alignment for script
      	*/
      	textView.textAlignment = NSTextAlignmentLeft;
      	    
      	// 设置文本颜色
      	textView.textColor = [UIColor redColor];
      	    
      	// 设置字体
      	textView.font = [UIFont boldSystemFontOfSize:20];
      	    
      	// 对文本中的电话和网址自动加链接 
      	/*
      		UIDataDetectorTypePhoneNumber                              = 1 << 0,  // Phone number detection
      		UIDataDetectorTypeLink                                     = 1 << 1,  // URL detection
      		UIDataDetectorTypeAddress NS_ENUM_AVAILABLE_IOS(4_0)       = 1 << 2,  // Street address detection
      		UIDataDetectorTypeCalendarEvent NS_ENUM_AVAILABLE_IOS(4_0) = 1 << 3,  // Event detection
      		UIDataDetectorTypeNone                                     = 0,       // No detection at all
      		UIDataDetectorTypeAll                                      = NSUIntegerMax    // All types
      	*/
      	textView.dataDetectorTypes = UIDataDetectorTypeAll;
      	    
      	// 禁止编辑
      	/*
      		设置为只读,不再能输入内容
      	*/
      	textView.editable = NO;
      	    
      	// 禁止选择
      	/*
      		禁止选中文本,此时文本也禁止编辑
      	*/
      	textView.selectable = NO;
      	    
      	// 禁止滚动
      	textView.scrollEnabled = NO;
      	    
      	// 获取当前选择的范围
      	NSRange range = textView.selectedRange;
      	   	
      	// 设置可以对选中的文字加粗
      	/*
      		选中文字时可以对选中的文字加粗
      	*/
      	textView.allowsEditingTextAttributes = YES;
      	    
      	// 设置 textView 的代理,需遵守协议 <UITextViewDelegate>
      	textView.delegate = self;
      
    • Swift

      	// 设置背景颜色
      	textView.backgroundColor = UIColor.lightGrayColor()
      	    
      	// 设置文本
      	/*
      		由于 UITextView 具有滚动功能,当 text 文本内容较多时可以实现滚动阅读和编辑
      	*/
      	textView.text = "写代码快乐
      15911110524
      写代码快乐
      https://www.baidu.com
      写代码快乐"
      	    
      	// 设置文本颜色
      	textView.textColor = UIColor.redColor()
      	    
      	// 设置对齐方式
      	/*
      		case Left       // Visually left aligned
      		case Center     // Visually centered
      		case Right      // Visually right aligned
      		case Justified  // Fully-justified. The last line in a paragraph is natural-aligned.
      		case Natural    // Indicates the default alignment for script
      	*/
      	textView.textAlignment = .Left
      	    
      	// 设置字体
      	textView.font = UIFont.systemFontOfSize(20)
      	    
      	// 对文本中的电话和网址自动加链接
      	/*
      		PhoneNumber      // Phone number detection
      		Link             // URL detection
      		Address          // Street address detection
      		CalendarEvent    // Event detection
      		None             // No detection at all
      		All              // All types
      	*/
      	textView.dataDetectorTypes = .All
      	    
      	// 禁止编辑
      	/*
      		设置为只读,不再能输入内容
      	*/
      	textView.editable = false
      	    
      	// 禁止选择
      	/*
      		禁止选中文本,此时文本也禁止编辑
      	*/
      	textView.selectable = false
      	    
      	// 禁止滚动
      	textView.scrollEnabled = false
      	    
      	// 获取当前选择的范围
      	let range:NSRange = textView.selectedRange
      	   	
      	// 设置可以对选中的文字加粗
      	/*
      		选中文字时可以对选中的文字加粗
      	*/
      	textView.allowsEditingTextAttributes = true
      	    
      	// 设置 textView 的代理,需遵守协议 UITextViewDelegate
      	textView.delegate = self
      

    3、UITextViewDelegate 的协议方法

    • 需遵守协议 UITextViewDelegate,并设置代理

    • Objective-C

          // 将要开始编辑,编辑开始前被调用
          - (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
      
              return YES;
          }
      
          // 已经开始编辑,编辑开始后被调用
          - (void)textViewDidBeginEditing:(UITextView *)textView {
      
          }
      
          // 将要结束编辑,编辑结束前被调用
          - (BOOL)textViewShouldEndEditing:(UITextView *)textView {
              
              return YES;
          }
      
          // 已经结束编辑,编辑结束后被调用
          - (void)textViewDidEndEditing:(UITextView *)textView {
              
              NSLog(@"编辑的内容是 : %@", textView.text);
          }
      
          // 文本修改,文本修改前被调用
          - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
          
              return YES;
          }
      
          // 文本变更,文本变更时被调用,每输入一个字符时都会被调用
          - (void)textViewDidChange:(UITextView *)textView {
      
          }
      
          // 游标移动,选择范围发生变化时被调用
          - (void)textViewDidChangeSelection:(UITextView *)textView {
      
          }
      
    • Swift

      	// 将要开始编辑,编辑开始前被调用
      	func textViewShouldBeginEditing(textView: UITextView) -> Bool {
      	    
      	    return true
      	}
      	    
      	// 已经开始编辑,编辑开始后被调用
      	func textViewDidBeginEditing(textView: UITextView) {
      	    
      	}
      	    
      	// 将要结束编辑,编辑结束前被调用
      	func textViewShouldEndEditing(textView: UITextView) -> Bool {
      	    
      	    return true
      	}
      	    
      	// 已经结束编辑,编辑结束后被调用
      	func textViewDidEndEditing(textView: UITextView) {
      	    
      	    print("编辑的内容是 : (textView.text)")
      	}
      	    
      	// 文本修改,文本修改前被调用
      	func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
      	    
      	    return true
      	}
      	    
      	// 文本变更,文本变更时被调用,每输入一个字符时都会被调用
      	func textViewDidChange(textView: UITextView) {
      	    
      	}
      	    
      	// 游标移动,选择范围发生变化时被调用
      	func textViewDidChangeSelection(textView: UITextView) {
      	    
      	}
      
  • 相关阅读:
    PDF 补丁丁 0.5.0.2713 发布(替换字库功能修正字符宽度问题)
    PDF 补丁丁 0.5.0.2691 发布(替换字库新增字符映射功能)
    PDF 补丁丁 0.5.0.2657 发布
    安装Windows 10后PDF补丁丁等程序界面变得模糊的解决办法
    《React+Redux前端开发实战》笔记1:不涉及React项目构建的Hello World案例
    React前端有钱途吗?《React+Redux前端开发实战》学起来
    《陪孩子像搭积木一样学编程》,一起来玩Scratch(1)使用Scratch编程的基本流程
    为什么使用React Native
    React Native移动开发实战-5-Android平台的调试技巧
    React Native移动开发实战-4-Android平台的适配原理
  • 原文地址:https://www.cnblogs.com/QianChia/p/5753887.html
Copyright © 2011-2022 走看看