Skip to content

Instantly share code, notes, and snippets.

View muhlenXi's full-sized avatar
🎯
Focusing

YinjunXi muhlenXi

🎯
Focusing
View GitHub Profile
@muhlenXi
muhlenXi / mainDelegate.swift
Last active November 23, 2018 08:27
Get AppDelegate
extension AppDelegate {
/// 获取 App delegate
static func mainDelegate() -> AppDelegate? {
if let delegate = UIApplication.shared.delegate as? AppDelegate {
return delegate
}
return nil
}
}
@muhlenXi
muhlenXi / is12HourMode.swift
Last active November 23, 2018 08:24
判断当前设备时间是否是12小时制?
public extension UIDevice {
/// 判断当前设备时间是否是12小时制?
static func is12HourMode() -> Bool{
if let formatStringForHours = DateFormatter.dateFormat(fromTemplate: "j", options: 0, locale: Locale.current) {
return formatStringForHours.contains("a")
}
return false
}
}
@muhlenXi
muhlenXi / MD5_SHA1.m
Last active November 23, 2018 01:47
对字符串进行MD5或SHA1 加密
// 需要 #import <CommonCrypto/CommonDigest.h>
// 对字符串进行MD5加密
+ (NSString *)MD5WithString:(NSString *)string
{
const char *cStr = [string UTF8String];
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, (uint32_t)strlen(cStr), digest);
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
@muhlenXi
muhlenXi / ViewShadow.swift
Last active November 23, 2018 08:13
给view添加阴影
public extension UIView {
/// 添加阴影效果
func setShadow(color: UIColor = UIColor.black, opacity: Float = 0.5, offset: CGSize = CGSize(width: 0, height: 0)) {
// 添加阴影
self.layer.shadowColor = color.cgColor
// 阴影的透明度
self.layer.shadowOpacity = opacity
// 阴影的偏移
self.layer.shadowOffset = offset
// 避免离屏渲染带来卡顿
@muhlenXi
muhlenXi / NSCoding.swift
Last active November 23, 2018 07:59
如何对对象进行序列化和反序列化?
//-------------------------------------------------------- define Model--------------------------------------------------
import Foundation
class TodoItem: NSObject, NSCoding {
// Attribute of model
var name: String = ""
var isFinished: Bool = false
@muhlenXi
muhlenXi / seguePrepare.swift
Last active November 23, 2018 01:44
一个 segue 的 prepare 方法 demo
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get navigation controller
let naviController = segue.destination as! UINavigationController
// Get view controller
let todoDetailController = naviController.topViewController as! TodoDetailTableViewController
if segue.identifier == "AddTodo" {
todoDetailController.title = "Add todo"
// Todo
}
@muhlenXi
muhlenXi / NavigationBar.swift
Last active November 23, 2018 01:43
设置 Navigationbar 的相关属性
func setNavigationBarAttributes() {
//设置NavgationBar title 字体 和 颜色
var titleTextAttributes = Dictionary<String, Any>()
titleTextAttributes[NSForegroundColorAttributeName] = UIColor.black
titleTextAttributes[NSFontAttributeName] = UIFont.systemFont(ofSize: 16)
self.navigationController?.navigationBar.titleTextAttributes = titleTextAttributes
//设置NavgationBar的背景颜色
self.navigationController?.navigationBar.barTintColor = UIColor.white
@muhlenXi
muhlenXi / NSDateFormatter.swift
Last active November 22, 2018 11:27
NSDateFormatter 格式化参数说明
附:NSDateFormatter格式化参数如下:
G: 公元时代,例如AD公元
yy: 年的后2位
yyyy: 完整年
MM: 月,显示为1-12
MMM: 月,显示为英文月份简写,如 Jan
MMMM: 月,显示为英文月份全称,如 Janualy
dd: 日,2位数表示,如02
d: 日,1-2位显示,如 2
@muhlenXi
muhlenXi / Calendar.swift
Last active November 23, 2018 07:53
通过 Calendar 计算今年几岁?
/// 计算今年年龄 eg: 1992-03-19
func calculateThisYearAge(birthday: String) -> Int?{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
if let birthday = dateFormatter.date(from: birthday) {
let gregorian = Calendar(identifier: .gregorian)
let components = gregorian.dateComponents([Calendar.Component.year], from: birthday, to: Date())
if let year = components.year {
return year + 1
}
@muhlenXi
muhlenXi / SeguePassValue.swift
Last active November 22, 2018 11:24
Segue 正、反向传值
// 正向传值
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let identifier = segue.identifier {
if identifier == "ToBeauty" {
let desVC = segue.destination as! BeautyViewController
let index = pickerView.selectedRow(inComponent: 0)
desVC.imageName = imageNames[index]
desVC.name = names[index]
}