Skip to content

Instantly share code, notes, and snippets.

View leoiphonedev's full-sized avatar

Aman Aggarwal leoiphonedev

View GitHub Profile
@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 / 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 / UITapGesture.swift
Last active October 30, 2024 21:26
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 / 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")
}
}
//
// ViewController.swift
// SendMailAttachment
//
// Created by Aman Aggarwal on 03/12/18.
// Copyright © 2018 Aman Aggarwal. All rights reserved.
//
import UIKit
import MessageUI
@leoiphonedev
leoiphonedev / SendMailAttachment-ViewController-3.swift
Created December 12, 2018 06:01
COnformimng to MFMailComposeViewControllerDelegate so that we get notified on user certain action with mail composer view
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@leoiphonedev
leoiphonedev / SendMailAttachment-ViewController-2.swift
Created December 12, 2018 05:07
Code for presenting mail composer (desfualt) when user tap on send mail button
@IBAction func sendEmail(_ sender: Any) {
if MFMailComposeViewController.canSendMail() {
let mailComposer = MFMailComposeViewController()
mailComposer.setSubject("Update about ios tutorials")
mailComposer.setMessageBody("What is the update about ios tutorials on youtube", isHTML: false)
mailComposer.setToRecipients(["[email protected]"])
self.present(mailComposer, animated: true
, completion: nil)
} else {
@leoiphonedev
leoiphonedev / SendMailAttachment-ViewController-1.swift
Created December 12, 2018 05:04
Importing MessageUI framework
import MessageUI