Thursday, July 5, 2018

Set UIView Shadow

Set UIView Shadow


//MARK: View Shadow
    func viewShadow(myView: UIView) -> UIView {
        myView.layer.shadowColor = UIColor.black.cgColor
        myView.layer.shadowOpacity = 1
        myView.layer.shadowOffset = CGSize.zero
        myView.layer.shadowRadius = 1
        myView.layer.shadowPath = UIBezierPath(rect: myView.bounds).cgPath
        myView.layer.shouldRasterize = true
        return myView
    }

Time calculation from given date

This will show no of seconds, minutes, hours, days, weeks, months and year passed or yet to pass from given date.



public func timeAgoSince(_ date: Date) -> String {
    
    let calendar = Calendar.current
    let now = Date()
    let unitFlags: NSCalendar.Unit = [.second, .minute, .hour, .day, .month, .year]
    let components = (calendar as NSCalendar).components(unitFlags, from: date, to: now, options: [])
    
    if let year = components.year, year >= 2 {
        return "\(year) years ago"
    }
    
    if let year = components.year, year >= 1 {
        return "Last year"
    }
    
    if let month = components.month, month >= 2 {
        return "\(month) months ago"
    }
    
    if let month = components.month, month >= 1 {
      return "\(month) month ago"
        // return "Last month"
    }
    
//    if let week = components.weekOfYear, week >= 2 {
//        return "\(week) weeks ago"
//    }
//    
//    if let week = components.weekOfYear, week >= 1 {
//        return "Last week"
//    }
    
    if let day = components.day, day >= 2 {
        return "\(day) days ago"
    }
    
    if let day = components.day, day >= 1 {
        return "\(day) day ago"
        //return "Yesterday"
    }
    
    if let hour = components.hour, hour >= 2 {
        return "\(hour) hours ago"
    }
    
    if let hour = components.hour, hour >= 1 {
        return "An hour ago"
    }
    
    if let minute = components.minute, minute >= 2 {
        return "\(minute) minutes ago"
    }
    
    if let minute = components.minute, minute >= 1 {
        return "A minute ago"
    }
    
    if let second = components.second, second >= 3 {
        return "\(second) seconds ago"
    }
    
    if let year = components.year, year < 0 {
        if(abs(year) == 1){
            return "\(abs(year)) year to go"}
        else{
            return "\(abs(year)) years to go"}
    }
    
    if let month = components.month, month < 0 {
        if(abs(month) == 1){
            return "\(abs(month)) month to go"}
        else{
            return "\(abs(month)) months to go"}
    }
    
//    if let week = components.weekOfYear, week < 0 {
//        if(abs(week) == 1){
//            return "\(abs(week)) week to go"}
//        else{
//            return "\(abs(week)) weeks to go"}
/   }
    
    if let day = components.day, day < 0 {
        if(abs(day) == 1){
            return "\(abs(day)) day to go"}
        else{
            return "\(abs(day)) days to go"}
    }
    
    if let hour = components.hour, hour < 0 {
        if(abs(hour) == 1){
            return "\(abs(hour)) hour to go"}
        else{
            return "\(abs(hour)) hours to go"}
    }
    
    
    if let minute = components.minute, minute < 0 {
        if(abs(minute) == 1){
            return "\(abs(minute)) minute to go"}
        else{
            return "\(abs(minute)) minutes to go"}
    }
    if let sec = components.second, sec < 0 {
        if(abs(sec) == 1){
            return "\(abs(sec)) second to go"}
        else{
            return "\(abs(sec)) seconds to go"}
    }
    
    return "Just now"    
}

Upload UIImage as base64 String

Upload UIImage as Base64 String (Upload UIImage as string) //Select Pic From Camera or Gallery       @objc func btnPro...