Giter Site home page Giter Site logo

stv-extensions's Introduction

STV-Extensions

Extension collection of Swift 3.

CI Status Version License Platform

Requirements

Swift Version 3.0 or more

Installation

Podfile

target 'TargetName' do
    use_frameworks!
    pod "STV-Extensions"
end

Usage

import STV_Extensions

Table of Contents

Foundation

Class Name property/Method Name Description
Data toDeviceToken Data型からDeviceTokenの形式に変換する
Date startOfMonth 月初めの日付を取得する
Date endOfMonth 月終わりの日付を取得する
Date preMonth 先月の日付を取得する
Date nextMonth 翌月の日付を取得する
Date toStr Date型をString型に変換する
Date shortWeekdayStr 日付から曜日を取得する (例)月
Date weekdayStr 日付から曜日を取得する (例)月曜日
Date now 現在の日時(yyyy/MM/dd HH:mm:ss)を日本時間で取得する
Date dateStyle 対象の日付が今日 or 今年 or 翌年以降によって表示を切り替える
Dictionary toJson Dictonary型をJSONに変換する
Int decimalStr Intを3桁ごとにカンマが入ったStringへ変換する
Bundle appShortVersion アプリバージョンを取得する
Bundle buildNo アプリのビルド番号を取得する
Bundle loadJson ローカルのJSONファイルを取得する
NSObject className クラス名を取得する
String toDate String型をDate型に変換する
String toUrl String型をURL型に変換する
String base64Decode StringをUIImageに変換する(Base64)
String isNumericValid 数字の形式か?
String isUrlValid URLの形式か?
String isEmailValid eMailの形式か?
String isTelephoneValid 電話番号の形式か?
String isdateValid 日付の形式か?
String isTimeValid 時間の形式か?
String localized LocalizedStringに設定された値を取得する
URL keyVals URLのパラメタをDictonary型に変換する

UIKit

Class Name property/Method Name Description
UIColor init(hex,alpha) 16進数でUIColorを作成する
UIImage base64Encode UIImageをUIImage変換する(Base64)
UIImage changeColor 画像の色を変更する
UIImageView download サーバーから画像をダウンロードする
UINavigationBar transparent NavigationBarを透過させる & 下線を消す
UINavigationController height NavigationBarの高さを取得する
UINavigationController width NavigationBarの幅を取得する
UINavigationItem hideBackButtonTitle 戻るボタンの文字列を消す
UIScreen screenwidth 画面の幅を取得する
UIScreen screenHeight 画面の高さを取得する
UIScrollView isScrollEnd 最下セルまでスクロールしたか?
UIStoryboard viewController Storyboardからインスタンスを取得する
UITabBar transparent UITabBarを透過させる & 下線を消す
UITableView removeTableFooterView TableFooterViewを削除する
UITableView removeTableHeaderView TableHeaderViewを削除する
UITableView scrollToBottom TableViewの下へスクロールする
UITableView scrollToTop TableViewの上へスクロールする
UITextField isEmpty TextFieldの値が空かチェックする
UITextField trimmedText TextFieldの値をトリムする
UITextField clear TextFieldの値ををクリアする
UITextField setPlaceHolderTextColor TextFieldのプレースホルダーの色を変更する
UITextView clear UITextViewの値をクリアする
UITextView scrollToBottom UITextViewの下へスクロールする
UITextView scrollToTop UITextViewの上へスクロールする
UIView borderColor Viewの枠線の色を変更する
UIView borderWidth Viewの枠線の幅を変更する
UIView cornerRadius Viewの角を丸くする
UIViewController addNotificationObserver Notificationを登録する
UIViewController removeNotificationObserver 所定のNotificationを解除する
UIViewController removeNotificationsObserver Notificationを解除する

Author

stv-ekushida, [email protected]

License

STV-Extensions is available under the MIT license. See the LICENSE file for more info.

stv-extensions's People

Contributors

stv-ekushida avatar

Stargazers

Gaku NISHIMOTO avatar y-okudera avatar  avatar Yuu Ogasawara avatar

Watchers

Shingo Fujita avatar  avatar y-okudera avatar

stv-extensions's Issues

isScrollEnd()をまとめる

@stv-ekushida
isScrollEnd()UIScrollViewにまとめられそうです。

  • 修正内容
    • isScrollEnd()UIScrollViewextensionにする
    • NITS: CGRectのプロパティに直接アクセス self.bounds.size.height -> self.bounds.height
  • 効果
    • サブクラスのUICollectionViewからもUITableViewからも利用できる
    • UIScrollViewからもisScrollEnd()が使えるようになる
    • 定義場所がひとつでOK
import UIKit

extension UIScrollView {

    /// 最下セルまでスクロールしたか?
    func isScrollEnd() -> Bool {
        return self.contentOffset.y >= self.contentSize.height - self.bounds.height
    }
}

// MARK: - demo

let collectionView = UICollectionView(frame: .zero)

if collectionView.isScrollEnd() {
    print("scrollEnd")
}

let tableView = UITableView(frame: .zero)

if tableView.isScrollEnd() {
    print("scrollEnd")
}

【Collection】Arrayなどでfatal error: Index out of rangeを発生させず、nilを返す

Arrayのfatal error: Index out of rangeでクラッシュしないよう、nilを返すExtensionです。

extension Collection {
    subscript(safe index: Index) -> _Element? {
        return index >= startIndex && index < endIndex ? self[index] : nil
    }
}

使い方

let array = ["foo", "bar"]

print(array[0]) // foo
print(array[1]) // bar
print(array[2]) // fatal error: Index out of rangeでクラッシュする
        
print(array[safe: 0]) // Optional("foo")
print(array[safe: 1]) // Optional("bar")
print(array[safe: 2]) // nil

【String】MD5ハッシュ化したString

頻出ではないかもしれませんが、MD5でハッシュ化するextensionです。

定義

extension String {

    /// MD5ハッシュ化したString
    var md5: String {
        var md5String = ""
        let digestLength = Int(CC_MD5_DIGEST_LENGTH)
        let md5Buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: digestLength)

        if let data = self.data(using: .utf8) {
            data.withUnsafeBytes({ (bytes: UnsafePointer<CChar>) -> Void in
                CC_MD5(bytes, CC_LONG(data.count), md5Buffer)
                md5String = (0..<digestLength).reduce("") { $0 + String(format:"%02x", md5Buffer[$1]) }
            })
        }

        return md5String
    }
}

使い方

let string = "demo"
let hashedString = string.md5

【Date】Date→曜日の文字列

Dateから曜日の文字列を取得するものです。

import Foundation

public extension Date {
    
    func shortWeekdayStr() -> String {
        return weekDaySymbol(date: self, style: .short)
    }
    
    func weekdayStr() -> String {
        return weekDaySymbol(date: self, style: .normal)
    }
    
    private enum WeekDayStyle {
        case normal
        case short
    }
    
    private func weekDaySymbol(date: Date, style: WeekDayStyle) -> String {
        let calendar = Calendar.init(identifier: .gregorian)
        let weekday = calendar.component(.weekday, from: date)
        
        let weekdaySymbolIndex = weekday - 1
        let formatter = DateFormatter()
        formatter.locale = Locale(identifier: "ja")
        
        switch style {
        case .normal:
            return formatter.weekdaySymbols[weekdaySymbolIndex]
        case .short:
            return formatter.shortWeekdaySymbols[weekdaySymbolIndex]
        }
    }
}

使い方

let date = Date()
print("\(date.weekdayStr())”)    // 火曜日
print("\(date.shortWeekdayStr())”) // 火

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.