Swift 彙整 – TK Lab https://tklab.club/category/apple/swift/ 雜記、研究日誌、旅遊日記 Sat, 15 May 2021 03:37:06 +0000 zh-TW hourly 1 https://wordpress.org/?v=6.8.2 171198695 Swift日誌:UILable使用技巧 https://tklab.club/swift%e6%97%a5%e8%aa%8c%ef%bc%9auilable%e4%bd%bf%e7%94%a8%e6%8a%80%e5%b7%a7/ https://tklab.club/swift%e6%97%a5%e8%aa%8c%ef%bc%9auilable%e4%bd%bf%e7%94%a8%e6%8a%80%e5%b7%a7/#respond Fri, 29 Dec 2017 07:38:41 +0000 https://epo.wfd.mybluehost.me/TKLab/?p=325 UILable是Swift中常拿來用於顯示文字的元件,任何字串、標題或一段文字都可以使用UILable,今天就 …

這篇文章 Swift日誌:UILable使用技巧 最早出現於 TK Lab

]]>
UILable是Swift中常拿來用於顯示文字的元件,任何字串、標題或一段文字都可以使用UILable,今天就來講個小範例

一樣我們先宣告今天要用到的元件

import UIKit

class ViewController: UIViewController {

    var lable1: UILabel!
    var lable2: UILabel!
  

在viewDidLoad裡制定我們要的文字背景、文字顏色、陰影顏色,需要解釋的都放在註釋裡了

    override func viewDidLoad() {
        super.viewDidLoad()
        self.lable1 = UILabel()
        self.lable1.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)  //color literal
        self.lable1.numberOfLines = 1
        self.lable1.text = "Text test"
        //self.lable1.font = UIFont.systemFont(ofSize: 14)
        //self.lable1.font = UIFont.systemFont(ofSize: UIFont.smallSystemFontSize)
        //self.lable1.font = UIFont.italicSystemFont(ofSize: 17)
        //self.lable1.font = UIFont.boldSystemFont(ofSize: 17)
        self.lable1.font = UIFont(name: "Helvetica Meue ", size: 23)
        self.lable1.textColor = UIColor.darkGray
        self.lable1.textAlignment = .center //致中
        self.lable1.lineBreakMode = .byWordWrapping //自動換行方法
        self.lable1.adjustsFontSizeToFitWidth = false //使文字自動適應UILable
        self.lable1.shadowColor = #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1)
        self.lable1.shadowOffset = CGSize(width: 2, height: 2) //陰影偏移量
        self.view.addSubview(lable1) //加上畫面
        
        self.lable2 = UILabel()
        self.lable2.backgroundColor = #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1)
        self.lable2.textColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
        self.lable2.numberOfLines = 0
        self.lable2.text = "現時電腦採用 x86 架構,那麼 x86 究竟是什麼大家又清楚嗎?這個字其實是一個「指令集架構」家族的總稱,起源自 1978 年的 Intel 8086 中央處理器,它是一個電腦指令巨集的主流沿用至今,可以說是最成功的電腦架構。它的特點是採用 複雜指令集電腦,Complex Instruction Set Computer 字組,指令數目多而且複雜,電腦需要加以判讀所以效率較低(相對於手機等流動裝置用的的 RISC( reduced instruction set computing) 架構),直到 90 年代技術進步情況才有所改變\n在 8086 之後推出 80286 、80386 、80486 等等處理器,其後再出現 Pentium 、Intel Atom 、 Intel Core i3 、i5 系列都採用 x86 架構,在 2017 年 Qualcomm 其中一個目標就是利用 ARM 處理器(採用 RISC 架構)去模擬出 x86 環境,令到 Qualcomm 處理器都能夠支援 x86 程式。"
        self.view.addSubview(lable2) //加上畫面

    }

最後設定兩個UILable的位置,我們放在viewWillAppear裡

    override func viewWillAppear(_ animated: Bool) {
        let frameW = UIScreen.main.bounds.width //找螢幕的寬
        let frameH = UIScreen.main.bounds.height //高
        let gap: CGFloat = 10.0
        //core graphic
        self.lable1.frame = CGRect(x: 0, y: 20, width: frameW, height: 30)
        
        let label1Y = self.lable1.frame.origin.y + self.lable1.frame.height + gap
        let label1W = frameW - gap*2
        let label1H = frameH - label1Y - gap
        self.lable2.frame = CGRect(x: gap, y: label1Y, width: label1W, height: label1H)
    }

}

簡單的小範例就完成了

這篇文章 Swift日誌:UILable使用技巧 最早出現於 TK Lab

]]>
https://tklab.club/swift%e6%97%a5%e8%aa%8c%ef%bc%9auilable%e4%bd%bf%e7%94%a8%e6%8a%80%e5%b7%a7/feed/ 0 325
Swift日誌:textView使用技巧 https://tklab.club/swift%e6%97%a5%e8%aa%8c%ef%bc%9atextview%e4%bd%bf%e7%94%a8%e6%8a%80%e5%b7%a7/ https://tklab.club/swift%e6%97%a5%e8%aa%8c%ef%bc%9atextview%e4%bd%bf%e7%94%a8%e6%8a%80%e5%b7%a7/#respond Mon, 11 Dec 2017 07:46:53 +0000 https://epo.wfd.mybluehost.me/TKLab/?p=250 textView相對於textField只能輸入一行內容,超過便不能滑動的特性來講,textView彈性許多( …

這篇文章 Swift日誌:textView使用技巧 最早出現於 TK Lab

]]>
textView相對於textField只能輸入一行內容,超過便不能滑動的特性來講,textView彈性許多(可以滑動文字框,輸入很多字),但他的屬性基本上與textField一樣,今天就來寫個範例:

畫面上有兩個按鈕和一個textView,這次除了要做textView還要讓鍵盤彈起時自動修正textView的frame(如下圖)

程式碼如下:

先定義按鈕們和textView,加上一些等下frame要用的x,y寬高
注意!ButtonTag是要用來區別undo與done按鈕的,搭配switch使用

import UIKit

class ViewController: UIViewController {
    var previousString = ""
    var undoButton: UIButton!
    var doneButton: UIButton!
    var textView: UITextView!
    
    enum ButtonTags: Int {
        case undo = 1, done
    }
    
    var frameW: CGFloat!
    var frameH: CGFloat!
    var gap: CGFloat!
    var textY: CGFloat!
    var textW: CGFloat!
    var textH: CGFloat!
    var keyboardHeight: CGFloat = 0

再來viewDidLoad我們直接呼叫self.initUI這個方法去使用
在viewWillAppear裡加上點擊textView會彈出keyboard的code
其用意在於對NotificationCenter呼叫鍵盤的開關,方法是固定的,寫久就習慣了
viewWillDisappear的部分是怕如果有不同的頁面要切換,那把通知給取消,可以減少不少記憶體的使用

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.initUI()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        self.buttonEnable(false) //
        
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: .UIKeyboardWillShow, object: nil) //通知鍵盤被打開
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: .UIKeyboardWillHide, object: nil) //通知鍵盤被關閉
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        NotificationCenter.default.removeObserver(self) //取消上面通知,減少記憶體的使用
    }

下面一連串的只是建構物件和給物件位置&屬性,便不再贅述

  //MARK: - fcuntion
    private func initUI() {
        self.view.backgroundColor = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1.0)
        
        self.frameW = UIScreen.main.bounds.width
        self.frameH = UIScreen.main.bounds.height
        self.gap = 10
        
        let buttonW = (self.frameW - self.gap * 3) / 2
        let buttonH: CGFloat = 30
        self.undoButton = UIButton(frame: CGRect(x: self.gap, y: 20, width: buttonW, height: buttonH))
        self.undoButton.setTitle("UNDO", for: .normal)
        self.undoButton.setTitleColor(UIColor.darkGray, for: .normal)
        self.undoButton.addTarget(self, action: #selector(self.onButtonAction(_:)), for: .touchUpInside)
        self.undoButton.tag = ButtonTags.undo.rawValue
        self.undoButton.layer.cornerRadius = 8.0
        self.undoButton.layer.borderWidth = 1.0
        self.undoButton.layer.borderColor = UIColor.darkGray.cgColor
        self.view.addSubview(self.undoButton)
        
        let undoX = self.gap + buttonW + self.gap
        self.doneButton = UIButton(frame: CGRect(x: undoX, y: 20, width: buttonW, height: buttonH))
        self.doneButton.setTitle("DONE", for: .normal)
        self.doneButton.setTitleColor(UIColor.darkGray, for: .normal)
        self.doneButton.addTarget(self, action: #selector(self.onButtonAction(_:)), for: .touchUpInside)
        self.doneButton.tag = ButtonTags.done.rawValue
        self.doneButton.layer.cornerRadius = 8.0
        self.doneButton.layer.borderWidth = 1.0
        self.doneButton.layer.borderColor = UIColor.darkGray.cgColor
        self.view.addSubview(self.doneButton)
        
        self.textY = 20 + buttonH + self.gap
        self.textW = self.frameW - gap * 2
        self.textH = frameH - textY! - gap //加!可能是nil,解決nil不能減nil的問題
        self.textView = UITextView(frame: CGRect(x: gap, y: textY, width: textW, height: textH))
        self.textView.layer.cornerRadius = 8.0
        self.textView.layer.borderWidth = 1.0
        self.textView.layer.borderColor = UIColor.darkGray.cgColor
        self.textView.textColor = UIColor.darkGray
        self.view.addSubview(textView)
        self.textView.delegate = self
        self.textView.text = """
        如果各位有在關注本網站應該會發從昨天開始就連不上了,找問題找了一天,才發現原先買的Cooler Master 酷媽GX 450W整個不讓我開機了….無奈之下拿其他的Power來支援,更無奈的事發生了…..有7顆硬碟SATA電源線卻不夠,果斷拿起之前主任給的雙Power電源線,加上大4pin轉SATA店的線兩條,伺服器火速重新上線!!
"""
    }
    

按鈕的方法寫在這裡,比較主要是setUI要計算當鍵盤彈起textView的高度那段
onButtonAction定義當兩顆按鈕按下要做什麼
keyboardWillShow那段比較複雜,講述keyboard彈起的動畫,也是固定寫法
keyboardWillHide比較簡單,只是回傳歸零而已

    func buttonEnable(_ enable:Bool){
        self.undoButton.isEnabled = enable //上一步的按鈕是否被觸發
    }
    
    func setUI(isKeyboardShown: Bool) {
        let offset = isKeyboardShown ? self.keyboardHeight : 0 //鍵盤有出來就是鍵盤的高度,沒出來就是0
        self.textView.frame.size.height = self.textH - offset
    }
    
    //MARK: - selector
    func onButtonAction(_ sender: UIButton) {
        guard let tag = ButtonTags(rawValue: sender.tag) else { return }
        switch tag {
        case .undo:
            self.textView.text = self.previousString //上一步
        case .done:
            self.view.endEditing(true) //End Editing
        }
    }
    
    func keyboardWillShow(notification: NSNotification) {
        if let infoKey = notification.userInfo?[UIKeyboardFrameEndUserInfoKey], let rectValue = (infoKey as AnyObject).cgRectValue {
            let keyboardFrame = self.view.convert(rectValue, to: nil)
            self.keyboardHeight = keyboardFrame.height
            self.setUI(isKeyboardShown: true)
        } //
        
    }
    
    func keyboardWillHide(notification: NSNotification) {
        self.setUI(isKeyboardShown: false)
    }

}

最後收尾是用extension把UITextView的Delegate寫在這兒
為暫存文字的方法:
textViewDidBeginEditing開始編輯就儲存一次,但還不夠
所以在用shouldChangeTextIn的方式存一遍

extension ViewController: UITextViewDelegate {
    
    func textViewDidBeginEditing(_ textView: UITextView) {
        self.previousString = self.textView.text //暫存輸入的文字
    }
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        print(text)
        self.previousString = self.textView.text
        self.buttonEnable(true) //編輯時按鈕可按
        return true
    }
    func textViewDidEndEditing(_ textView: UITextView) {
        self.buttonEnable(false) //不編輯時按鈕不可按
    }
    
}

這篇文章 Swift日誌:textView使用技巧 最早出現於 TK Lab

]]>
https://tklab.club/swift%e6%97%a5%e8%aa%8c%ef%bc%9atextview%e4%bd%bf%e7%94%a8%e6%8a%80%e5%b7%a7/feed/ 0 250