【Swift】iOS開發小技巧(一)

來源:互聯網
上載者:User

標籤:

 

前言

  邊開發邊學習,邊攢經驗,匯總一下記錄到這裡

 

聲明
  歡迎轉載,但請保留文章原始出處:)
  部落格園:http://www.cnblogs.com
  農民伯伯: http://over140.cnblogs.com

 

1、隱藏/顯示密碼功能

  光設定secureTextEntry還不行,你會發現UITextField在切換到顯示密碼時會多一個Null 字元,看著巨彆扭,需要在更改secureTextEntry後進行如下設定:

        let pwd = psdField.text
        self.psdField.text = pwd + " "
        self.psdField.text = pwd

 

2、擷取當前類的名稱

String.fromCString(object_getClassName(self))

  注意:通過_stdlib_getDemangledTypeName也能取到,但是如果在父類裡面去就只能取到父類的名稱

 

3、 國際化

find . \( -name ‘*.m‘ -o -name ‘*.h‘ \) -print0 | xargs -0 genstrings -o en.lproj 

  凡是使用了NSLocalizedString的字串都能被找到,支援子目錄尋找,注意替換en.lproj

 

4、UITableView分割線的顯示問題

  去掉分割線:設定UITableView的separatorStyle = UITableViewCellSeparatorStyle.None

  去掉多餘的分割線:設定UITableView的tableFooterView = UIView()  (要是不設定會很醜,不管有沒有資料都會顯示分割線)

  處理 iOS8 分割線左邊距設定為0失效的問題,參考這裡(http://stackoverflow.com/questions/25770119/ios-8-uitableview-separator-inset-0-not-working):

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {


        // Remove seperator inset
        if cell.respondsToSelector("setSeparatorInset:") {
            cell.separatorInset = UIEdgeInsetsZero
        }

        // Prevent the cell from inheriting the Table View‘s margin settings
        if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
            cell.preservesSuperviewLayoutMargins = false
        }

        // Explictly set your cell‘s layout margins
        if cell.respondsToSelector("setLayoutMargins:") {
            cell.layoutMargins = UIEdgeInsetsZero
        }

    }

 

5、 格式化數字輸出 K/M

extension String {

    public func substring(startIndex: Int, endIndex: Int) -> String{
        return (self as NSString).substringWithRange(NSRange(location: startIndex, length: endIndex - startIndex))
    }

}

    public static func prettyNumber(num: Double) -> String{
        if (num < 10000) {
            return "\(Int(num))";
        } else if (num < 100000) {
            return "\(num / 1000.0)".substring(0, endIndex: 4) + "K"
        } else if (num < 1000000) {
            return "\(num / 1000.0)".substring(0, endIndex: 3) + "K"
        } else if (num < 100000000) {
            return "\(num / 1000000.0)".substring(0, endIndex: 4) + "M"
        } else if (num < 1000000000) {
            return "\(num / 1000000.0)".substring(0, endIndex: 3) + "M"
        } else if (num < 100000000000) {
            return "\(num / 1000000000.0)".substring(0, endIndex: 4) + "M"
        } else if (num < 1000000000000) {
            return "\(num / 1000000000.0)".substring(0, endIndex: 3) + "M"
        }
        return "INF";
    }

 

6、 判斷螢幕是否是橫屏

    public static func isIsLandscape() -> Bool {        return UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation) || UIApplication.sharedApplication().statusBarOrientation == UIInterfaceOrientation.LandscapeLeft  || UIApplication.sharedApplication().statusBarOrientation == UIInterfaceOrientation.LandscapeRight    }

 

7、 URL 編碼

text.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())

   這個 text 的類型是 String ,常用於搜尋功能,在  URL 中包含被搜的關鍵字,如果不處理搜中文或者帶空格的英文會直接崩潰

 

【Swift】iOS開發小技巧(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.