Skip to content

Instantly share code, notes, and snippets.

@jackd942
Last active November 20, 2017 12:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackd942/3e967cdd7c2d1cfd6795 to your computer and use it in GitHub Desktop.
Save jackd942/3e967cdd7c2d1cfd6795 to your computer and use it in GitHub Desktop.
Custom IBDesignable and IBInspectable UITextField created for use in the Devslopes App
//
// DATextField.swift
// devslopes-app
//
// Created by Jack Davis on 2/26/16.
// Copyright © 2016 Devslopes. All rights reserved.
//
import UIKit
@IBDesignable
class DATextField: UITextField {
// MARK: - IBInspectable
@IBInspectable var tintCol: UIColor = UIColor(netHex: 0x707070)
@IBInspectable var fontCol: UIColor = UIColor(netHex: 0x707070)
@IBInspectable var shadowCol: UIColor = UIColor(netHex: 0x707070)
@IBInspectable var image: UIImage? = nil
@IBInspectable var placeholderText: String?
@IBInspectable var imgLeftInset: CGFloat = 10.0
@IBInspectable var imgTopInset: CGFloat = 7.0
@IBInspectable var imgBottomInset: CGFloat = 7.0
// MARK: - Properties
var textFont = UIFont(name: "Noto Sans", size: 14.0)
var imgView: UIImageView?
// MARK: - View
override func awakeFromNib() {
setupView()
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
setupView()
}
func setupView() {
self.layer.masksToBounds = false
self.layer.shadowColor = shadowCol.CGColor
self.layer.shadowOpacity = 0.2
self.layer.shadowRadius = 1.0
self.layer.shadowOffset = CGSizeMake(0.0, 0.0)
self.tintColor = tintCol
self.textColor = fontCol
// email icon color #B3B3B3
if let phText = self.placeholderText {
self.attributedPlaceholder = NSAttributedString(string: phText, attributes: [NSForegroundColorAttributeName: UIColor(netHex: 0xB3B3B3)])
}
if let fnt = textFont {
self.font = fnt
} else {
self.font = UIFont(name: "Helvetica Neue", size: 14.0)
}
self.setNeedsLayout()
self.setNeedsDisplay()
}
override func drawRect(rect: CGRect) {
super.drawRect(rect)
if self.image != nil {
self.imgView = UIImageView(frame: CGRectMake(self.imgLeftInset, self.imgTopInset, self.bounds.height - (self.imgTopInset + self.imgBottomInset), self.bounds.height - (self.imgTopInset + imgBottomInset)))
self.imgView!.contentMode = .ScaleAspectFit
self.imgView!.image = self.image
self.addSubview(self.imgView!)
}
}
// Placeholder text
override func textRectForBounds(bounds: CGRect) -> CGRect {
if let iView = self.imgView {
return CGRectInset(bounds, iView.bounds.size.width + 15, 0)
} else {
return CGRectInset(bounds, 10, 0)
}
}
// Editable text
override func editingRectForBounds(bounds: CGRect) -> CGRect {
if let iView = self.imgView {
return CGRectInset(bounds, iView.bounds.size.width + 15, 0)
} else {
return CGRectInset(bounds, 10, 0)
}
}
}
@ERaBala
Copy link

ERaBala commented Nov 20, 2017

hi, I'm facing issue in iPad while using "textRectForBounds" like (calayer position contains nan 240 nan) May i know what is the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment