Skip to content

Instantly share code, notes, and snippets.

View lamprosg's full-sized avatar

Lampros Giampouras lamprosg

  • Athens, Greece
View GitHub Profile
@lamprosg
lamprosg / AwakeAfter.swift
Last active March 25, 2023 14:34
(iOS) Load custom UIview in xib
//Create the xib CustomView.xib and add the View class as CustomView (this one)
override func awakeAfter(using aDecoder: NSCoder) -> Any? {
if self.subviews.isEmpty {
let nib = UINib(nibName: "CustomView", bundle: nil)
let viewArray = nib.instantiate(withOwner: nil, options: nil)
guard let view = viewArray.first as? CustomView else {
return self
}
view.translatesAutoresizingMaskIntoConstraints = false
@lamprosg
lamprosg / UILabel+highlight.swift
Created June 27, 2019 07:58
(iOS) Label string highlight
import UIKit
extension UILabel {
/// Highlights a specific substring of the label's text with a desired color
/// Text must be set before calling this method.
///
/// - Parameters:
/// - subString: The substring to change color
/// - color: The desired color
func highlight(subString: String, color: UIColor?) {
@lamprosg
lamprosg / UIView+Rounded.swift
Created March 30, 2019 14:28
(iOS) View rounded corners
import UIKit
extension UIView {
func roundCorners(corners: UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: bounds,
byRoundingCorners: corners,
cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
@lamprosg
lamprosg / gcd.swift
Last active September 20, 2021 10:39
(iOS) GCD
//https://medium.com/@nimjea/grand-central-dispatch-in-swift-fdfdd8b22d52
//https://medium.com/@wailord/a-high-level-primer-on-ios-concurrency-df3bca3a6993
//https://cocoacasts.com/choosing-between-nsoperation-and-grand-central-dispatch/
DispatchQueue.main.async {
// Perform your async code here
}
//Synced operations
let northZone = DispatchQueue(label: "perform_task_with_team_north")
@lamprosg
lamprosg / GenericTableViewDatasource.swift
Last active October 11, 2018 13:19
(iOS) Generic tableview datasource
//Variation of this feature: https://www.swiftbysundell.com/posts/reusable-data-sources-in-swift
import UIKit
/// Keep a strong reference to this, since UITableView only uses a weak reference for it. Used only for simple table views fast rendering.
class TableViewCommonCellDataSource<Model>: NSObject, UITableViewDataSource {
/// Cell configuration
typealias CellConfigurator = (Model, UITableViewCell) -> Void
@lamprosg
lamprosg / VCExtension.swift
Created August 27, 2018 08:05
(iOS) Extensions with computed properties
extension UIViewController {
private static var _myComputedProperty = [String:Bool]()
var myComputedProperty:Bool {
get {
let tmpAddress = String(format: "%p", unsafeBitCast(self, to: Int.self))
return UIViewController._myComputedProperty[tmpAddress] ?? false
}
set(newValue) {
let tmpAddress = String(format: "%p", unsafeBitCast(self, to: Int.self))
@lamprosg
lamprosg / MySwitch.swift
Last active November 28, 2020 16:25
(iOS) Swift button tap with handler
class MySwitch: UISwitch {
typealias DidTapSwitch = (WMSwitch) -> ()
var didChangeState: DidTapSwitch? {
didSet {
if didChangeState != nil {
addTarget(self, action: #selector(self.switchStateDidChange(_:)), for: .valueChanged)
} else {
removeTarget(self, action: #selector(self.switchStateDidChange(_:)), for: .valueChanged)
@lamprosg
lamprosg / 0.map.swift
Created June 19, 2018 14:11
(iOS) Collection higher order functions
//Map
//Loops over a collection and applies the same operation to each element in the collection.
var numberArray = [1,2,3,4,5]
var newArray = numberArray.map( {(value:Int) -> Int in
return value*2
})
//or quicker
@lamprosg
lamprosg / CircleView.swift
Last active June 17, 2018 11:02
(iOS) CAShapeLayer circle
import UIKit
class CircleView: UIView {
var viewLoaded:Bool = false
var lineWidth:CGFloat = 10
var animationDuration:Double = 5
let circlePathLayer = CAShapeLayer()
@lamprosg
lamprosg / 0.xibView.swift
Last active July 3, 2018 08:00
(iOS) Chart line with Charts
//In case we had to load a view from a xib
//In case we had a nib for the chart
override public func awakeAfter(using aDecoder: NSCoder) -> Any? {
let isPlaceHolder:Bool = self.subviews.count == 0
if isPlaceHolder {
let lineChartView:MyLineChartView? = Bundle.main.loadNibNamed("LineChartViewNibName", owner: nil, options: nil)?.first as? MyLineChartView
lineChartView?.autoresizingMask = self.autoresizingMask;
lineChartView?.translatesAutoresizingMaskIntoConstraints = self.translatesAutoresizingMaskIntoConstraints;