SwiftExtension 0.1.3

SwiftExtension 0.1.3

测试已测试
语言语言 SwiftSwift
许可 MIT
发布上次发布2019年3月
SPM支持 SPM

Dhaval Golakiya 维护。



  • 作者
  • dhvl-golakiya

SwiftExtension

CI Status Version License Platform

为 iOS 应用中常用的类提供简单的 Swift 扩展。

需求

需要 Swift 2.0 或更高版本

安装

手动安装

下载并将类文件夹拖放到项目中

通过 CocoaPods 安装

SwiftExtension可以通过CocoaPods安装。要安装它,只需将以下行添加到Podfile中

pod "SwiftExtension"

SwiftExtension导入到你的类中

import UIKit
import SwiftExtension

##Array 扩展

从数组制作以逗号分隔的字符串

var myArray = ["New York", "London", "Paris"]
myArray.toCommaString // New York,London,Paris

从数组中移除特定对象

var myArray = ["New York", "London", "Paris"]
myArray.removeObject("London") // ["New York", "Paris"]

检查数组是否包含特定对象

var myArray = ["New York", "London", "Paris"]
myArray.containsObject("London") // true
myArray.containsObject("Seattle") // false

获取特定对象的索引

var myArray = ["New York", "London", "Paris"]
myArray.indexOfObject("Paris") // 2

获取指定索引的对象,如果存在。

var myArray = ["New York", "London", "Paris"]
myArray.get(1) // London

Int 扩展

let number = 10
print(number.isOdd) // false
print(number.isEven) // true
print(number.isNegative) // false
print(number.isPositive) // true
print(number.toUInt) // 10
print(number.toFloat)// 10.0
print(number.toInt64) // 10
print(number.toDouble) // 10.0
print(number.toString) // 10
print(number.toCGFloat) // 10.0
print(number.digit()) // [1, 0]
print(number.digitCount) // 2
print(number.factorial()) // 3628800

NSData 扩展

let data = NSData()
data.toDictionary // Convert NSData to NSDictionary
data.toJsonString // Convert NSData to NSString

NSMutableData 扩展

let data = NSMutableData()
data.appendString("String") // Convert String to NSdata then append to NSMutableData

NSDate 扩展

let date = NSDate()
let afterDate = NSDate(timeIntervalSinceNow: 253333)
print(date.weekDay) // 2 : Get Week day from date
print(date.weekOfMonth)// : Get Week index of month from date
print(date.weekDayName) // Monday : Get Week day name from date
print(date.monthName) // April  : Get Month name from date
print(date.month) // 4  : Get Month index from date
print(date.day) // 4  : Get Day index from date
print(date.year) // 2016  : Get Year index from date
print(date.getHourAndMinute()) // (19, 43)  : Get Hour and Minute from date
print(date.weekDaysInMonth()) // 4  : Get Total count of weeks in month from date
print(date.weeksInMonth()) // 4 : Get Total count of week days in month from date
print(date.daysInMonth()) // 30 : Get Total count of days in month from date
print(date.getTime()) // 07:43 PM : Get Time in AM / PM format
print(date.getTimeInShortFormat()) // 04 Apr  : Get Time short (i.e 12 Mar) format
print(date.getTimeInFullFormat()) // 04 Apr, 2016 :Get Time short (i.e 12 Mar, 2016) format
print(date.formateBirthDate()) // 2016-04-04  : Get Time standard (i.e 2016-03-12) format
print(date.afterDate(afterDate)) // true  : Check date is after date
print(date.beforDate(afterDate)) // false : Check date is before date
print(date.equalDate(afterDate)) // false : Check date is equal date
print(date.daysInBetweenDate(afterDate)) // 2 : Get days difference between dates
print(date.hoursInBetweenDate(afterDate)) // 70 : Get hours difference between dates
print(date.minutesInBetweenDate(afterDate)) // 4222 : Get minutes difference between dates
print(date.secondsInBetweenDate(afterDate)) // 253333 : Get seconds difference between dates
print(afterDate.getDifferenceBetweenDates()) // 2 days : 22h : 22m : 22s : Get time difference between date from today

NSDictionary 扩展

let dictionary = NSDictionary(objects: ["value1", "value2"], forKeys: ["key", "secondKey"])
print(dictionary.has("key"))  //  true
print(dictionary.has("firstKey")) //  false
dictionary.toNSData // Convert NSDictionary to NSDate

String 扩展

通用扩展

let testString = "Simple Swift extensions ,for classes which are mostly used in iOS apps"
print(testString.trim())  // SimpleSwiftextensions,forclasseswhicharemostlyusediniOSapps
print(testString.length)  //  70
print(testString.makeArray) //  ["Simple Swift extensions ","for classes which are mostly used in iOS apps" ]
print(testString.makeArrayByWhiteSpace) //  [ Simple, Swift, extensions,",for", classes, which, are, mostly, used, in, iOS, apps ]
print(testString[5])  //  e
print(testString[5 ..< 15]) // e Swift ex
print(testString.stringSizeWith(UIFont.systemFontOfSize(16), width: 200, lineSpecing: 5)) //  (0.0, 0.0, 183.0390625, 91.375)
print(testString.stringWidth(UIFont.systemFontOfSize(16)))  //  510.4140625
print(testString.removeCharsFromEnd(15))  //  Simple Swift extensions ,for classes which are mostly u
print(testString.contains("extensions"))  //  true
print(testString.contains("Extensions"))  //  false
print(testString.containsIgnoreCase("extensions"))  //  true  

let testStringNew = "Simple Swift extensions for classes \nwhich are mostly used in iOS apps"
print(testStringNew.trimForNewLineCharacterSet()) //  Simple Swift extensions for classes  which are mostly used in iOS apps

从YouTube网址中获取YouTube视频ID

let youtubeUrl = "https://www.youtube.com/watch?v=0wIiDnjz4X4"
print(youtubeUrl.getYoutubeID())  //  0wIiDnjz4X4

从电话号码字符串中获取电话号码

let phoneNumberStriing = "+91 22222-22222"
print(phoneNumberStriing.trimPhoneNumberString()) //  912222222222

字符串转日期

let dateString = "1995-12-15"
print(dateString.getDate()) //  1995-12-14 18:30:00 +0000

let dateAndTimeString = "2016-04-05 12:12:10"
print(dateAndTimeString.getDateAndTime()) //  2016-04-05 06:42:10 +0000

字符串转为NSDictionary和NSDate

let jsonString
print(jsonString.toDictionary)  //  Convert String to NSDicationry
print(jsonString.toNSData())    //  Convert String to NSdate

检查字符串是否包含正则表达式

let imageUrl = http://www.testing.com/uploads/images/__w-200-400-600-800-1000__/9fbaa5bc4e032fb528f3f41997f660e7.jpg
print(imageUrl.matchesForRegexInText("__w-((?:-?\\d+)+)__")) // Get only image size string array ["__w-200-400-600-800-1000__"]

路径组件和扩展

let pathString = "file.png" //  
print(pathString.lastPathComponent) //  file.png
print(pathString.pathExtension) //  png
print(pathString.stringByDeletingPathExtension) //  file
print(pathString.stringByDeletingLastPathComponent) //  ""
print(pathString.pathComponents)  //  ["file.png"]
print(pathString.stringByAppendingPathComponent("jpeg"))  //  file.png/jpeg
print(pathString.stringByAppendingPathExtension("files")) //  file.png.files
print(pathString.insertSubString(2, "new testing")) //  finew testingle.png

字符串转数字

let numberString = "44"
print(numberString.toDouble())  //  44.0
print(numberString.toFloat()) //  44.0
print(numberString.toUInt())  //  44
print(numberString.toBool())  //  true

邮箱验证

let emailString = "[email protected]"
let emailStringNew = "dhvl@[email protected]"
print(emailString.isEmail)  //  true
print(emailStringNew.isEmail) //  false

URL验证

let urlString = "www.google.com"
let urlStriongNew = "www."
print(urlString.isValideUrl)  //  true
print(urlStriongNew.isValideUrl)  //  false

UIButton 扩展

let button = UIButton()
button.applyCornerRadius(true)  //  Make button round
button.setBackgroundColor(UIColor.greenColor(), forState: .Selected)  //  Set background color green for selected state
button.textForAllState("Done")  //  Set title label text "Done" for all state
button.textForNormal("Done")  //  Set title label text "Done" for normal state
button.textForSelected("Done")  //  Set title label text "Done" for selected state
button.textForHighlighted("Done")  //  Set title label text "Done" for highlighted state
button.imageForAllState(UIImage(named: "done")!)  //  Set "done" image for all state
button.imageForNormal(UIImage(named: "done")!)  //  Set "done" image for normal state
button.imageForSelected(UIImage(named: "done")!)  //  Set "done" image for selected state
button.imageForHighlighted(UIImage(named: "done")!) //  Set "done" image for highlighted state
button.colorOfTitleLabelForAllState(UIColor.grayColor()) //  Set title label color grey for all state
button.colorOfTitleLabelForNormal(UIColor.grayColor()) //  Set title label color grey for normal state
button.colorOfTitleLabelForSelected(UIColor.grayColor()) // Set title label color grey for selected state
button.colorOfTitleLabelForHighlighted(UIColor.grayColor()) //  Set title label color grey for highlighted state
button.setImageBehindTextWithCenterAlignment(15, buttonWidth: 100, space: 2)  //  Set image behind text in Center alignment
button.setImageOnRightAndTitleOnLeft(15, buttonWidth: 100)  //  Set image right side and text on left side
button.setImageBehindTextWithLeftAlignment(15, buttonWidth: 100)  //  Set image behind text in left alignment

UIColor 扩展

UIColor(hex: 222222, alpha: 1.0)  // Return UIColor from hext
UIColor(hexString: "222222")  //  Return UIcolor from hex string
UIColor(hexString: "222222", alpha: 1.0)  //  Return UIColor from hext string with alpha value

UIFont 扩展

UIFont(fontname: "Custom Font", fontSize: 16) //  Return Custom font with font size

UIImage 扩展

let image = UIImage(named : "test")
image.colorizeImage(UIColor.blackColor) // Return black color image from this image
image.tintedImage() //  Return tinted image from this image
image.croppedImage()  //  Return cropped image 
image.rotateImageToNighntyDegree() // Return 90 degree rotated image

UILabel 扩展

let label = UILabel()
label.getEstimatedSize(320, height: 100)  //  Return estimated size of label
label.getEstimatedHeight()  //  Return estimated height of label
label.getEstimatedWidth() //  Return estimated width of label
label.fitHeight() //  Fit label height
label.fitWidth()  //  Fit label width
label.fitSize() //  Fit label size
label.getLinesArrayOfString() // Get line text in string array
label.getLinesArrayOfString(2)  //  Get nth line text

UIViewController

用于通过event_viewDidLoad方法跟踪屏幕,以分析显示的屏幕

self.className() // Viewcontroller class name

UIView 扩展

let view = UIView()
print(view.endX)  //  Get view.frame.size.width + view.frame.origin.x
print(view.endY)  //  Get view.frame.size.height + view.frame.origin.y
print(view.startX) //  Get view.frame.origin.x
print(view.startY)  //  Get view.frame.origin.y
print(view.getWidth)  // Get view's width
print(view.getHeight) //Get view's height
view.setStartX(10)  //  Set view origin.x to 10
view.setStartY(100) //  Set view origin.y to 100
view.setWidth(150)  //  Set view width 150
view.setHeight(200) //  Set view height 200
view.setCenter(75, y: 75) //  Set vew center to (75,75)
print(view.getCenter()) //  Get view center
view.setCenterX(75) //  Set view center.x to 75
print(view.getCenterX())  //  Get view center.x
view.setCenterY(75) //  Set view center.y to 75
print(view.getCenterY())  //  Get view center.y
print(view.parentViewController) // Get view's parent view controller
print(view.applyPlainShadow()) // Apply plain shadow
print(view.applyBorder()) //  Apply border
view.applyCornerRadius(10, mask: true)  //  Apply corner radius with mask endble / disable
view.addTopBorderWithColor(UIColor.redColor(), width: 2)  //  Add top border
view.addRightBorderWithColor(UIColor.redColor(), width: 2)  //  Add right border
view.addBottomBorderWithColor(UIColor.redColor(), width: 2) //  Add bottom border
view.addLeftBorderWithColor(UIColor.redColor(), width: 2) //  Add left border

作者

dhvl-golakiya, [email protected]

许可证

SwiftExtension 在MIT许可证下可用。更多信息请参阅LICENSE文件。