Skip to content

Instantly share code, notes, and snippets.

View sourleangchhean168's full-sized avatar
🎯
Focusing

Sour LeangChhean sourleangchhean168

🎯
Focusing
View GitHub Profile
@sourleangchhean168
sourleangchhean168 / PaddedLabel.swift
Last active April 23, 2024 04:27
Padded Label in Swift 5
//Created by SOUR LEANGCHHEAN
class PaddedLabel: UILabel {
// MARK: - Properties
var padding: UIEdgeInsets = .zero {
didSet {
invalidateIntrinsicContentSize()
}
@sourleangchhean168
sourleangchhean168 / password.swift
Last active April 23, 2024 04:29
Check Valid Password
func isValidPassword(password: String) -> Bool {
// define validation rules here
let passwordRegEx = "(?=.*[A-Z])(?=.*[0-9])(?=.*[a-z]).{8,}"
let passwordPred = NSPredicate(format: "SELF MATCHES %@", passwordRegEx)
return passwordPred.evaluate(with: password)
}
@sourleangchhean168
sourleangchhean168 / email.swift
Created January 25, 2023 09:18
Check Valid Email
func isValidEmail(email: String) -> Bool {
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
let emailPred = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
return emailPred.evaluate(with: email)
}
@sourleangchhean168
sourleangchhean168 / base64toImage.swift
Last active May 3, 2022 08:10
Convert Image to base64 and base64 to image (Encode and Decode Image to Base64) in Swift 5
//Swift 5
//Encoding from image to base64
func convertImageToBase64String (img: UIImage) -> String {
return img.jpegData(compressionQuality: 1)?.base64EncodedString() ?? ""
}
//Decoding from base 64 to image
func convertBase64StringToImage (imageBase64String:String) -> UIImage {
let imageData = Data(base64Encoded: imageBase64String)
@sourleangchhean168
sourleangchhean168 / PasswordGenerator.php
Created May 2, 2022 07:23
Password Random Generator in PHP
<?php
function randomPassword() {
$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
$pass = array();
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
for ($i = 0; $i < 8; $i++) {
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
}
return implode($pass); //turn the array into a string
@sourleangchhean168
sourleangchhean168 / AspectRatio.swift
Last active May 2, 2022 07:16
Aspect Ratio in Swift
//iOS 13, swift 5.1
//Example programmatic constraints with aspect ratio.
imageView.translatesAutoresizingMaskIntoConstraints = false
//A) 4:3
imageView.heightAnchor.constraint(equalTo: widthAnchor, multiplier: 3.0/4.0).isActive = true
//B) 16:9
@sourleangchhean168
sourleangchhean168 / dataPrettyPrintJSONString.swift
Last active February 21, 2020 03:45
Print Data in Pretty JSON Format in Swift 5.1
import Foundation
extension Data {
var prettyPrintedJSONString: NSString? { /// NSString gives us a nice sanitized debugDescription
guard let object = try? JSONSerialization.jsonObject(with: self, options: []),
let data = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted]),
let prettyPrintedString = NSString(data: data, encoding: String.Encoding.utf8.rawValue) else { return nil }
return prettyPrintedString
}
@sourleangchhean168
sourleangchhean168 / biometrictype.swift
Last active January 31, 2020 17:20
Check device support biometric type in iOS
//Noted: This code is written in Swift 5.1
//By: Sour Leangchhean
import LocalAuthentication
//Mark: BiometricType for define type
enum BiometricType{
case touchID
case faceID
case none
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row + 1 == list.count {
if currentPage != totalPage{
self.getList(currentPage + 1, max_row)
}
}
}
@sourleangchhean168
sourleangchhean168 / .gitignore
Created August 20, 2019 09:51 — forked from simonexmachina/.gitignore
Example .gitignore file for iOS projects
## Build generated
build/
DerivedData
build.xcarchive
## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3