Skip to content

Instantly share code, notes, and snippets.

View leoiphonedev's full-sized avatar

Aman Aggarwal leoiphonedev

View GitHub Profile
@leoiphonedev
leoiphonedev / UITapGesture.swift
Last active November 28, 2023 12:45
Extension for UITapGesture that contains a function to detect range of particular text in UILabel's text.
extension UITapGestureRecognizer {
func didTapAttributedTextInLabel(label: UILabel, inRange targetRange: NSRange) -> Bool {
// Create instances of NSLayoutManager, NSTextContainer and NSTextStorage
let layoutManager = NSLayoutManager()
let textContainer = NSTextContainer(size: CGSize.zero)
let textStorage = NSTextStorage(attributedString: label.attributedText!)
// Configure layoutManager and textStorage
layoutManager.addTextContainer(textContainer)
@leoiphonedev
leoiphonedev / ViewControllerDetectTextUILabel-2.swift
Created August 7, 2019 05:52
Selector for UITapGestre added to UILable and telling us wether user tap on desired text or not
//MARK:- tappedOnLabel
@objc func tappedOnLabel(_ gesture: UITapGestureRecognizer) {
guard let text = self.lblTermsAndConditions.text else { return }
let privacyPolicyRange = (text as NSString).range(of: "privacy policy")
let termsAndConditionRange = (text as NSString).range(of: "terms and condition")
if gesture.didTapAttributedTextInLabel(label: self.lblTermsAndConditions, inRange: privacyPolicyRange) {
print("user tapped on privacy policy text")
} else if gesture.didTapAttributedTextInLabel(label: self.lblTermsAndConditions, inRange: termsAndConditionRange){
print("user tapped on terms and conditions text")
}
@leoiphonedev
leoiphonedev / ViewControllerDetectTextUILabel.swift
Created August 7, 2019 05:46
Adding UITapGestureRecognizer to UILabel
self.lblTermsAndConditions.isUserInteractionEnabled = true
let tapgesture = UITapGestureRecognizer(target: self, action: #selector(tappedOnLabel(_ :)))
tapgesture.numberOfTapsRequired = 1
self.lblTermsAndConditions.addGestureRecognizer(tapgesture)
@leoiphonedev
leoiphonedev / Populating our employeeArray with dummy data.swift
Created January 10, 2018 07:17
Populating our employee Array with dummy data in order to create csv file
class ViewController: UIViewController {
var employeeArray:[Dictionary<String, AnyObject>] = Array()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
for i in 1...10 {
var dct = Dictionary<String, AnyObject>()
dct.updateValue(i as AnyObject, forKey: "EmpID")
dct.updateValue("NameForEmplyee id = \(i)" as AnyObject, forKey: "EmpName")
func animatePulsatingLayerAt(index:Int) {
//Giving color to the layer
pulseArray[index].strokeColor = UIColor.darkGray.cgColor
//Creating scale animation for the layer, from and to value should be in range of 0.0 to 1.0
// 0.0 = minimum
//1.0 = maximum
let scaleAnimation = CABasicAnimation(keyPath: "transform.scale")
scaleAnimation.fromValue = 0.0
@leoiphonedev
leoiphonedev / Saving checkbox state in uitableview.swift
Created June 2, 2020 09:19
How to maintain checkbox state when scrolling uitableview
//
// ViewController.swift
// addCheckBoxonTable-Tutorial
//
// Created by Aman Aggarwal on 2/1/18.
// Copyright © 2018 iostutorialjunction.com. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UITableViewDataSource {
@leoiphonedev
leoiphonedev / Decode base64 string to UIImafe in swift4.swift
Created February 23, 2018 15:21
Decode base64 string to UIImafe in swift4
func getImageFromBase64(base64:String) -> UIImage {
let data = Data(base64Encoded: base64)
return UIImage(data: data!)!
}
@leoiphonedev
leoiphonedev / AdaptableSizeButton.swift
Last active July 14, 2019 07:29
AdaptableSizeButton class enables the size of button as per the title set to UIButton via overriding intrinsicContentSize of UIButton
class AdaptableSizeButton: UIButton {
override var intrinsicContentSize: CGSize {
let labelSize = titleLabel?.sizeThatFits(CGSize(width: frame.size.width, height: CGFloat.greatestFiniteMagnitude)) ?? .zero
let desiredButtonSize = CGSize(width: labelSize.width + titleEdgeInsets.left + titleEdgeInsets.right, height: labelSize.height + titleEdgeInsets.top + titleEdgeInsets.bottom)
return desiredButtonSize
}
}
@leoiphonedev
leoiphonedev / AppDelegate-localnotification.swift
Created May 20, 2019 11:23
Asking for user permissions in order to send hm notifications
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if granted {
print("User gave permissions for local notifications")
}
}
@leoiphonedev
leoiphonedev / Add-shadow-to-all sides-of-UIView-swift4.swift
Created February 12, 2018 15:37
How to add shadow to all sides of UIView in swift 4
self.cntView.layer.cornerRadius = 13.0
self.cntView.layer.shadowColor = UIColor.lightGray.cgColor
self.cntView.layer.shadowOpacity = 0.5
self.cntView.layer.shadowRadius = 10.0
self.cntView.layer.shadowOffset = .zero
self.cntView.layer.shadowPath = UIBezierPath(rect: self.cntView.bounds).cgPath
self.cntView.layer.shouldRasterize = true
//Note:- cntView is an IBOutlet