Skip to content

Instantly share code, notes, and snippets.

// MARK: - textfield Delegate functions
// navigation through textfields with return key
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == self.emailField {
self.passwordField.becomeFirstResponder()
}else if textField == self.passwordField{
self.emailField.becomeFirstResponder()
}
return false
}
@wassim93
wassim93 / Red asterisk .swift
Last active July 10, 2022 07:22
adding required asterisk in textfield swift
extension UITextField {
func addRequiredAsterisk() {
let asterisk = UILabel()
let string = ("*" as NSString).range(of: "*")
asterisk.frame = CGRect.init(x: self.frame.size.width-5, y: self.frame.size.height/2, width: 10, height: 10)
let redAst = NSMutableAttributedString(string: "*", attributes: [.foregroundColor: UIColor.red])
redAst.addAttribute(NSAttributedString.Key.font,value:UIFont.systemFont(ofSize: 12, weight: .regular), range: string)
asterisk.attributedText = redAst
self.addSubview(asterisk)
@wassim93
wassim93 / bottom border textfield.swift
Last active April 21, 2022 03:57
adding bottom border to textfield swift
import UIKit.UITextField
extension UITextField {
func underlined(color:UIColor){
let border = CALayer()
let width = CGFloat(1.0)
border.borderColor = color.cgColor
border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: self.frame.size.height)
border.borderWidth = width
self.layer.addSublayer(border)
@wassim93
wassim93 / 2 lines title tabbar.swift
Last active January 14, 2022 12:06
making UITabbar title label in 2 lines Swift
// we need to implement this inside viewwillLayoutSubviews. to update the ui and make it works
// in this example in going to customize my 3rd tab bar item only
override func viewWillLayoutSubviews() {
// acess to list of tab bar items
if let items = self.tabBar.items {
// in each item we have a view where we find 2 subviews imageview and label
// in this example i would like to change
// access to item view
let viewTabBar = items[2].value(forKey: "view") as? UIView
@wassim93
wassim93 / String+Localization.swift
Created February 20, 2020 15:55 — forked from Juanpe/String+Localization.swift
String extension to localize more easy way
extension String {
var localized: String {
return NSLocalizedString(self, comment: "\(self)_comment")
}
func localized(_ args: [CVarArg]) -> String {
return localized(args)
}
@wassim93
wassim93 / loadGifExtension.swift
Last active August 23, 2021 02:36
load gif image extension
import UIKit
import ImageIO
extension UIImage {
public class func gifImageWithData(data: NSData) -> UIImage? {
guard let source = CGImageSourceCreateWithData(data, nil) else {
print("image doesn't exist")
return nil
}
@wassim93
wassim93 / WKWebview programatically.swift
Last active August 23, 2021 02:39
load WKWebview programatically with accepting invalid ssl
import UIKit
import WebKit
class PrivacyPolicyViewController: UIViewController,WKNavigationDelegate {
var webView: WKWebView!
@IBOutlet weak var containerView: UIView!
override func viewDidLoad() {
@wassim93
wassim93 / Custom SegmentedControl.Swift
Last active August 23, 2021 02:35
Custom SegmentedControl Swift
//
// CustomSegmentedControl.swift
// coop
//
// Created by Ben Hassen on 2020. 02. 27..
// Copyright © 2020. Ben Hassen. All rights reserved.
//
import UIKit
protocol CustomSegmentedControlDelegate:class {
@wassim93
wassim93 / prev_next Button collectionView.swift
Last active August 23, 2021 02:35
previous/next buttons to show item in collection view
@IBAction func nextPressed(_ sender: Any) {
let collectionBounds = collectionView.bounds
let contentOffset = CGFloat(floor(collectionView.contentOffset.x + collectionBounds.size.width+5))
self.moveCollectionToFrame(contentOffset: contentOffset)
}
@IBAction func backPressed(_ sender: Any) {
let collectionBounds = collectionView.bounds
let contentOffset = CGFloat(floor(collectionView.contentOffset.x - collectionBounds.size.width+5))
self.moveCollectionToFrame(contentOffset: contentOffset)
}
@wassim93
wassim93 / constraintLayout.swift
Last active August 23, 2021 02:34
constraintLayout Extension
//
// UIViewExtension.swift
// coop
//
// Created by Ben Hassen on 2020. 02. 21..
// Copyright © 2020. Ben Hassen. All rights reserved.
//
import UIKit