How to convert UIColor to HexColor in swift
func hexString(from color: UIColor?) -> String? {
let components = color?.cgColor.components
let r: CGFloat? = components?[0]
let g: CGFloat? = components?[1]
let b: CGFloat? = components?[2]
return String(format: "#%02lX%02lX%02lX", lroundf(Float((r ?? 0.0) * 255)),
lroundf(Float((g ?? 0.0) * 255)), lroundf(Float((b ?? 0.0) * 255)))
}
How to Save Image in Document Directory Folder in swift
func saveImageDocumentDirectory(imageName: String) {
let fileManager = FileManager.default
let path = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("FolderName")
if !fileManager.fileExists(atPath: path) {
try! fileManager.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
}
let url = NSURL(string: path)
let imagePath = url!.appendingPathComponent(imageName)
let urlString: String = imagePath!.absoluteString
let imageData = image.jpegData(compressionQuality: 1.0)
fileManager.createFile(atPath: urlString as String, contents: imageData, attributes: nil)
}
How to Create DataBase in Share App Group Directory In Swift
var pathToDatabase: String!
var AppGroupContainerUrl : URL?
let databaseFileName = "data.sqlite"
AppGroupContainerUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.companyname.appname")?.appendingPathComponent(databaseFileName)
if let groupContainerURL = AppGroupContainerUrl {
let groupContainerString = groupContainerURL.path
pathToDatabase = groupContainerString
}
How to Set Custom Blur effect view in Swift
let blurEffect = (NSClassFromString("_UICustomBlurEffect") as! UIBlurEffect.Type).init()
blurEffect.setValue(5, forKey: "blurRadius")
let visualEffect = UIVisualEffectView.init(effect: blurEffect)
let bluredView = UIVisualEffectView.init(effect: blurEffect)
bluredView.contentView.addSubview(visualEffect)
visualEffect.frame = UIScreen.main.bounds
bluredView.frame = UIScreen.main.bounds
self.view.insertSubview(bluredView, at: 0)
How to Set Corner Radius Top Left and Top Right
let maskPath = UIBezierPath(roundedRect: btn.bounds,
byRoundingCorners: [.bottomLeft, .bottomRight],
cornerRadii: CGSize(width: 8.0, height: 8.0))
let shape = CAShapeLayer()
shape.path = maskPath.cgPath
btn.layer.mask = shape
How to Convert UIView Into UIImage In Swift
extension UIImage{
convenience init(view: UIView) {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.isOpaque, 0.0)
view.drawHierarchy(in: view.bounds, afterScreenUpdates: false)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.init(cgImage: (image?.cgImage)!)
//let img = UIImage.init(view: myView) //use to get image
}
}
how to use pragma mark in Swift
– Xcode 6 on, you have to use
// MARK: – your comment here
(is used to your section heading)
// TODO: – your comment here
(is used to your to do item)
// FIXME: – your comment here
(is used to your bug fix reminder)
These preprocessor features to set structure of the coding.
how to get length of string in swift
var Demo : String = “Swift”
Swift 4 : Demo.count
Swift 2 : Demo.characters.count
// Swift 4
extension String {
var length: Int { return self.count }
}
How can I used to String slicing subscripts in Swift 4?
let str = "Hello, world"
let index = str.index(of: ",")!
let newStr = str[..<index]
let newStr = str[index...]
how to get String before specific character
var delimiter = "-"
var newstring = "objective C-swift"
var token = newstring.components(separatedBy: delimiter)
print (token[0])
How to remove tableview separator in swift
self.tableView.separatorStyle = .none
//OR if you are using storyboard goto Interface Builder and set separator = none