iOS App Basics 2
Here is the second post for some functions for basic code makes easy.
Create the one Extension file for few basic feature like below:
-> Create Extension.swift file in App Basic folder.
1. This extension is used to add max length of TextField entry.
Create the one Extension file for few basic feature like below:
-> Create Extension.swift file in App Basic folder.
1. This extension is used to add max length of TextField entry.
extension UITextField {
@IBInspectable var maxLength: Int
{
get {
guard let length = maxLengths[self] else {
return Int.max
}
return length
}
set {
maxLengths[self] = newValue
addTarget(self, action: #selector(limitLength), for: .editingChanged)
}
}
@objc func limitLength(textField:UITextField){
guard let prospectiveText = textField.text , prospectiveText.count > maxLength
else
{
return
}
let selection = selectedTextRange
let index = prospectiveText.index(prospectiveText.startIndex, offsetBy: maxLength)
text = prospectiveText.substring(to: index)
selectedTextRange = selection
}
}
extension UIView {
//It is to use to add shadow to any UIView using storyboard
@IBInspectable var shadow:Bool {
set {
if newValue {
layer.shadowColor = UIColor.black.cgColor
layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
layer.shadowRadius = 2.0
layer.shadowOpacity = 0.3
layer.masksToBounds = false
}
}
get {
if layer.shadowColor != nil {
return true
}
else {
return false
}
}
}
//It is to use to add border to any UIView
func setBoder(color: UIColor, width: CGFloat = 1) {
self.layer.borderColor = color.cgColor
self.layer.borderWidth = width
}
//It is to use to add Corner Radius to any UIView
func setCornerRadius(radius: CGFloat) {
self.layer.cornerRadius = radius
self.clipsToBounds = true
}
//It is to use to add shadow to any UIView programmatically
func setShadow() {
self.layer.shadowOpacity = 0.65
self.layer.shadowOffset = CGSize(width: 0, height: 5)
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowRadius = 8
self.layer.masksToBounds = false
}
//It is to use to add border color to any UIView using storyboard
@IBInspectable var borderColor:UIColor? {
set {
layer.borderColor = newValue!.cgColor
}
get {
if let color = layer.borderColor {
return UIColor(cgColor:color)
}
else {
return nil
}
}
}
//It is to use to add border Width to any UIView using storyboard
@IBInspectable var borderWidth:CGFloat {
set {
layer.borderWidth = newValue
}
get {
return layer.borderWidth
}
}
//It is to use to add corner radius to any UIView using storyboard
@IBInspectable var cornerRadius:CGFloat {
set {
layer.cornerRadius = newValue
clipsToBounds = newValue > 0
}
get {
return layer.cornerRadius
}
}
}
use full for me as i am new to ios .
ReplyDelete