Skip to content

Instantly share code, notes, and snippets.

Aaina Jain aainaj

  • News Technology Services
  • Bangalore, India
Block or report user

Report or block aainaj

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@aainaj
aainaj / CountLinesForMaxHeightForAllScreenSize.swift
Created Jun 10, 2019
Count number of lines of uilabel for max height for all screen sizes
View CountLinesForMaxHeightForAllScreenSize.swift
func countLines(of label: UILabel, maxHeight: CGFloat) -> Int {
// viewDidLayoutSubviews() in ViewController or layoutIfNeeded() in view subclass
guard let labelText = label.text else {
return 0
}
let rect = CGSize(width: label.bounds.width, height: maxHeight)
let labelSize = labelText.boundingRect(with: rect, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: label.font!], context: nil)
let lines = Int(ceil(CGFloat(labelSize.height) / label.font.lineHeight))
@aainaj
aainaj / CountLinesForMaxHeight.swift
Last active Jun 10, 2019
Count number of lines of uilabel for max height
View CountLinesForMaxHeight.swift
func countLines(of label: UILabel, maxHeight: CGFloat) -> Int {
// viewDidLayoutSubviews() in ViewController or layoutIfNeeded() in view subclass
guard let labelText = label.text else {
return 0
}
let rect = CGSize(width: label.bounds.width, height: maxHeight)
let labelSize = labelText.boundingRect(with: rect, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: label.font!], context: nil)
return Int(ceil(CGFloat(labelSize.height) / label.font.lineHeight))
View PropertyObserver.swift
final class ViewController: UIViewController {
let containerView = UIView()
let tableView = UITableView()
var dataModel: [String] = ["Jan", "Feb", "March"] {
didSet {
if (dataModel != oldValue) {
tableView.reloadData()
}
}
willSet(newValue) {
@aainaj
aainaj / ComputedProperty.swift
Created Apr 22, 2019
Computed property example
View ComputedProperty.swift
import Foundation
struct Rectangle {
var width = 0.0
var height = 0.0
var area: Double {
set {
width = sqrt(newValue)
height = sqrt(newValue)
@aainaj
aainaj / DI-bad-example.swift
Last active Feb 13, 2019
Dependency Inversion Bad Example
View DI-bad-example.swift
struct User {
let name: String
let identifier: String
}
final class UserTransaction {
private let dataBase: DataBase
init(dataBase: DataBase) {
self.dataBase = dataBase
@aainaj
aainaj / DependencyInversion.swift
Created Feb 12, 2019
Dependency Inversion Example
View DependencyInversion.swift
protocol Storable {}
struct User: Storable {
let name: String
let identifier: String
}
protocol UserTransaction {
func add(user: User)
func edit(user: User)
View swift-test-template.codesnippet
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDECodeSnippetCompletionPrefix</key>
<string>test-template</string>
<key>IDECodeSnippetCompletionScopes</key>
<array>
<string>All</string>
</array>
View Contravariance.swift
class Product {
func order(strategy: WorldShippingStrategy) {
}
}
class PhoneProduct: Product {
override func order(strategy: BaseShippingStrategy) {
}
View Covariance.swift
class Product {
func calculateShippingCost() -> BaseShippingStrategy {
return BaseShippingStrategy()
}
}
class PhoneProduct: Product {
// An instance of a subclass can always be substituted for an instance of its superclass.
override func calculateShippingCost() -> WorldShippingStrategy {
return WorldShippingStrategy()
You can’t perform that action at this time.