Skip to content

Instantly share code, notes, and snippets.

View Lancewer's full-sized avatar
🎯
Focusing

Lancewer Lancewer

🎯
Focusing
View GitHub Profile
@Lancewer
Lancewer / ConvertPlistToDictionary.swift
Created January 26, 2018 00:54
[ConvertPlistToDictionary] new API that replace NSDictionary.init?(contentsOfFile path: String) #Dictionary #plist #contentOfFile
class func getCategories() -> (categoryDict:[String:Any], level1CateArray:[String]) {
var categoriesDict:[String:Any] = [:]
var level1Categories:[String] = []
if let path = Bundle.main.path(forResource: "Category", ofType: "plist") {
let rawData = try! Data(contentsOf: URL(fileURLWithPath: path))
if let categoryDict = try! PropertyListSerialization.propertyList(from: rawData, format: nil) as? [String:Any] {
categoriesDict = categoryDict
for k in categoryDict.keys {
level1Categories.append(k)
@Lancewer
Lancewer / GetStringSize.swift
Created January 17, 2018 02:26
[GetStringSize] how to get the string size rect in iOS #String #Swift #iOS #size #UI
let cellTitle:NSString = "string to calculate"
var autoSize = cellTitle.size(withAttributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 17.0)])
@Lancewer
Lancewer / Readme.md
Created January 17, 2018 01:24
[AddSubviewToSuperViewCenter] how to add a subview to superview's center #UIView #center #iOS #OC

#This simple snippet shows how to add a subview to superview's center.

@Lancewer
Lancewer / panGestureBasicUsage.swift
Created January 15, 2018 14:19
[PanGestureBasicUsage] basic usage of pan gesture recongizer
// The Pan Gesture
func createPanGestureRecognizer(targetView: UIImageView) {
var panGesture = UIPanGestureRecognizer(target: self, action:#selector(handlePanGesture(_:)))
targetView.addGestureRecognizer(panGesture)
}
@objc func handlePanGesture(panGesture: UIPanGestureRecognizer) {
// get translation
let translation = panGesture.translation(in: view)
// This line is reset translation point to zero, let the next translation base on
@Lancewer
Lancewer / FetchPropertiesOnDemand.swift
Created January 12, 2018 07:29
[FetchPropertiesOnDemand] Fetch part of properties in an managed object #CoreData #Fetch
let briefInfoFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "ClothItem")
briefInfoFetchRequest.resultType = .dictionaryResultType
briefInfoFetchRequest.returnsObjectsAsFaults = false
let desc = NSExpressionDescription()
desc.name = "mainPhoto"
desc.expression = NSExpression(forKeyPath: "mainPhotoURLString")
desc.isStoredInExternalRecord = true
desc.expressionResultType = .stringAttributeType
@Lancewer
Lancewer / getAllManagedID.swift
Created January 11, 2018 08:57
[GetManagedObjectID] get managedObjectID created by CoreData #CoreData #iOS #ID #swift #ObjectID
let itemIDsFetchRequest = NSFetchRequest<NSManagedObjectID>(entityName: "ClothItem")
itemIDsFetchRequest.resultType = .managedObjectIDResultType
do {
let ids = try managedContext!.fetch(itemIDsFetchRequest)
print(ids.first!.uriRepresentation().absoluteString)
}catch let error as NSError {
print("Fetch item failed:\(error), \(error.userInfo)")
}
@Lancewer
Lancewer / CloseKeyboard.swift
Created December 23, 2017 09:34
[Close keyboard] close all the keyboard on the screen in iOS #iOS #keyboard #first responder #close
//You can drag a tapGestureRecongizer to the very bottom view, and link a IBAction like below to it
@IBAction func closeAllKeyborad(_ sender: Any) {
view.endEditing(true)
}
@Lancewer
Lancewer / CollectionViewCell.swift
Last active May 17, 2023 09:05
[Collection Single Selection] single selection in UICollectionView with swift #swift #UICollectionView #selection #select #single select
//custom cell class
import UIKit
class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var nameLabel: UILabel!
override var isSelected: Bool{
didSet(newValue){
contentView.backgroundColor = newValue ? UIColor.green : UIColor.white
@Lancewer
Lancewer / CustomizeTabBarItem.swift
Last active December 20, 2017 23:38
[Customize UITabBarItem] customize UITabBarItem with image and text color #UITabBarItem #customize #Appearence #Image
func setupTabBarItem(){
var defaultIconImage = UIImage(named: "035")
if let image = defaultIconImage {
defaultIconImage = image.withRenderingMode(.alwaysOriginal) //Keep image render model original is important!
}
var selectedIconImage = UIImage(named: "020")
if let image = selectedIconImage {
selectedIconImage = image.withRenderingMode(.alwaysOriginal)
}
@Lancewer
Lancewer / SimpleAlertController.swift
Created December 11, 2017 03:39
[Simple AlertController in iOS] how to create a basic alert controller with a textfield in iOS #iOS #Swift #alert #alertController #textField
@IBAction func addName(_ sender: Any) {
let alert = UIAlertController(title: "New Name", message: "Add a new name", preferredStyle: .alert)
let saveAction = UIAlertAction(title: "Save", style: .default) {
[unowned self] action in
guard let textField = alert.textFields?.first,
let nameToSave = textField.text else {
return
}
self.names.append(nameToSave)
self.tableView.reloadData()