Skip to content

Instantly share code, notes, and snippets.

View harshvishu's full-sized avatar
🦀
Learning

हर्ष • Harsh • ハラシャ harshvishu

🦀
Learning
View GitHub Profile
// Authoer: The SwiftUI Lab
// Full article: https://swiftui-lab.com/scrollview-pull-to-refresh/
import SwiftUI
struct ContentView: View {
@ObservedObject var model = MyModel()
@State private var alternate: Bool = true
let array = Array<String>(repeating: "Hello", count: 100)
// Authoer: The SwiftUI Lab
// Full article: https://swiftui-lab.com/scrollview-pull-to-refresh/
import SwiftUI
import Combine
struct Dog: Identifiable {
let id = UUID()
let name:String
let picture: String
@jstheoriginal
jstheoriginal / SearchBar.swift
Created June 20, 2019 00:42
A simple SwiftUI search bar
struct SearchBar : View {
@Binding var searchText: String
var body: some View {
HStack {
Image(systemName: "magnifyingglass").foregroundColor(.secondary)
TextField(
$searchText,
placeholder: Text("Search")) {
UIApplication.shared.keyWindow?.endEditing(true)
extension CGSize {
init(_ point: CGPoint) {
self.width = point.x
self.height = point.y
}
func friendlyString() -> String {
return "(\(String(format: "%.2f", width)), \(String(format: "%.2f", height)))"
}
@smileyborg
smileyborg / SimulatedSlowLoadingImage.swift
Created August 5, 2017 02:06
Snippet showing how to create an NSItemProvider with a simulated delay for loading an image
import UIKit
import MobileCoreServices
let image = UIImage() // your actual image
let itemProvider = NSItemProvider()
itemProvider.registerDataRepresentation(forTypeIdentifier: kUTTypeJPEG as String, visibility: .all) { (completionBlock) -> Progress? in
let unitsOfWork = 10 + Int64(arc4random_uniform(UInt32(10))) // 10 - 19 units
let progress = Progress.discreteProgress(totalUnitCount: unitsOfWork)
@aydenp
aydenp / UIViewController+ShorthandAlerts.swift
Last active November 25, 2020 07:10
Creating alerts on iOS is annoying. This extension allows you to easily present alerts on view controllers. It also improves UIAlertAction by adding a quicker more Swift-like shorthand for making actions.
//
// UIViewController+ShorthandAlerts.swift
//
// Created by Ayden Panhuyzen on 2017-07-31.
// Copyright © 2017-2018 Ayden Panhuyzen. All rights reserved.
// https://gist.github.com/aydenp
//
import UIKit
@smileyborg
smileyborg / SelfSizingTableHeaderAndTableFooterViews.swift
Last active February 9, 2024 09:53
How to manually self-size UITableView tableHeaderView/tableFooterView in iOS 11
// For the best results, your tableHeaderView/tableFooterView should be a UITableViewHeaderFooterView with your content inside the contentView.
let tableHeaderView = UITableViewHeaderFooterView()
let fittingSize = CGSize(width: tableView.bounds.width - (tableView.safeAreaInsets.left + tableView.safeAreaInsets.right), height: 0)
let size = tableHeaderView.systemLayoutSizeFitting(fittingSize, withHorizontalFittingPriority: .required, verticalFittingPriority: .fittingSizeLevel)
tableHeaderView.frame = CGRect(origin: .zero, size: size)
tableView.tableHeaderView = tableHeaderView
// When you set this view to the tableHeaderView/tableFooterView on the table view, the table view will preserve the existing size of its frame.
// If you need to change the size, remove the tableHeaderView/tableFooterView, set a new frame on it, then re-set it on the table view again.
@brennanMKE
brennanMKE / EventEmitter.swift
Last active March 7, 2024 04:04
React Native Event Emitter for RCTEventEmitter in Objective-C and Swift
class EventEmitter
/// Shared Instance.
public static var sharedInstance = EventEmitter()
// ReactNativeEventEmitter is instantiated by React Native with the bridge.
private static var eventEmitter: ReactNativeEventEmitter!
private init() {}
@nesquena
nesquena / PatternEditableBuilder.java
Last active October 21, 2022 10:20
PatternEditableBuilder - Easy way to create colored clickable spans within a TextView!
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.regex.Matcher;
@codelynx
codelynx / ThemableViewController.swift
Created March 19, 2016 12:21
Declaring variable to keep UIViewController that conforms to protocol Themeable
protocol Themeable {
var viewController: UIViewController { get }
}
extension Themeable where Self: UIViewController {
var viewController: UIViewController { return self }
}
class ThemeableViewController: UIViewController, Themeable {}
let themeable: Themeable = ThemeableViewController()