if !defaults.synchronize() { /*TODO:error handling*/}
//在enum中使用switch
public enum LogLevel: Int {
case Error = 0
case Warning
case Info
case Debug
case Verbose
var prefix : String {
get {
switch self {
case .Error:
return "E"
case .Warning: return "W"
case .Info: return "I"
case .Debug: return "D"
case .Verbose: return "V"
}
}
}
}
//回到上一頁(不管是present或push出來的vc)
dynamic func popToPrevious() {
logInfo(TAG, "popToPrevious")
if self.parentNavigationController != nil {
//self.parentNavigationController是自己創的,看有沒有被給值
self.parentNavigationController?.popViewControllerAnimated(true)
} else if let navigation = self.navigationController {
navigation.popViewControllerAnimated(true)
} else if self.presentingViewController != nil
&& (self.presentingViewController is NewLoginViewController) == false {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
//在textView中加入圖片
var textAttachment = NSTextAttachment()
textAttachment.image = UIImage.scaleToSize(image, size: CGSize(width: UIMessageTheme.AvatarTitle.size, height: UIMessageTheme.AvatarTitle.size))
let imageString = NSAttributedString(attachment: textAttachment)
var mas = NSMutableAttributedString(attributedString: inputTextField.attributedText)
mas.replaceCharactersInRange(NSMakeRange(mas.length, 0), withAttributedString: imageString)
self.inputTextField.attributedText = mas;
//偵測status bar點擊事件(寫在AppDelegate中)
public override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
let touchesSet = touches as NSSet
if let touch = touchesSet.anyObject() as? UITouch {
let location = touch.locationInView(self.window)
//
你偵測的高度
if location.y > 0 && location.y < 20 {
self.touchStatusBar()
}
}
}
private func touchStatusBar() {
NSNotificationCenter.defaultCenter().postNotificationName(DefaultConfig.STATUS_BAR_CLICK, object: nil)
}
//設定UITextView的文字邊界
self.yourTextView.textContainerInset = UIEdgeInsets(top: 10.5, left: 9, bottom: 9.5, right: 15)
//加上模糊效果
//should import UIKit
func insertBlurView (view: UIView, style: UIBlurEffectStyle) {
view.backgroundColor = UIColor.clearColor()
var blurEffect = UIBlurEffect(style: style)
var blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
view.insertSubview(blurEffectView, atIndex: 0)
}
//取得現在時間(原來是用[NSDate date])
let date = NSDate()
//去掉空尾空白和換行
let dataString = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
//data to string (utf8)
let data = command.dataUsingEncoding(NSUTF8StringEncoding)
//data to string (utf8)
let string = String(data: data!, encoding: NSUTF8StringEncoding)
//移除present出來的viewController
func dismissViewController(viewController: UIViewController, animated: Bool) {
if viewController.isBeingDismissed() || viewController.isBeingPresented() {
dispatch_async(dispatch_get_main_queue()) {
dismissViewController(viewController, animated: animated)
}
return
}
if viewController.presentingViewController != nil {
viewController.dismissViewControllerAnimated(animated, completion: nil)
}
}
// UIColor Extension
// 自定義color
class func styleDarkColor() -> UIColor {
return UIColor.hexToColor(0xFF008CEE)
}
class func hexToColor(hex: UInt32) -> UIColor {
return UIColor(red: CGFloat((hex & 0xFF0000) >> 16) / 255, green: CGFloat((hex & 0xFF00) >> 8) / 255 ,
blue: CGFloat(hex & 0xFF) / 255 , alpha: CGFloat((hex & 0xFF000000) >> 24) / 255)
}
// 十六進制String轉UIColor
class func hexStringToColor(hexString: String) -> UIColor {
var rgbValue: UInt32 = 0
let scanner = NSScanner(string: hexString)
scanner.scanLocation = 0
scanner.scanHexInt(&rgbValue)
return UIColor(red: CGFloat((rgbValue & 0xFF0000) >> 16)/255.0,
green: CGFloat((rgbValue & 0xFF00) >> 8)/255.0,
blue: CGFloat(rgbValue & 0xFF)/255.0,
alpha: 1.0)
}
// 以這個顏色產生一個長度為1的小圖(在button好用)
func tinyImage() -> UIImage {
let rect = CGRectMake(0.0, 0.0, 1.0, 1.0)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, self.CGColor)
CGContextFillRect(context, rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
//改變navigationController的顏色
//navigation bar
navigationController.navigationBar.barTintColor = UIColor.yellowColor()
//navigation bar text
navigationController.navigationBar.titleTextAttributes = [UITextAttributeTextColor: UIColor.yellowColor()]
//改變tabBarController的顏色
//tab bar
tabBarController.tabBar.barTintColor = UIColor.yellowColor()
//tab bar text
tabBarController.tabBar.tintColor = UIColor.yellowColor()
//不讓app進入休眠
UIApplication.sharedApplication().idleTimerDisabled = true
同時最好去實作appDelegate裡面的func applicationWillResignActive(application: UIApplication) { ... }
把可以發生的中斷事件都處理好,例如phone call、SMS等。
//客製化UITextField的輸入鍵盤
//換掉inputView就行
yourTextField.inputView = yourPickerView
//換回來系統預設的
yourTextField.inputView = nil
yourTextField.reloadInputViews()
//這是換掉鍵盤上面那一條控制用的Bar
yourTextField.inputAccessoryView = yourCustomAccessoryView
//使用其他預設的鍵盤-改變UIKeyboardType
self.phoneTextField.keyboardType = .PhonePad
self.emailTextField.keyboardType = .EmailAddress
// UIButton預設是置中對齊,如果要改成向左就這樣設
yourButton.contentHorizontalAlignment = .Left
//object to data
let yourViewToData = NSKeyedArchiver.archivedDataWithRootObject(yourView)
if let yourViewFromData = NSKeyedUnarchiver.unarchiveObjectWithData(yourViewToData) as? UIView {
// Do what you want with your view
}