1 使用十六进制颜色:
UIColor.colorWithHexString("#f5f4f4")
对UIColor的扩展如下:
import Foundation import UIKit extension UIColor { class func colorWithHexString(stringToConvert:String)->UIColor{ NSCharacterSet.whitespaceAndNewlineCharacterSet() var cString:NSString = stringToConvert.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).uppercaseString if (cString.length < 6){ return UIColor.clearColor(); } if cString.hasPrefix("0X"){ cString = cString.substringFromIndex(2) } if cString.hasPrefix("#"){ cString = cString.substringFromIndex(1) } if (cString.length != 6){ return UIColor.clearColor(); } var range:NSRange = NSMakeRange(0,2); let rString:String = cString.substringWithRange(range) range.location = 2; let gString:String = cString.substringWithRange(range) range.location = 4; let bString:String = cString.substringWithRange(range) var r:CUnsignedInt=0 var g:CUnsignedInt=0 var b:CUnsignedInt=0 NSScanner(string: rString).scanHexInt(&r) NSScanner(string: gString).scanHexInt(&g) NSScanner(string: bString).scanHexInt(&b) // NSScanner.scannerWithString(rString).scanHexInt(&r) // NSScanner.scannerWithString(gString).scanHexInt(&g) // NSScanner.scannerWithString(bString).scanHexInt(&b) return UIColor(red: CGFloat(r)/255.0,green: CGFloat(g)/255.0,blue: CGFloat(b)/255.0,alpha: 1.0) } }
2 获取当前年月:
let today = NSCalendar.currentCalendar().components([NSCalendarUnit.NSEraCalendarUnit,NSCalendarUnit.NSYearCalendarUnit,NSCalendarUnit.NSMonthCalendarUnit,NSCalendarUnit.NSDayCalendarUnit],fromDate: NSDate()) as NSDateComponents //获取当前月第一天是星期几 let startNumber:Int = Datecalculate.getStart( today.month,year:today.year)
Datecalculate.getStart定义在最后
3 设置按钮样式:
//文字色 button.setTitleColor(UIColor.colorWithHexString("#b8b7b7"),forState: UIControlState.Normal) //边框色 button.layer.borderColor = CGColorCreateCopy(UIColor.colorWithHexString("#d9d6d6").CGColor) //填充色 button.backgroundColor = UIColor.colorWithHexString("#f5f4f4")
最终效果:
日期函数类:
import Foundation class Datecalculate: NSObject{ class func getDayCountOf(month:Int,year:Int)->Int{ switch (month) { case 1: return 31 case 3: return 31 case 4: return 30 case 5: return 31 case 6: return 30 case 7: return 31 case 8: return 31 case 9: return 30 case 10: return 31 case 11: return 30 case 12: return 31 default: break } if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) { return 29 } else { return 28 } } class func getStart(month:Int,year:Int)->Int{ var baseNumber:Int var startNumber:Int if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) { switch (month) { case 1: baseNumber = 0 case 2: baseNumber = 3 case 3: baseNumber = 4 case 4: baseNumber = 0 case 5: baseNumber = 2 case 6: baseNumber = 5 case 7: baseNumber = 0 case 8: baseNumber = 3 case 9: baseNumber = 6 case 10: baseNumber = 1 case 11: baseNumber = 4 case 12: baseNumber = 6 default: baseNumber = 0 } startNumber = (year + year/4 + year/400 - year/100 - 2 + baseNumber + 1) % 7 } else { switch (month) { case 1: baseNumber = 0 case 2: baseNumber = 3 case 3: baseNumber = 3 case 4: baseNumber = 6 case 5: baseNumber = 1 case 6: baseNumber = 4 case 7: baseNumber = 6 case 8: baseNumber = 2 case 9: baseNumber = 5 case 10: baseNumber = 0 case 11: baseNumber = 3; break; case 12: baseNumber = 5 default: baseNumber = 0 } startNumber = (year + year/4 + year/400 - year/100 - 1 + baseNumber + 1) % 7 } return startNumber } class func getLayer(year:Int,month:Int)->Int{ let monthDay = self.getDayCountOf(month,year: year) let startNumber = self.getStart(month,year: year) var newLayer = startNumber+monthDay let layer = ceilf(Float(startNumber+monthDay)/7) return Int(layer); } }原文链接:https://www.f2er.com/swift/325878.html