Skip to content

Instantly share code, notes, and snippets.

View anirudhamahale's full-sized avatar

Anirudha Mahale anirudhamahale

View GitHub Profile
@anirudhamahale
anirudhamahale / TopAlignedLabel.swift
Last active April 24, 2017 11:56
Top left UIlabel text
import UIKit
@IBDesignable class TopAlignedLabel: UILabel {
override func drawText(in rect: CGRect) {
if let stringText = text {
let stringTextAsNSString = stringText as NSString
let labelStringSize = stringTextAsNSString.boundingRect(with: CGSize(width: self.frame.width,height: CGFloat.greatestFiniteMagnitude), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil).size
super.drawText(in: CGRect(x:0,y: 0,width: self.frame.width, height:ceil(labelStringSize.height)))
} else {
super.drawText(in: rect)
@anirudhamahale
anirudhamahale / CONFIG.swift
Last active July 29, 2017 18:07
Most commonly used methods.
// Best way to dismiss the keyboard
func resignKeyboard(){
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to:nil, from:nil, for:nil)
// Visit this link: http://roadfiresoftware.com/2015/01/the-easy-way-to-dismiss-the-ios-keyboard/
}
// Used to set the status bar background color.
func setStatusBarBackgroundColor(color: UIColor) {
// Beware that this method uses the private API, your app might bet rejected from the app store.
@anirudhamahale
anirudhamahale / ScrollViewController.swift
Last active June 11, 2017 13:32
Populate in UIScrollView.
//add all images to the list
func setupList() {
for i in herbs.indices {
//create image view
let imageView = UIImageView(image: UIImage(named: herbs[i].image))
imageView.tag = i
imageView.contentMode = .scaleAspectFill
imageView.isUserInteractionEnabled = true
@anirudhamahale
anirudhamahale / CountryCodes.json
Last active July 19, 2017 04:49
Parse phone number (country code, number)
[{"name":"Israel","dial_code":"+972","code":"IL"},{"name":"Afghanistan","dial_code":"+93","code":"AF"},{"name":"Albania","dial_code":"+355","code":"AL"},{"name":"Algeria","dial_code":"+213","code":"DZ"},{"name":"AmericanSamoa","dial_code":"+1 684","code":"AS"},{"name":"Andorra","dial_code":"+376","code":"AD"},{"name":"Angola","dial_code":"+244","code":"AO"},{"name":"Anguilla","dial_code":"+1 264","code":"AI"},{"name":"Antigua and Barbuda","dial_code":"+1268","code":"AG"},{"name":"Argentina","dial_code":"+54","code":"AR"},{"name":"Armenia","dial_code":"+374","code":"AM"},{"name":"Aruba","dial_code":"+297","code":"AW"},{"name":"Australia","dial_code":"+61","code":"AU"},{"name":"Austria","dial_code":"+43","code":"AT"},{"name":"Azerbaijan","dial_code":"+994","code":"AZ"},{"name":"Bahamas","dial_code":"+1 242","code":"BS"},{"name":"Bahrain","dial_code":"+973","code":"BH"},{"name":"Bangladesh","dial_code":"+880","code":"BD"},{"name":"Barbados","dial_code":"+1 246","code":"BB"},{"name":"Belarus","dial_code":"+375","
@anirudhamahale
anirudhamahale / Sync.swift
Last active June 15, 2017 09:14
Very easy sync code logic.
var server_data = [0, 11, 13, 15]
var local_data = [1, 11, 12]
for (i, local) in server_data.enumerated() {
for (j, server) in local_data.enumerated() {
if local.id == server.id {
server_data.remove(at: i)
local_data.remove(at: j)
// Update record.
// Write a code to update a record in the coredata
@anirudhamahale
anirudhamahale / extensions.swift
Last active July 19, 2017 04:32
Useful extensions
import CoreGraphics
extension CGFloat {
func toRadians() -> CGFloat {
return self * CGFloat.pi / 180
}
func toDegress() -> CGFloat {
return self * 180 / CGFloat.pi
@anirudhamahale
anirudhamahale / CustomeView.swift
Last active July 19, 2017 04:03
Create custome view.
class CustomeView: UIView {
// This view is the xib's view.
@IBOutlet var view: UIView!
// MARK: - Initializers
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
@anirudhamahale
anirudhamahale / DatePickerViewController.swift
Created July 19, 2017 04:12
Inserting Date Picker in UITextField
func insertDatePicker() {
let inputView = UIView(frame: CGRect(x: 0,y: 0, width: self.view.frame.width, height: 240))
let datePicker = UIDatePicker(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 240))
datePicker.datePickerMode = .date
let now: Date = Date()
var plusOneDay: DateComponents = DateComponents()
plusOneDay.day = +1
let oneDayAfter: Date = (Calendar.current as NSCalendar).date(byAdding: plusOneDay, to: now, options: NSCalendar.Options.init(rawValue: 0))!
datePicker.minimumDate = oneDayAfter
@anirudhamahale
anirudhamahale / ViewController.swift
Created July 19, 2017 04:21
Trick when some of the content of the UITableViewCell's get chopped.
/*
When we give dynamic height for the UITableViewCell, sometimes the below line gets truncated, by doing this it can be prevented.
*/
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "NotifyTableViewCell") as! NotifyTableViewCell
cell.bounds = CGRect(x: 0, y: 0, width: tableView.bounds.width * 0.9, height: 99999)
cell.contentView.bounds = cell.bounds
cell.layoutIfNeeded()
@anirudhamahale
anirudhamahale / Date.swift
Last active July 19, 2017 05:23
Better way to manage the date.
func dateOutOf(Iso8601 date: String) -> Date {
let dateFormatter = DateFormatter()
// dateFormatter.calendar = Calendar(identifier: .iso8601)
// dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSz" //Your date format
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0) //Current time zone
let date = dateFormatter.date(from: date) //according to date format your date string
return date! //Convert String to Date
}