Skip to content

Instantly share code, notes, and snippets.

View Tulakshana's full-sized avatar

Tula Tulakshana

View GitHub Profile
@Tulakshana
Tulakshana / Beacon.swift
Last active October 11, 2018 22:31
Using this class you can make an iPhone behave like an iBeacon :)
import Foundation
import CoreBluetooth
import CoreLocation
class Beacon: NSObject {
static let shared = Beacon()
var localBeacon: CLBeaconRegion!
var beaconPeripheralData: [String: AnyObject]?
@Tulakshana
Tulakshana / CustomSlider.swift
Last active June 28, 2023 14:35
A subclass of UISlider which has a tool tip indicating the current value of the slider.
import UIKit
class CustomSlider: UISlider {
private var toolTip: ToolTipPopupView?
override func awakeFromNib() {
super.awakeFromNib()
self.initToolTip()
@Tulakshana
Tulakshana / MovieTransitionsVC.swift
Last active January 12, 2023 08:25 — forked from SheffieldKevin/movietransitions.swift
Make a movie with transitions with AVFoundation and swift. Add an UIActivityIndicatorView and an UIButton and connect appropriately. You will also need to attach few videos to the project.
//
// MovieTransitionsVC.swift
// VideoAnimations
//
// Created by Tula on 2018-06-14.
// Copyright © 2018 Tula. All rights reserved.
//
import UIKit
import AVFoundation
@Tulakshana
Tulakshana / ExitHandler.swift
Created May 11, 2018 21:50
Experimental code to exit an iOS app.
@IBAction func btnExit0Tapped (sender: UIButton) {
exit(0) // Not recommended by Apple https://developer.apple.com/library/content/qa/qa1561/_index.html and app crashes
}
@IBAction func btnSuspendTapped (sender: UIButton) {
UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil) // App exits gracefully
}
@IBAction func btnFatalErrorTapped (sender: UIButton) {
fatalError() // App will crash
@Tulakshana
Tulakshana / StringReplace.swift
Last active November 22, 2017 15:55
This String extension contains a convenient method to replace characters in a CharacterSet with a given String.
func stringByReplacing(charSet: CharacterSet, with string: String) -> String {
let components = self.components(separatedBy: charSet)
return components.joined(separator: string)
}
@Tulakshana
Tulakshana / UIViewFirstResponder.swift
Created November 16, 2017 10:25
An UIView extension with a method to return the frame of the first responder.
extension UIView {
func frameOfFirstResponder() -> CGRect? {
if self.isFirstResponder {
return self.frame
}
for view in self.subviews {
if let frame = view.frameOfFirstResponder() {
return frame
}
@Tulakshana
Tulakshana / CustomButton.swift
Created November 13, 2017 12:55
A custom UIButton which could have different background colours for enabled and disabled states.
class CustomButton: UIButton {
@IBInspectable private var enabledBackgroundColor: UIColor?
@IBInspectable private var disabledBackgroundColor: UIColor?
override var isEnabled: Bool {
set {
if newValue {
self.backgroundColor = enabledBackgroundColor
} else {
self.backgroundColor = disabledBackgroundColor
@Tulakshana
Tulakshana / AttribManipulator.swift
Last active October 17, 2017 13:09
A convenience method create an attributed string from a plain html string and apply a font of your choice. The method could be modified to support any fonts and styles.
enum FontName: String {
case timesNewRoman = "TimesNewRomanPSMT"
case timesNewRomanItalic = "TimesNewRomanPS-ItalicMT"
case timesNewRomanBold = "TimesNewRomanPS-BoldMT"
}
extension UIFont {
class var sanFranciscoRegular: UIFont {
if let font = UIFont.init(name: "SFUIText-Regular", size: 15) {
@Tulakshana
Tulakshana / URICoder
Created June 8, 2017 12:28
This method will mimic javascript function "encodeURIComponent(<string to be encoded>)" in Objective C
+ (NSString *) encodeString: (NSString *) string {
NSMutableString *output = [NSMutableString string];
const unsigned char *source = (const unsigned char *)[string UTF8String];
int sourceLen = (int)strlen((const char *)source);
for (int i = 0; i < sourceLen; ++i) {
const unsigned char thisChar = source[i];
if (thisChar == ' '){
[output appendString:@"%20"];
} else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' ||
@Tulakshana
Tulakshana / MyAPI.swift
Last active April 20, 2023 12:31
Handling re-direction with URLSession (Swift)
import UIKit
@objc class MyAPI: NSObject {
static let keyError: String = "Error"
static let keyResult: String = "Result"
static let unknownError: String = "Unknown error"
static let notificationMyAPIDidEnd = Notification.Name(rawValue: "notificationMyAPIDidEnd")