Skip to content

Instantly share code, notes, and snippets.

@abhi21git
abhi21git / UIView+AddSubview+Constraints.swift
Last active May 14, 2023 13:05
Add subviews with constraints for cleaner code
//
// UIView+AddSubview+Constraints.swift
//
// Created by Abhishek Maurya on 22/04/23.
//
extension UIView {
func addSubview(_ subView: UIView, considerSafeArea: Bool = false, with constraints: Array<Constraints>) -> [NSLayoutConstraint] {
addSubview(subView)
return subView.addConstraints(in: self, considerSafeArea: considerSafeArea, with: constraints)
@abhi21git
abhi21git / UIAnimatableLabel.swift
Created April 4, 2023 11:08
An animatable label with push text animation which can either be used from storyboard or programatically.
//
// UIAnimatableLabel.swift
//
// Created by Abhishek Maurya on 04/04/23.
//
import UIKit
// MARK: - UIAnimatableLabel
final class UIAnimatableLabel: UILabel {
@abhi21git
abhi21git / CollectionSnapFlowLayout.swift
Last active July 24, 2023 14:08
Make your cells perfectly scroll to content
class CollectionSnapFlowLayout: UICollectionViewFlowLayout {
var onlyScrollToNext: Bool = true
override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
guard let collectionView = self.collectionView else {
let latestOffset = super.targetContentOffset(forProposedContentOffset: proposedContentOffset, withScrollingVelocity: velocity)
return latestOffset
} /// Add some snapping behaviour so that the cell is always centered
var proposedX: CGFloat = proposedContentOffset.x
class GradientTextLabel: UILabel {
var gradientColors: [CGColor] = [UIColor.orangeRed.cgColor, UIColor.brightRed.cgColor, UIColor.warmPurple.cgColor, UIColor.darkishBlue.cgColor]
var locations: [CGFloat]? = [0.0, 0.25, 0.5, 0.75]
override func drawText(in rect: CGRect) {
if let gradientColor = drawGradientColor(in: rect, colors: gradientColors) {
self.textColor = gradientColor
}
super.drawText(in: rect)
}
@abhi21git
abhi21git / GradientView.swift
Created November 1, 2022 07:52
Create gradients and cache them to reduce memory footprint.
class GradientView: UIImageView {
var gradientColors: [CGColor] = [UIColor.greyishBrown.withAlphaComponent(0.2).cgColor, UIColor.greyishBrown.cgColor]
var locations: [CGFloat]? = [0.0, 1.0]
override func layoutSubviews() {
self.contentMode = .scaleToFill
self.backgroundColor = .hotPink
self.image = drawGradientColor(in: self.bounds, colors: gradientColors)
}
@abhi21git
abhi21git / CollectionCellSize.swift
Last active November 1, 2022 07:54
Dynamic cell size based on screenWidth
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let totalAvailableWidth = collectionView.bounds.width
let padding: CGFloat = 25
let interSpace: CGFloat = 15
let numOfCellInaRow: CGFloat = 3
let cellWidth: CGFloat = (totalAvailableWidth - (padding * 2) - (interSpace * (numOfCellInaRow - 1))) / numOfCellInaRow
/// padding * 2 = leading + trailing space, (interSpace * (numOfCellInaRow - 1)) = totalInterspacing as 3 cells will have 2 interspace
let cellSize = CGSize(width: cellWidth, height: cellWidth)
return cellSize
}
import UIKit
// Confirm this to your cell/header if using Prototype cell
public protocol ReusableView: AnyObject {
static var defaultReuseIdentifier: String { get }
}
extension ReusableView where Self: UIView {
public static var defaultReuseIdentifier: String {
return String(describing: self)
@abhi21git
abhi21git / ExtensionURLRequest.swift
Last active February 19, 2024 12:23
Swift cURL Printer
//
// ExtensionURLRequest.swift
//
// Created by Abhishek Maurya on 16/07/20.
// Copyright © 2020. All rights reserved.
//
import Foundation
extension URLRequest {