Skip to content

Instantly share code, notes, and snippets.

View fethica's full-sized avatar
:octocat:

Fethi El Hassasna fethica

:octocat:
View GitHub Profile
@fethica
fethica / Storyboard.swift
Created November 24, 2022 06:35
Create controllers programmatically from the Main Storyboard
import UIKit
struct Storyboard<T: UIViewController> {
static var storyboardName: String {
return String(describing: T.self)
}
static var viewController: T {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// source: https://stackoverflow.com/questions/27652227/add-placeholder-text-inside-uitextview-in-swift
textView.text = "Placeholder"
textView.textColor = UIColor.lightGray
func textViewDidBeginEditing(_ textView: UITextView) {
guard textView.textColor == UIColor.lightGray else { return }
textView.text = nil
textView.textColor = UIColor.black
}
// From: https://stackoverflow.com/questions/55592124/ios-how-to-give-a-peek-at-the-swipe-to-delete-action-on-a-table-view-cell
extension UITableView {
func presentTrailingSwipeHint() {
let cell = self.cellForRow(at: IndexPath(item: 0, section: 0))
cell?.presentTrailingSwipeHint(actionColor: UIColor.red)
}
}
fileprivate extension UITableViewCell {
let duration = "44:18"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "mm:ss"
let date = dateFormatter.date(from: duration)
let minutesComponents = Calendar.current.dateComponents([.minute, .second], from: date!)
let accessibilityLabel = DateComponentsFormatter.localizedString(from: minutesComponents, unitsStyle: .spellOut)
extension URLRequest {
static func record(_ request:URLRequest, _ response:URLResponse?, _ data:Data?, _ error:Error?) {
#if DEBUG
print("\n🔵 REQUEST:\n" + request.debugDescription)
if let data = data,
let response = String(data: data, encoding: .utf8) {
print("\n✅ RESPONSE:\n\(response)" + "\n")
} else {
print("\n❌ ERROR:\n" + (error?.localizedDescription ?? "unknown error") + "\n")
// Source: https://stackoverflow.com/questions/17753800/android-default-values-for-shared-preferences
public class Preferences {
public static final String MY_PREF = "MyPreferences";
private SharedPreferences sharedPreferences;
private Editor editor;
public Preferences(Context context) {
this.sharedPreferences = context.getSharedPreferences(MY_PREF, 0);
@fethica
fethica / LanguageCodeToName.swift
Created March 23, 2018 18:00
Get the language name for code based on the user's device current language
let language = Locale.current.localizedString(forLanguageCode: "ar")
print(language) // Arabic (if the user's device language is English)
@fethica
fethica / subscript.swift
Last active March 14, 2018 19:25
Example of subscript in Swift
struct daysofaweek {
private var days = [“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “saturday”]
subscript(index: Int) -> String {
get {
return days[index]
}
set(newValue) {
days[index] = newValue
}
@fethica
fethica / ProductCodable.swift
Last active February 24, 2023 15:49
NSKeyedArchiver using Swift 4 Codable protocol
struct Product: Codable {
var title: String
var price: Double
var quantity: Int
init(title: String, price: Double, quantity: Int) {
self.title = title
self.price = price
self.quantity = quantity
}
@fethica
fethica / EstimateCellHeight.swift
Last active February 19, 2018 18:01
Estimated height for a cell based on string value and font type
func estimateHeight(string: String?, font: UIFont, right: CGFloat = 0, left: CGFloat = 0) -> CGFloat {
guard let string = string else { return 0 }
let width = view.frame.width - right - left
let rect = NSString(string: string).boundingRect(with: CGSize(width: width, height: 1000.0), options: NSStringDrawingOptions.usesFontLeading.union(NSStringDrawingOptions.usesLineFragmentOrigin), attributes: [NSAttributedStringKey.font: font], context: nil)
return rect.height
}
// Usage for UICollectionViewCell
override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {