This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{"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"," |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import CoreGraphics | |
extension CGFloat { | |
func toRadians() -> CGFloat { | |
return self * CGFloat.pi / 180 | |
} | |
func toDegress() -> CGFloat { | |
return self * 180 / CGFloat.pi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
OlderNewer