This question already has answers here:
CGRectMake, CGPointMake, CGSizeMake, CGRectZero, CGPointZero is unavailable in Swift
(11 answers)
Closed 6 years ago.
I have a Gradient class which I am trying to convert to Swift 3 but I get the following error
'CGPointMake' is unavailable in swift
for
func configureGradientView() {
let color1 = topColor ?? self.tintColor as UIColor
let color2 = bottomColor ?? UIColor.black as UIColor
let colors: Array <AnyObject> = [ color1.cgColor, color2.cgColor ]
let layer = self.layer as! CAGradientLayer
layer.colors = colors
layer.startPoint = CGPointMake(startX, startY)
layer.endPoint = CGPointMake(endX, endY)
}
Can anyone help me out with what I can use instead of CGPointMake
Here's the full class;
#IBDesignable public class XGradientView: UIView {
#IBInspectable public var topColor: UIColor? {
didSet {
configureGradientView()
}
}
#IBInspectable public var bottomColor: UIColor? {
didSet {
configureGradientView()
}
}
#IBInspectable var startX: CGFloat = 0.0 {
didSet{
configureGradientView()
}
}
#IBInspectable var startY: CGFloat = 1.0 {
didSet{
configureGradientView()
}
}
#IBInspectable var endX: CGFloat = 0.0 {
didSet{
configureGradientView()
}
}
#IBInspectable var endY: CGFloat = 0.0 {
didSet{
configureGradientView()
}
}
public class func layeredClass() -> AnyClass {
return CAGradientLayer.self
}
public required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
configureGradientView()
}
override init(frame: CGRect) {
super.init(frame: frame)
configureGradientView()
}
public override func tintColorDidChange() {
super.tintColorDidChange()
configureGradientView()
}
func configureGradientView() {
let color1 = topColor ?? self.tintColor as UIColor
let color2 = bottomColor ?? UIColor.black as UIColor
let colors: Array <AnyObject> = [ color1.cgColor, color2.cgColor ]
let layer = self.layer as! CAGradientLayer
layer.colors = colors
layer.startPoint = CGPointMake(startX, startY)
layer.endPoint = CGPointMake(endX, endY)
}
}
In swift you can create a CGPoint in swifty way CGPoint(x: xPos, y:yPos).
So change your CGPointMake(startX, startY) to CGPoint(x: startX, y: startY)
swift 3 emphasizes in use of named Parameters.
CGPoint Can be created like this.
let point = CGPoint(x: 0,y :0) // CGFloat, Double, Int
Related
I'm trying to create a circular progressBar with timer function in my UItableviewCell class. I have created a custom class timer, and I am using Jonni's progressBar template (Credit to
Jonni Åkesson) from - https://github.com/innoj/CountdownTimer.
The only noticeable difference between my code and source template is that I am adding the Custom ProgressBar class UIView programmatically whereas the source template used IBOutlet to connect to the Custom ProgressBar Class.
My goal is to ensure that the CAShapeLayers (both actual and background layers are shown as per below image - Source: https://www.youtube.com/watch?v=-KwFvGVstyc
Below is my code and I am unable to see both front CAShapeLayer and background CAShapeLayer. I am suspecting a logical error when I initialize constant "progressBar".
Please advise if it is possible to utilize the Custom ProgressBar Class, instead of re-writing the entire code.
import Foundation
import UIKit
import IQKeyboardManagerSwift
class ActiveExerciseTableViewCell: UITableViewCell, UITextFieldDelegate {
let progressBar: ProgressBar = {
let progressBar = ProgressBar()
return progressBar
}()
lazy var activeExerciseTimerUIView: UIView = { [weak self] in
let activeExerciseTimerUIView = UIView()
activeExerciseTimerUIView.backgroundColor = .black
activeExerciseTimerUIView.isUserInteractionEnabled = true
activeExerciseTimerUIView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapTimerView)))
return activeExerciseTimerUIView
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubview(activeExerciseTimerUIView)
}
override func layoutSubviews() {
super.layoutSubviews()
setUpActiveExerciseUIViewLayout()
}
func setUpActiveExerciseUIViewLayout(){
activeExerciseTimerUIView.translatesAutoresizingMaskIntoConstraints = false
activeExerciseTimerUIView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
activeExerciseTimerUIView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: (contentView.frame.height-tableviewContentViewTabBarHeight)*0.25).isActive = true
activeExerciseTimerUIView.widthAnchor.constraint(equalToConstant: 225).isActive = true
activeExerciseTimerUIView.heightAnchor.constraint(equalToConstant: 225).isActive = true
activeExerciseTimerUIView.layer.cornerRadius = activeExerciseTimerUIView.frame.width/2
progressBar.frame = CGRect(
x: 0,
y: 0,
width: activeExerciseTimerUIView.frame.width,
height: activeExerciseTimerUIView.frame.height)
self.activeExerciseTimerUIView.addSubview(progressBar)
}
Below is the Custom ProgressBar Class from source file.
import UIKit
class ProgressBar: UIView, CAAnimationDelegate {
fileprivate var animation = CABasicAnimation()
fileprivate var animationDidStart = false
fileprivate var timerDuration = 0
lazy var fgProgressLayer: CAShapeLayer = {
let fgProgressLayer = CAShapeLayer()
return fgProgressLayer
}()
lazy var bgProgressLayer: CAShapeLayer = {
let bgProgressLayer = CAShapeLayer()
return bgProgressLayer
}()
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
loadBgProgressBar()
loadFgProgressBar()
}
override init(frame: CGRect) {
super.init(frame: frame)
loadBgProgressBar()
loadFgProgressBar()
}
fileprivate func loadFgProgressBar() {
let startAngle = CGFloat(-Double.pi / 2)
let endAngle = CGFloat(3 * Double.pi / 2)
let centerPoint = CGPoint(x: frame.width/2 , y: frame.height/2)
let gradientMaskLayer = gradientMask()
fgProgressLayer.path = UIBezierPath(arcCenter:centerPoint, radius: frame.width/2 - 30.0, startAngle:startAngle, endAngle:endAngle, clockwise: true).cgPath
fgProgressLayer.backgroundColor = UIColor.clear.cgColor
fgProgressLayer.fillColor = nil
fgProgressLayer.strokeColor = UIColor.black.cgColor
fgProgressLayer.lineWidth = 4.0
fgProgressLayer.strokeStart = 0.0
fgProgressLayer.strokeEnd = 0.0
gradientMaskLayer.mask = fgProgressLayer
layer.addSublayer(gradientMaskLayer)
}
fileprivate func gradientMask() -> CAGradientLayer {
let gradientLayer = CAGradientLayer()
gradientLayer.frame = bounds
gradientLayer.locations = [0.0, 1.0]
let colorTop: AnyObject = CustomColor.lime.cgColor
let colorBottom: AnyObject = CustomColor.lime.cgColor
let arrayOfColors: [AnyObject] = [colorTop, colorBottom]
gradientLayer.colors = arrayOfColors
return gradientLayer
}
fileprivate func loadBgProgressBar() {
let startAngle = CGFloat(-Double.pi / 2)
let endAngle = CGFloat(3 * Double.pi / 2)
let centerPoint = CGPoint(x: frame.width/2 , y: frame.height/2)
let gradientMaskLayer = gradientMaskBg()
bgProgressLayer.path = UIBezierPath(arcCenter:centerPoint, radius: frame.width/2 - 30.0, startAngle:startAngle, endAngle:endAngle, clockwise: true).cgPath
bgProgressLayer.backgroundColor = UIColor.clear.cgColor
bgProgressLayer.fillColor = nil
bgProgressLayer.strokeColor = UIColor.black.cgColor
bgProgressLayer.lineWidth = 4.0
bgProgressLayer.strokeStart = 0.0
bgProgressLayer.strokeEnd = 1.0
gradientMaskLayer.mask = bgProgressLayer
layer.addSublayer(gradientMaskLayer)
}
fileprivate func gradientMaskBg() -> CAGradientLayer {
let gradientLayer = CAGradientLayer()
gradientLayer.frame = bounds
gradientLayer.locations = [0.0, 1.0]
let colorTop: AnyObject = CustomColor.strawberry.cgColor
let colorBottom: AnyObject = CustomColor.strawberry.cgColor
let arrayOfColors: [AnyObject] = [colorTop, colorBottom]
gradientLayer.colors = arrayOfColors
return gradientLayer
}
Resolve the issue by loading the background and foreground progress bars in layoutSubViews function.
ProgressBar.swift
import UIKit
import Pulsator
class ProgressBar: UIView, CAAnimationDelegate {
fileprivate var animation = CABasicAnimation()
fileprivate var animationDidStart = false
fileprivate var updatedAnimationAdded = false
fileprivate var updateExecuted = false
fileprivate var totalTimerDuration = 0
fileprivate var isProgressBarAdded:Bool = false
fileprivate var barlineWidth: CGFloat = 8
// let pulsator = Pulsator()
// fileprivate var width: CGFloat
// fileprivate var height: CGFloat
//
lazy var fgProgressLayer: CAShapeLayer = {
let fgProgressLayer = CAShapeLayer()
return fgProgressLayer
}()
lazy var bgProgressLayer: CAShapeLayer = {
let bgProgressLayer = CAShapeLayer()
return bgProgressLayer
}()
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
loadBgProgressBar()
loadFgProgressBar()
}
override init(frame: CGRect) {
super.init(frame: frame)
}
override func layoutSubviews() {
super.layoutSubviews()
if self.frame.width > 0 && self.frame.height > 0 && isProgressBarAdded == false {
loadBgProgressBar()
loadFgProgressBar()
isProgressBarAdded = true
}
}
I have added a UIView in the storyboard and want to set its background with gradient colors.
This is my code
extension UIView {
func setGradientBackground(colors: [CGColor]) {
let gradientLayer = CAGradientLayer()
gradientLayer.colors = colors
gradientLayer.locations = [0.0, 1.0]
gradientLayer.frame = self.bounds
self.layer.insertSublayer(gradientLayer, at:0)
}
}
and in the viewcontroller,
viewSocial.setGradientBackground(colors: [UIColor.gradientBottomColor.cgColor, UIColor.gradientTopColor.cgColor])
But this creates double gradient layers. see the image
I have tried adding a class for GradientLayer as mentioned here. But this is not allowing to set on a view from the storyboard. Gives the warning of the weak variable.
Here is one way to make a #IBDesignable gradient view. It's using the default top-to-bottom gradient direction:
#IBDesignable
class MyGradientView: UIView {
#IBInspectable var color1: UIColor = .red {
didSet { setNeedsDisplay() }
}
#IBInspectable var color2: UIColor = .yellow {
didSet { setNeedsDisplay() }
}
private var gradientLayer: CAGradientLayer!
override class var layerClass: AnyClass {
return CAGradientLayer.self
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() -> Void {
// use self.layer as the gradient layer
gradientLayer = self.layer as? CAGradientLayer
gradientLayer.colors = [color1.cgColor, color2.cgColor]
}
override func layoutSubviews() {
super.layoutSubviews()
gradientLayer.colors = [color1.cgColor, color2.cgColor]
}
}
It has #IBInspectable vars for "color1" and "color2" ... changing them in the Attributes Inspector will be reflected in Storyboard.
No need for any additional code, such as your current approach with setGradientBackground()
Here is a code of #IBDesignable Gradient View. You can use Top to Bottom and Left to Right from Storyboard.
private var startGradientColorAssociatedKey : UIColor = .black
private var endGradientColorAssociatedKey : UIColor = .black
private var observationGradientView: NSKeyValueObservation?
extension UIView {
#IBInspectable var startGradientColor: UIColor {
get {
if let color = objc_getAssociatedObject(self, &startGradientColorAssociatedKey) as? UIColor {
return color
} else {
return .black
}
} set {
objc_setAssociatedObject(self, &startGradientColorAssociatedKey, newValue, .OBJC_ASSOCIATION_RETAIN)
}
}
#IBInspectable var endGradientColor: UIColor {
get {
if let color = objc_getAssociatedObject(self, &endGradientColorAssociatedKey) as? UIColor {
return color
} else {
return .black
}
} set {
objc_setAssociatedObject(self, &endGradientColorAssociatedKey, newValue, .OBJC_ASSOCIATION_RETAIN)
}
}
#IBInspectable
var isTopToBottomGradient: Bool {
get {
return self.isTopToBottomGradient
}
set {
DispatchQueue.main.async {
if newValue {
self.setGradientBackground(colorTop: self.startGradientColor, colorBottom: self.endGradientColor)
} else {
self.setGradientBackground(colorLeft: self.startGradientColor, colorRight: self.endGradientColor)
}
}
}
}
func setGradientBackground(colorLeft: UIColor, colorRight: UIColor) {
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [colorLeft.cgColor, colorRight.cgColor]
gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
gradientLayer.locations = [0, 1]
gradientLayer.frame = bounds
observationGradientView = self.observe(\.bounds, options: .new) { [weak gradientLayer] view, change in
if let value = change.newValue {
gradientLayer?.frame = value
}
}
layer.insertSublayer(gradientLayer, at: 0)
}
}
Use from Storyboard : If you want to set gradient from top to bottom then set ON isTopToBottomGradient key. Defalt value is OFF
I am new in iOS Development, I want to make gradient rounded button by using this designable class
#IBDesignable
class GradientButton: UIButton {
let gradientLayer = CAGradientLayer()
#IBInspectable
var topGradientColor: UIColor? {
didSet {
setGradient(topGradientColor: topGradientColor, bottomGradientColor: bottomGradientColor)
}
}
#IBInspectable
var bottomGradientColor: UIColor? {
didSet {
setGradient(topGradientColor: topGradientColor, bottomGradientColor: bottomGradientColor)
}
}
override func layoutSubviews() {
super.layoutSubviews()
updateCornerRadius()
}
#IBInspectable var fullRounded: Bool = false {
didSet {
updateCornerRadius()
}
}
#IBInspectable var cornerRadiusOfButton : CGFloat = 0 {
didSet {
layer.cornerRadius = cornerRadiusOfButton
}
}
func updateCornerRadius() {
layer.cornerRadius = fullRounded ? (frame.size.height / 2) : cornerRadiusOfButton
}
#IBInspectable var borderWidth: CGFloat {
set {
layer.borderWidth = newValue
}
get {
return layer.borderWidth
}
}
#IBInspectable var borderColor: UIColor? {
set {
guard let uiColor = newValue else { return }
layer.borderColor = uiColor.cgColor
}
get {
guard let color = layer.borderColor else { return nil }
return UIColor(cgColor: color)
}
}
private func setGradient(topGradientColor: UIColor?, bottomGradientColor: UIColor?) {
if let topGradientColor = topGradientColor, let bottomGradientColor = bottomGradientColor {
gradientLayer.frame = bounds
gradientLayer.colors = [topGradientColor.cgColor, bottomGradientColor.cgColor]
gradientLayer.borderColor = layer.borderColor
gradientLayer.borderWidth = layer.borderWidth
gradientLayer.cornerRadius = layer.cornerRadius
layer.insertSublayer(gradientLayer, at: 0)
} else {
gradientLayer.removeFromSuperlayer()
}
}
}
but here is the result:
as you can see the width of the gradient layer button is incorret.
here is the constraint layout I use:
I want that gradient layer to have the same width as the original button, I suspect that the problem is in this line
gradientLayer.frame = bounds
but unfortunately I don't know how to set the gradient layer width to be the same size as the original button programmatically in #IBDesignable class
could you please help me to solve this issue ? thanks in advance
Override layoutSubviews inside GradientButton and update the gradientLayer frame as,
override func layoutSubviews() {
super.layoutSubviews()
self.gradientLayer.frame = bounds
}
I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.
create a UIViewX in the View Controller or
add UIButton on custom view(UIViewX). And IBAction is print(" test ")
Everything work good.
But...
When add UIButton on custom view(UIViewX) and add animation code with UIViewX, button does't work if I press it
how to fix it that can response print(" test ")?
Thanks in advance.
Here is animation code :
var currentColorArrayIndex = -1
var colorArray:[(color1: UIColor, color2: UIColor)] = []
override func viewDidLoad() {
super.viewDidLoad()
colorArrayFunction()
backgroundAnimate()
}
colorArrayFunction & backgroundAnimate
Here is UIViewX code :
import UIKit
#IBDesignable
class UIViewXXXXX: UIView {
// MARK: - Gradient
#IBInspectable var firstColor: UIColor = UIColor.white {
didSet {
updateView()}}
#IBInspectable var secondColor: UIColor = UIColor.white {
didSet {
updateView()}}
#IBInspectable var horizontalGradient: Bool = false {
didSet {
updateView()}}
override class var layerClass: AnyClass {
get {
return CAGradientLayer.self}}
func updateView() {
let layer = self.layer as! CAGradientLayer
layer.colors = [ firstColor.cgColor, secondColor.cgColor ]
if (horizontalGradient) {
layer.startPoint = CGPoint(x: 0.0, y: 0.5)
layer.endPoint = CGPoint(x: 1.0, y: 0.5)
} else {
layer.startPoint = CGPoint(x: 0, y: 0)
layer.endPoint = CGPoint(x: 0, y: 1)
}}
// MARK: - Border
#IBInspectable public var borderColor: UIColor = UIColor.clear {
didSet {
layer.borderColor = borderColor.cgColor}}
#IBInspectable public var borderWidth: CGFloat = 0 {
didSet {
layer.borderWidth = borderWidth}}
#IBInspectable public var cornerRadius: CGFloat = 0 {
didSet {
layer.cornerRadius = cornerRadius
}
}
// MARK: - Shadow
#IBInspectable public var shadowOpacity: CGFloat = 0 {
didSet {
layer.shadowOpacity = Float(shadowOpacity)}}
#IBInspectable public var shadowColor: UIColor = UIColor.clear {
didSet {
layer.shadowColor = shadowColor.cgColor}}
#IBInspectable public var shadowRadius: CGFloat = 0 {
didSet {
layer.shadowRadius = shadowRadius}}
#IBInspectable public var shadowOffsetY: CGFloat = 0 {
didSet {
layer.shadowOffset.height = shadowOffsetY}}}
I am trying to add corner radius on uitextfield effect applies but not sharp corner.
Please check attached pictures.
I am expecting result like this.
Here is my Code for gray textfield.
self.layer.cornerRadius = 7.5
self.layer.borderWidth = 1.5
self.layer.borderColor = UIColor(red: 209/255.0, green: 209/255.0, blue: 209/255.0, alpha: 1.0).CGColor
self.backgroundColor = UIColor.whiteColor()
Here is my Code for Red textfield.
self.layer.cornerRadius = 7.5
self.layer.borderWidth = 1.5
self.layer.borderColor = TextFieldRedBorderColor.CGColor
self.backgroundColor = TextFieldRedBackgroundColor
There is only color difference between both code but still it's not working.
self.layer.masksToBounds = true
This will solves your issue
#IBOutlet var passwordboarderview: UIView!
#IBOutlet var textusername: UITextField!
#IBOutlet var txtpassword: UITextField!
passwordboarderview.layer.cornerRadius = 7.5
passwordboarderview.layer.borderColor = UIColor.whiteColor().CGColor
passwordboarderview.layer.borderWidth = 2.0
passwordboarderview.clipsToBounds = true
txtpassword.attributedPlaceholder = NSAttributedString(string:"Password",attributes:[NSForegroundColorAttributeName: UIColor.whiteColor()])
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
textusername.leftView = paddingView
textusername.leftViewMode = .Always
textusername.layer.borderWidth = 2.0
textusername.layer.cornerRadius = 7.5
textusername.layer.borderColor = UIColor.grayColor().CGColor
textusername.clipsToBounds = true
Output :
Storyboard:
//
// FDTextField.swift
// FilloutDocs
//
// Created by Janak Thakkar on 15/03/16.
// Copyright © 2016 zetrixweb. All rights reserved.
//
import UIKit
private var maxLengthDictionary = [UITextField : Int]()
#IBDesignable
class ZWTextField : UITextField {
var topBorder: UIView?
var bottomBorder: UIView?
var leftBorder: UIView?
var rightBorder: UIView?
var leftimageview : UIImageView?
var rightimageview : UIImageView?
#IBInspectable var borderColor: UIColor = UIColor.clearColor() {
didSet {
layer.borderColor = borderColor.CGColor
}
}
#IBInspectable var ULText: Bool = false {
didSet {
let textRange = NSMakeRange(0, (self.text?.characters.count)!)
let attributedText = NSMutableAttributedString(string: (self.text)!)
attributedText.addAttribute(NSUnderlineStyleAttributeName , value:NSUnderlineStyle.StyleSingle.rawValue, range: textRange)
// Add other attributes if needed
self.attributedText = attributedText
}
}
#IBInspectable var borderWidth: CGFloat = 0 {
didSet {
layer.borderWidth = borderWidth
}
}
#IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
layer.cornerRadius = cornerRadius
}
}
#IBInspectable var fitToWidth: Bool = false {
didSet {
adjustsFontSizeToFitWidth = fitToWidth
}
}
#IBInspectable var cursorColor : UIColor = UIColor.blueColor(){
didSet {
tintColor = cursorColor
}
}
#IBInspectable var placeHolderColor : UIColor = UIColor.lightGrayColor(){
didSet {
setValue(placeHolderColor, forKeyPath: "_placeholderLabel.textColor")
}
}
override func layoutSubviews() {
super.layoutSubviews()
}
#IBInspectable var rightImage : UIImage? {
didSet {
if rightImage != nil {
let width = rightviewWidth > rightImage!.size.width + 10 ? rightviewWidth : rightImage!.size.width + 10
rightViewMode = UITextFieldViewMode.Always
rightimageview = UIImageView()
rightimageview!.frame=CGRectMake(self.frame.size.width - width, self.frame.origin.y+2, width,self.frame.size.height-4)
rightimageview!.image = rightImage
rightView = rightimageview
self.rightViewMode = .Always
rightimageview!.contentMode = .Center
}
else {
if rightimageview != nil {
rightimageview?.removeFromSuperview()
rightimageview = nil
}
}
}
}
#IBInspectable var rightviewWidth : CGFloat = 0 {
didSet{
if rightimageview != nil{
let width = rightviewWidth > rightImage!.size.width + 10 ? rightviewWidth : rightImage!.size.width + 10
rightimageview!.frame=CGRectMake(self.frame.origin.x+5, self.frame.origin.y+2, width,self.frame.size.height-4)
}
}
}
#IBInspectable var leftImage : UIImage? {
didSet {
if leftImage != nil {
let width = leftviewWidth > leftImage!.size.width + 10 ? leftviewWidth : leftImage!.size.width + 10
leftViewMode = UITextFieldViewMode.Always
leftimageview = UIImageView();
leftimageview!.frame=CGRectMake(self.frame.origin.x+50, self.frame.origin.y+5, width,self.frame.size.height-5)
//leftimageview!.frame = CGRectMake(10, 5, 40, 40)
leftimageview!.image = leftImage;
leftView = leftimageview;
self.leftViewMode = .Always
leftimageview!.contentMode = .Center
}
}
}
#IBInspectable var leftviewWidth : CGFloat = 0 {
didSet{
if leftimageview != nil{
let width = leftviewWidth > leftImage!.size.width + 10 ? leftviewWidth : leftImage!.size.width + 10
leftimageview!.frame=CGRectMake(self.frame.origin.x+5, self.frame.origin.y+2, width,self.frame.size.height-4)
}
}
}
#IBInspectable var bottomLineWidth : CGFloat = 1 {
didSet{
let border: CALayer = CALayer()
border.borderColor = UIColor.darkGrayColor().CGColor
self.frame = CGRectMake(0, self.frame.size.height - bottomLineWidth, self.frame.size.width, self.frame.size.height)
border.borderWidth = borderWidth
self.layer.addSublayer(border)
self.layer.masksToBounds = true
}
}
#IBInspectable var bottomLineColor : UIColor = UIColor.lightGrayColor(){
didSet {
let border: CALayer = CALayer()
border.borderColor = bottomLineColor.CGColor
}
}
#IBInspectable var paddingLeft: CGFloat = 0
#IBInspectable var paddingRight: CGFloat = 0
override func textRectForBounds(bounds: CGRect) -> CGRect {
return CGRectMake(bounds.origin.x + paddingLeft, bounds.origin.y,
bounds.size.width - paddingLeft - paddingRight, bounds.size.height);
}
override func editingRectForBounds(bounds: CGRect) -> CGRect {
return textRectForBounds(bounds)
}
#IBInspectable var topBorderColor : UIColor = UIColor.clearColor()
#IBInspectable var topBorderHeight : CGFloat = 0 {
didSet{
if topBorder == nil{
topBorder = UIView()
topBorder?.backgroundColor=topBorderColor;
topBorder?.frame = CGRectMake(0, 0, self.frame.size.width, topBorderHeight)
addSubview(topBorder!)
}
}
}
#IBInspectable var bottomBorderColor : UIColor = UIColor.clearColor()
#IBInspectable var bottomBorderHeight : CGFloat = 0 {
didSet{
if bottomBorder == nil{
bottomBorder = UIView()
bottomBorder?.backgroundColor=bottomBorderColor;
bottomBorder?.frame = CGRectMake(0, self.frame.size.height - bottomBorderHeight, self.frame.size.width, bottomBorderHeight)
addSubview(bottomBorder!)
}
}
}
#IBInspectable var leftBorderColor : UIColor = UIColor.clearColor()
#IBInspectable var leftBorderHeight : CGFloat = 0 {
didSet{
if leftBorder == nil{
leftBorder = UIView()
leftBorder?.backgroundColor=leftBorderColor;
leftBorder?.frame = CGRectMake(0, 0, leftBorderHeight, self.frame.size.height)
addSubview(leftBorder!)
}
}
}
#IBInspectable var rightBorderColor : UIColor = UIColor.clearColor()
#IBInspectable var rightBorderHeight : CGFloat = 0 {
didSet{
if rightBorder == nil{
rightBorder = UIView()
rightBorder?.backgroundColor=topBorderColor;
rightBorder?.frame = CGRectMake(self.frame.size.width - rightBorderHeight, 0, rightBorderHeight, self.frame.size.height)
addSubview(rightBorder!)
}
}
}
#IBInspectable var maxLength: Int {
get {
if let length = maxLengthDictionary[self] {
return length
} else {
return Int.max
}
}
set {
maxLengthDictionary[self] = newValue
addTarget(self, action: "checkMaxLength:", forControlEvents: UIControlEvents.EditingChanged)
}
}
func checkMaxLength(sender: UITextField) {
let newText = sender.text
if newText?.characters.count > maxLength {
let cursorPosition = selectedTextRange
text = (newText! as NSString).substringWithRange(NSRange(location: 0, length: maxLength))
selectedTextRange = cursorPosition
}
}
}
Just add this file in project and you can set all the properties and can see realtime out.All properties you can set through storyboard.
There was just one mistake in my code.
TextField Border Style Was Line
textField.borderStyle = .Line
then I make it None
textField.borderStyle = .None
and it work like I wanted (Rounded Corners)...
Anyway.. Thanks for support and answers...