Skip to content

Instantly share code, notes, and snippets.

@arpitdsoni
arpitdsoni / PlaceHolderTextEditor.swift
Created February 22, 2021 01:03
SwiftUI TextEditor with placeholder and border.
struct PlaceHolderTextEditor: View {
let placeholder: String
@Binding var text: String
var body: some View {
ZStack(alignment: Alignment(horizontal: .leading, vertical: .top)) {
if text.isEmpty {
Text(placeholder)
.foregroundColor(Color(.label))
.padding(.top, 10)
}
@arpitdsoni
arpitdsoni / asset-names-swift5.stencil
Created December 4, 2020 14:52
SwiftGen asset names template
// swiftlint:disable all
// Generated using SwiftGen — https://github.com/SwiftGen/SwiftGen
{% if catalogs %}
{% set enumName %}{{param.enumName|default:"AssetNames"}}{% endset %}
{% set forceNamespaces %}{{param.forceProvidesNamespaces|default:"false"}}{% endset %}
{% set accessModifier %}{% if param.publicAccess %}public{% else %}internal{% endif %}{% endset %}
{% macro enumStringsBlock assets %}
{% for asset in assets %}
{% if asset.type != "group" %}
protocol HyperLinkTextViewDelegate: class {
func textView(textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool
}
/// converts text to attributedText with clickable strings
class HyperLinkTextView: UITextView, UITextViewDelegate {
weak var hyperLinkTextViewDelegate: HyperLinkTextViewDelegate?
var additionalAttributesForLinkText: [NSAttributedString.Key : Any] = [:]
@arpitdsoni
arpitdsoni / images-swift5.stencil
Created September 16, 2020 16:37
Modified SwiftGen xcassets template for images. Makes static const of type UIImage or NSImage.
// swiftlint:disable all
// Generated using SwiftGen — https://github.com/SwiftGen/SwiftGen
{% if catalogs %}
{% set enumName %}{{param.enumName|default:"Images"}}{% endset %}
{% set enumNameForStrings %}{{param.enumNameForStrings|default:"ImageNames"}}{% endset %}
{% set imageType %}{{param.imageTypeName|default:"Image"}}{% endset %}
{% set forceNamespaces %}{{param.forceProvidesNamespaces|default:"false"}}{% endset %}
{% set accessModifier %}{% if param.publicAccess %}public{% else %}internal{% endif %}{% endset %}
#if os(macOS)
@arpitdsoni
arpitdsoni / colors-swift5.stencil
Last active September 16, 2020 16:40
Modified SwiftGen xcassets template to make static constant of type UIColor or NSColor
// swiftlint:disable all
// Generated using SwiftGen — https://github.com/SwiftGen/SwiftGen
{% if catalogs %}
{% set enumName %}{{param.enumName|default:"Colors"}}{% endset %}
{% set colorType %}{{param.colorTypeName|default:"Color"}}{% endset %}
{% set forceNamespaces %}{{param.forceProvidesNamespaces|default:"false"}}{% endset %}
{% set accessModifier %}{% if param.publicAccess %}public{% else %}internal{% endif %}{% endset %}
#if os(macOS)
import AppKit
protocol SafeValueForKeyPath {}
extension SafeValueForKeyPath where Self: CFXManagedObject {
func safeValueForKeyPath<Value>(_ keyPath: KeyPath<Self, Value?>) -> Value? {
let keyPathStr = NSExpression(forKeyPath: keyPath).keyPath
return managedObjectContext?.performAndWait {
return value(forKeyPath: keyPathStr) as? Value
}
}
}
import UIKit
class HyperlinkLabel: UILabel {
var stringAndUrl: [String: URL] = [:] {
didSet {
attributedText = attributedText()
}
}
@arpitdsoni
arpitdsoni / fastlane_build_bump.rb
Created November 9, 2019 12:26
develop branch as source of truth
desc "Gets the build number from develop, returns incremented number"
lane :incremented_build_number do
# the truth for the current build number is on develop, switch branch and get the number
orig_git_branch = git_branch
sh("git checkout develop")
build_number = get_build_number.to_i + 1
sh("git checkout #{orig_git_branch}")
build_number
end
@arpitdsoni
arpitdsoni / get_devices_dev_profile.rb
Created October 24, 2019 21:43
Save all devices for a dev provisioning profile using fastlane
desc "Save all devices for a dev provisioning profile to devices.txt"
lane :get_devices_dev_profile do
require "spaceship"
Spaceship::Portal.login()
bundle_id = CredentialsManager::AppfileConfig.try_fetch_value(:app_identifier).first
profile = Spaceship::Portal.provisioning_profile.development.find_by_bundle_id(bundle_id: bundle_id).first
devices = profile.devices
File.open('devices.txt', 'w') do |f|
f.puts "Device ID\tDevice Name"
devices.each do |device|
@arpitdsoni
arpitdsoni / OptionSetStringIntializing.swift
Created July 2, 2019 02:54
Create OptionSet from JSON in Swift
public protocol OptionSetStringIntializing {
init?(_ stringValue: String)
}
public extension OptionSet {
static func decode<T>(from strings: [String]) -> T where T: OptionSet, T: OptionSetStringIntializing {
var options: T = []
strings.forEach { (str) in