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 | |
struct Storyboard<T: UIViewController> { | |
static var storyboardName: String { | |
return String(describing: T.self) | |
} | |
static var viewController: T { | |
let storyboard = UIStoryboard(name: "Main", bundle: nil) |
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
// 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 | |
} |
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
// 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 { |
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
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) |
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
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") |
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
// 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); |
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
let language = Locale.current.localizedString(forLanguageCode: "ar") | |
print(language) // Arabic (if the user's device language is English) |
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
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 | |
} |
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
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 | |
} |
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 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 { |
NewerOlder