1. 程式人生 > >[Swift通天遁地]二、表格表單-(12)設定表單文字對齊方式以及自適應高度的文字區域TextArea

[Swift通天遁地]二、表格表單-(12)設定表單文字對齊方式以及自適應高度的文字區域TextArea

本文將演示如何調整文字的對齊方式,以及建立一個可根據內容自動調整高度的文字區域。

在專案導航區,開啟檢視控制器的程式碼檔案【ViewController.swift】

現在開始編寫程式碼,建立星期選項表單和擁有浮動標籤的文字框。

  1 import UIKit
  2 //首先在當前類檔案中,
  3 //引入以及安裝的第三方類庫
  4 import Eureka
  5 
  6 //修改當前檢視控制器類的父類的名稱
  7 class ViewController: FormViewController {
  8     
  9     override func viewDidLoad() {
10 super.viewDidLoad() 11 12 //建立一個新的表單 13 form +++ 14 //在表單中新增一個段落,並設定段落的頭部區域和尾部區域 15 Section(header: "Default field rows", 16 footer: "Rows with title have a right-aligned text field.\nRows without title have a left-aligned text field.\nBut this can be changed...
") 17 18 //新增一個名稱行 19 <<< NameRow() 20 { 21 //設定該行的標題 22 $0.title = "Your name:" 23 //設定該行的佔位文字 24 $0.placeholder = "(right alignment)" 25 } 26 //
在當前表單行的左側,新增一個縮圖標 27 .cellSetup 28 { 29 cell, row in 30 cell.imageView?.image = UIImage(named: "plus_image") 31 } 32 //新增一個名稱行 33 <<< NameRow() 34 { 35 //設定該行的佔位文字 36 $0.placeholder = "Name (left alignment)" 37 } 38 //在當前表單行的左側,新增一個縮圖標 39 .cellSetup 40 { 41 cell, row in 42 cell.imageView?.image = UIImage(named: "plus_image") 43 } 44 45 //新增一個新的段落 46 +++ Section("Customized Alignment") 47 //新增一個名稱行 48 <<< NameRow() 49 { 50 //設定該行的標題 51 $0.title = "Your name:" 52 } 53 54 .cellUpdate 55 { 56 cell, row in 57 //設定文字框的文字對齊方式為左對齊 58 cell.textField.textAlignment = .left 59 //設定該行的佔位文字 60 cell.textField.placeholder = "(left alignment)" 61 } 62 //新增一個名稱行 63 <<< NameRow().cellUpdate 64 { 65 cell, row in 66 //設定文字框的文字對齊方式為右對齊 67 cell.textField.textAlignment = .right 68 //設定該行的佔位文字 69 cell.textField.placeholder = "Name (right alignment)" 70 } 71 72 //新增一個新的段落,並設定段落的頭部區域和尾部區域 73 +++ Section(header: "Customized Text field width", 74 footer: "Eureka allows us to set up a specific UITextField width using textFieldPercentage property. In the section above we have also right aligned the textLabels.") 75 //新增一個名稱行 76 <<< NameRow() 77 { 78 //設定該行的標題 79 $0.title = "Title" 80 //設定文字框的寬度比例為0.6 81 $0.textFieldPercentage = 0.6 82 $0.placeholder = "textFieldPercentage = 0.6" 83 } 84 .cellUpdate 85 { 86 //設定文字框的對齊方式為左對齊 87 $1.cell.textField.textAlignment = .left 88 //設定文字標籤的對齊方式為右對齊 89 $1.cell.textLabel?.textAlignment = .right 90 } 91 //新增一個名稱行 92 <<< NameRow() { 93 //設定該行的標題 94 $0.title = "Another Title" 95 //設定文字框的寬度比例為0.7 96 $0.textFieldPercentage = 0.7 97 $0.placeholder = "textFieldPercentage = 0.7" 98 } 99 .cellUpdate 100 { 101 //設定文字框的對齊方式為左對齊 102 $1.cell.textField.textAlignment = .left 103 //設定文字標籤的對齊方式為右對齊 104 $1.cell.textLabel?.textAlignment = .right 105 } 106 //新增一個新的段落,在該段落中,建立一個自適應高度的文字區域 107 +++ Section("TextAreaRow") 108 109 //新增一個文字區域行 110 <<< TextAreaRow() 111 { 112 //設定改行的佔位文字 113 $0.placeholder = "TextAreaRow" 114 //設定文字區域的高度為自適應,其初始高度為110 115 $0.textAreaHeight = .dynamic(initialTextViewHeight: 110) 116 } 117 } 118 119 override func didReceiveMemoryWarning() { 120 super.didReceiveMemoryWarning() 121 // Dispose of any resources that can be recreated. 122 } 123 }