Change Width of UITextfield Input - ios

Is there a way to change the input size of my UITextield so it doesn't overlap with my "vergessen"-Button?
The Textfield is a simple TextfieldView with a width of 315. I added the "Vergessen?" Button programmatically with the code down below.
func createForgetButton () {
let button = UIButton(type: .system)
button.setTitle("Vergessen?", for: .normal)
button.addTarget(self, action: #selector(self.vergessenTapped(_:)), for: .touchUpInside)
button.titleLabel?.font = UIFont(name: "Avenir Next", size: 19.0)
passwordTextField.rightView = button
passwordTextField.rightViewMode = .unlessEditing
}

Use the following custom class for the field. Then update .rightPadding field in IB or in the code to match the width of the button you have on the right.
/**
* Text field with some changes according to design
*
* - author: Alexander Volkov
* - version: 1.0
*/
#IBDesignable public class CustomTextField: UITextField {
/// the left padding
#IBInspectable public var leftPadding: CGFloat = 0 { didSet { self.setNeedsLayout() } }
/// the right padding
#IBInspectable public var rightPadding: CGFloat = 0 { didSet { self.setNeedsLayout() } }
/// Text rectangle
///
/// - Parameter bounds: the bounds
/// - Returns: the rectangle
override public func textRect(forBounds bounds: CGRect) -> CGRect {
let originalRect: CGRect = super.editingRect(forBounds: bounds)
return CGRect(x: originalRect.origin.x + leftPadding, y: originalRect.origin.y, width: originalRect.size.width - leftPadding - rightPadding, height: originalRect.size.height)
}
/// Editing rectangle
///
/// - Parameter bounds: the bounds
/// - Returns: the rectangle
override public func editingRect(forBounds bounds: CGRect) -> CGRect {
let originalRect: CGRect = super.editingRect(forBounds: bounds)
return CGRect(x: originalRect.origin.x + leftPadding, y: originalRect.origin.y, width: originalRect.size.width - leftPadding - rightPadding, height: originalRect.size.height)
}
}

Related

Animation of drawing does not work in Swift

Screenshot
I have created a custom UIButton class where I'm filling its inside with a color but only partially.
The Goal:
The goal is to fill inside of the button with background color but only partially. For example you have a long button(height: 50.0, width: 300.0). I want to fill it with background color horizontally, let's say only for 50%. In this case only the half of my button will be filled with color, another half will stay transparent or white.
The entire class looks like this:
class CustomButton: UIButton {
var color: UIColor = .clear
var percentage: CGFloat = 0
func fill(withPercentage percentage: CGFloat, isCorrect correct: Bool, onRect rect: CGRect) {
self.color = correct ? .lightishGreen : .pastelRed
self.percentage = percentage
draw(rect)
}
override func draw(_ rect: CGRect) {
super.draw(rect)
let fillRect = CGRect(
origin: CGPoint(x: rect.origin.x, y: rect.origin.y),
size: CGSize(width: rect.size.width * percentage, height: rect.size.height)
)
color.set()
guard let context = UIGraphicsGetCurrentContext() else { return }
context.fill(fillRect)
}
}
and this class works perfectly. However, I want to animate this drawing process, so the button is getting filled with background color from 0.0 to 1.0 let's say.
I've tried to put context.fill(fillRect) inside of the UIView.animate(){} but it did not work. So I wondered, is it possible to animate drawing?
I just do not want to make button bg color clear and to put UIView behind it and simulate filling there. Wanted a clean way. So I will be happy, if someone can give me a hint or show me how to do it.
Thanks in advance!
It sounds to me like you want a custom animatable property. Let's call it percentage. Here's one way:
class MyView : UIView {
required init?(coder: NSCoder) {
super.init(coder: coder)
self.backgroundColor = .clear
}
override class var layerClass : AnyClass {
return MyLayer.self
}
override func draw(_ rect: CGRect) {}
}
class MyLayer : CALayer {
#NSManaged var percentage : CGFloat
override class func needsDisplay(forKey key: String) -> Bool {
if key == #keyPath(percentage) {
return true
}
return super.needsDisplay(forKey:key)
}
override func action(forKey key: String) -> CAAction? {
if key == #keyPath(percentage) {
let ba = CABasicAnimation(keyPath: key)
ba.fromValue = self.presentation()!.value(forKey:key)
return ba
}
return super.action(forKey:key)
}
override func draw(in con: CGContext) {
con.setFillColor(UIColor.green.cgColor)
let rect = self.bounds.insetBy(dx: 2, dy: 2)
let rounded = UIBezierPath.init(roundedRect: rect, cornerRadius: 20)
con.addPath(rounded.cgPath)
con.setStrokeColor(UIColor.lightGray.cgColor)
con.strokePath()
con.addPath(rounded.cgPath)
con.clip(using: .evenOdd)
let fillRect = CGRect(
origin: CGPoint(x: rect.origin.x, y: rect.origin.y),
size: CGSize(width: rect.size.width * percentage, height: rect.size.height)
)
con.fill(fillRect)
}
}
The result is that when you set this view's layer's percentage, the change is animated:

Swift - adding clear button to UITextfield programmatically

I am having problems adding a clear button to my UITextfield.
This is my textfield:
let emailTextField: CustomTextField = {
let v = CustomTextField()
v.borderActiveColor = .white
v.borderInactiveColor = .white
v.textColor = .white
v.font = UIFont(name: "AvenirNext-Regular", size: 17)
v.placeholder = "Email-Adresse"
v.placeholderColor = .gray
v.placeholderFontScale = 1
v.clearButtonMode = UITextField.ViewMode.always
v.minimumFontSize = 13
v.borderStyle = .line
v.autocapitalizationType = .none
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
As you can see I set clearButtonMode = .always but it is not being displayed.
My CustomTextFieldClass is nothing special either:
class CustomTextField: HoshiTextField {
/// the left padding
#IBInspectable public var leftPadding: CGFloat = 0 { didSet { self.setNeedsLayout() } }
/// the right padding
#IBInspectable public var rightPadding: CGFloat = 0 { didSet { self.setNeedsLayout() } }
/// Text rectangle
///
/// - Parameter bounds: the bounds
/// - Returns: the rectangle
override public func textRect(forBounds bounds: CGRect) -> CGRect {
let originalRect: CGRect = super.editingRect(forBounds: bounds)
return CGRect(x: originalRect.origin.x + leftPadding, y: originalRect.origin.y, width: originalRect.size.width - leftPadding - rightPadding, height: originalRect.size.height)
}
/// Editing rectangle
///
/// - Parameter bounds: the bounds
/// - Returns: the rectangle
override public func editingRect(forBounds bounds: CGRect) -> CGRect {
let originalRect: CGRect = super.editingRect(forBounds: bounds)
return CGRect(x: originalRect.origin.x + leftPadding, y: originalRect.origin.y, width: originalRect.size.width - leftPadding - rightPadding, height: originalRect.size.height)
}
Does anyone know why the clear button is not being displayed???

UITextfield gets cut from the top while in editing mode

I got two UITextfields with separator(UILabel) between them.
I put them all into UIStackView.
While in editing mode, content of the textfield is cut from the top, as seen in the picture below
I've found that the only way to remove this issue is to make this separator big enough, but this spoils my design.
How to fix it?
It's worth to mention my UIStackView settings:
and show how I implement this custom bottomline-style UITextfield
class CustomTextField: UITextField {
override func awakeFromNib() {
super.awakeFromNib()
let attributedString = NSAttributedString(string: self.placeholder!, attributes: [NSForegroundColorAttributeName:UIColor.lightGray, NSFontAttributeName: UIFont(name: "GothamRounded-Book", size: 18.0)! ])
self.attributedPlaceholder = attributedString
self.tintColor = UIColor.appRed
self.font = UIFont(name: "GothamRounded-Book", size: 18.0)!
self.borderStyle = .none
self.textAlignment = .center
}
override func textRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: 0, dy: 5)
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: 0, dy: 5)
}
override var tintColor: UIColor! {
didSet {
setNeedsDisplay()
}
}
override func draw(_ rect: CGRect) {
let startingPoint = CGPoint(x: rect.minX, y: rect.maxY)
let endingPoint = CGPoint(x: rect.maxX, y: rect.maxY)
let path = UIBezierPath()
path.move(to: startingPoint)
path.addLine(to: endingPoint)
path.lineWidth = 2.0
tintColor.setStroke()
tintColor = UIColor.appRed
path.stroke()
}
}
Any help much appreciated
EDIT
I have another TextField like that and it works fine, but it doesn't sit inside any horizontal UIStackView. Here is the screenshot of hierarchy:
Unfortunately you need to check the size on editing
class CustomTextField: UITextField {
override func awakeFromNib() {
super.awakeFromNib()
self.addTarget(self, action: #selector(textFieldEditingChanged), for: .editingChanged)
}
func textFieldEditingChanged(_ textField: UITextField) {
textField.invalidateIntrinsicContentSize()
}
override var intrinsicContentSize: CGSize {
if isEditing {
let string = text ?? ""
let size = string.size(attributes: typingAttributes)
return CGSize(width: size.width + (rightView?.bounds.size.width ?? 0) + (leftView?.bounds.size.width ?? 0) + 2,
height: size.height)
}
return super.intrinsicContentSize
}
}

Setting the frame of a sublayer inside of layoutSubviews() makes changing it impossible. Any workaround for that?

I'm working on a customizable UITextField (see code below). I have added a border at the bottom (you can set it in the storyboard). However, I had problems setting the frame of the CALayer that this border consists of.
If I set it inside the didSet method of var showBottomBorder it doesn't appear on the screen. I think this is because the frame (of the UITextField) hasn't been calculated yet (maybe didSet gets called before that).
So I moved it to the layoutSubviews() method (see code below). This works perfectly.
But now I have another problem. I can't really change that frame anymore. Every time I change it, it gets reset by layoutSubviews() which I think is called then.
At the bottom of my code, there is the method textFieldDidBeginEditing. In there, I wanted to move up my bottom border (animated). But it doesn't work. The border does not move anywhere. And like I said, I think it's because I set the frame inside the layoutSubviews() method.
Is there a better way to set the frame of the bottom border? A way which allows me to change stuff?
#IBDesignable
class CustomizableTextField: UITextField, UITextFieldDelegate {
// MARK: - Properties
private var bottomBorder = CALayer()
// MARK: - #IBInspectables
#IBInspectable var roundCorners: CGFloat = 0 {
didSet {
self.layer.cornerRadius = roundCorners
self.clipsToBounds = true
}
}
/** -- */
#IBInspectable var borderWidth: CGFloat = 1.0 {
didSet {
self.layer.borderWidth = self.borderWidth
}
}
#IBInspectable var borderColor: UIColor = UIColor.white {
didSet {
self.layer.borderColor = self.borderColor.cgColor
}
}
/** -- */
/** -- */
private var showBottomBorder: Bool = false {
didSet {
switch showBottomBorder {
case true:
bottomBorder.borderColor = self.bottomBorderColor.cgColor
bottomBorder.borderWidth = self.bottomBorderWidth
self.layer.addSublayer(bottomBorder)
self.layer.masksToBounds = true
break
case false:
bottomBorder.removeFromSuperlayer()
break
}
}
}
#IBInspectable var bottomBorderWidth: CGFloat = 1.0 {
didSet {
self.showBottomBorder = false
self.showBottomBorder = true
}
}
#IBInspectable var bottomBorderColor: UIColor = UIColor.white {
didSet {
self.showBottomBorder = false
self.showBottomBorder = true
}
}
/** -- */
/** -- */
// Somwhow, the default panel for my font color doesn't change anything, so I created this
#IBInspectable var fixedFontColor: UIColor = UIColor.white {
didSet {
self.textColor = fixedFontColor
}
}
#IBInspectable var placeholderFontColor: UIColor = UIColor.white {
didSet {
var placeholderTxt = ""
if let txt = self.placeholder {
placeholderTxt = txt
}
self.attributedPlaceholder = NSAttributedString(string: placeholderTxt, attributes: [NSForegroundColorAttributeName: placeholderFontColor])
}
}
/** -- */
// MARK: - Overrides and Initializers
override init(frame: CGRect) {
super.init(frame: frame)
}
override func layoutSubviews() {
super.layoutSubviews()
// HERE
bottomBorder.frame = CGRect(x: 0, y: self.frame.size.height - self.bottomBorderWidth, width: self.frame.size.width, height: self.frame.size.height)
}
// setting the textField delegate to self
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
//self.borderStyle = .none
self.delegate = self
}
// MARK: - Events
func textFieldDidBeginEditing(_ textField: UITextField) {
}
You can use and extension on UITextFiled for setting the border.
And keep a reference to it with KVC.
By overriding LayoutSubview, every the layout will change, we'l check if the border exists, if so remove it, and re-create a new one with the new frame:
import UIKit
let MyTopBorder = "myTopBorder"
let MyBottomBorder = "myBottomBorder"
struct Defaults {
static let width = CGFloat(1.0)
static func bottonBorderFrame(view: UIView)->CGRect {
return CGRect(x: CGFloat(0), y: view.frame.size.height - Defaults.width, width: view.frame.size.width, height: view.frame.size.height)
}
static func topBorderFrame(view: UIView)->CGRect {
return CGRect(x: CGFloat(0), y: CGFloat(0) , width: view.frame.size.width, height: Defaults.width)
}
}
extension UITextField
{
func setBottomBorder(color:CGColor)
{
if let isBottomBorder = self.getBottomBorderIfExists() {
isBottomBorder.removeFromSuperlayer()
}
self.setBorderWithFrame(Defaults.bottonBorderFrame(self), color: color, andKey: MyBottomBorder)
}
func setTopBorder(color:CGColor)
{
if let isTopBorder = self.getTopBorderIfExists() {
isTopBorder.removeFromSuperlayer()
}
self.setBorderWithFrame(Defaults.topBorderFrame(self), color: color, andKey: MyTopBorder)
}
func setBorderWithFrame(frame: CGRect, color: CGColor, andKey: String) {
self.borderStyle = UITextBorderStyle.None;
let border = CALayer()
border.borderColor = color
border.frame = frame
border.borderWidth = Defaults.width
self.layer.addSublayer(border)
self.layer.masksToBounds = true
self.layer.setValue(border, forKey: andKey)
}
func removeTopBorder() {
if let isTopBorder = self.getTopBorderIfExists() {
self.layer.setValue(nil, forKey: MyTopBorder)
isTopBorder.removeFromSuperlayer()
}
}
func removeBottomBorder() {
if let isBottomBorder = self.getBottomBorderIfExists() {
self.layer.setValue(nil, forKey: MyBottomBorder)
isBottomBorder.removeFromSuperlayer()
}
}
private func getBorderIfExistsByKey(key: String)->CALayer? {
if let isBorderSet = self.layer.valueForKey(key) {
if let borderIsCALayer = isBorderSet as? CALayer {
return borderIsCALayer
}
}
return nil
}
private func getTopBorderIfExists()->CALayer? {
return self.getBorderIfExistsByKey(MyTopBorder)
}
private func getBottomBorderIfExists()->CALayer? {
return self.getBorderIfExistsByKey(MyBottomBorder)
}
public override func layoutSubviews() {
super.layoutSubviews()
// Update bottom on frame change
if let isBottomBorder = self.getBottomBorderIfExists() {
let borderColor = isBottomBorder .borderColor
self.removeBottomBorder()
self.setBottomBorder(borderColor!)
}
// Update top on frame change
if let isTopBorder = self.getTopBorderIfExists() {
let borderColor = isTopBorder.borderColor
self.removeTopBorder()
self.setTopBorder(borderColor!)
}
}
}
Usage:
let textField = UITextField(frame: CGRect(x: 100,y: 100, width: 100, height: 100))
textField.backgroundColor = UIColor.blueColor() // Thie color is for visulizing better
self.view.addSubview(textField)
textField.setBottomBorder(UIColor.blackColor().CGColor) // Now you have a border
textField.frame = CGRect(x: 150, y: 200, width: 200, height: 200) // And the border updated to the new frame
// Now if you would like to change from bottom to top, simply do this:
textField.removeBottomBorder()
textField.setTopBorder(UIColor.blackColor().CGColor)

UIView changing its position in swift

How do I make a UIView slide up with a touch of a button from its original position and them bring it back down with a touch of a button? Using Swift and Xcode 6.
I have currently tried this:
#IBOutlet weak var DynView: UIView!
#IBAction func btnUp(sender: AnyObject) {
}
You have to implement an animation changing the DynView position on click. Here's an example:
#IBAction func btnUp(sender: AnyObject) {
let xPosition = DynView.frame.origin.x
let yPosition = DynView.frame.origin.y - 20 // Slide Up - 20px
let width = DynView.frame.size.width
let height = DynView.frame.size.height
UIView.animateWithDuration(1.0, animations: {
dynView.frame = CGRect(x: xPosition, y: yPosition, width: width, height: height)
})
}
Hi create this extends if you want. For Swift
Create File Extends.Swift and add this code
/**
Extension UIView
by DaRk-_-D0G
*/
extension UIView {
/**
Set x Position
:param: x CGFloat
by DaRk-_-D0G
*/
func setX(#x:CGFloat) {
var frame:CGRect = self.frame
frame.origin.x = x
self.frame = frame
}
/**
Set y Position
:param: y CGFloat
by DaRk-_-D0G
*/
func setY(#y:CGFloat) {
var frame:CGRect = self.frame
frame.origin.y = y
self.frame = frame
}
/**
Set Width
:param: width CGFloat
by DaRk-_-D0G
*/
func setWidth(#width:CGFloat) {
var frame:CGRect = self.frame
frame.size.width = width
self.frame = frame
}
/**
Set Height
:param: height CGFloat
by DaRk-_-D0G
*/
func setHeight(#height:CGFloat) {
var frame:CGRect = self.frame
frame.size.height = height
self.frame = frame
}
}
For Use (inherits Of UIView)
inheritsOfUIView.setX(x: 100)
button.setX(x: 100)
view.setY(y: 100)
I kinda combined the two most voted answers into one and updated to Swift 3. So basically created an extension that animates a view moving to a different position:
extension UIView {
func slideX(x:CGFloat) {
let yPosition = self.frame.origin.y
let height = self.frame.height
let width = self.frame.width
UIView.animate(withDuration: 1.0, animations: {
self.frame = CGRect(x: x, y: yPosition, width: width, height: height)
})
}
}
// MARK: - Properties
var bottomViewHeight: CGFloat = 200
var isViewHide = false
private let bottomView: UIView = {
let view = UIView()
view.backgroundColor = .red
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
private let showHideButton: UIButton = {
let button = UIButton()
button.setTitle("Show / Hide", for: .normal)
button.setTitleColor(.black, for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(showHideButtonTapped(_:)), for: .touchUpInside)
return button
}()
// MARK: - Lifecycle
override func loadView() {
super.loadView()
view.addSubview(bottomView)
NSLayoutConstraint.activate([
bottomView.heightAnchor.constraint(equalToConstant: bottomViewHeight),
bottomView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
bottomView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
bottomView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
])
view.addSubview(showHideButton)
NSLayoutConstraint.activate([
showHideButton.widthAnchor.constraint(equalToConstant: 200),
showHideButton.heightAnchor.constraint(equalToConstant: 50),
showHideButton.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor),
showHideButton.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor)
])
}
override func viewDidLoad() {
super.viewDidLoad()
showHideView(isShow: isViewHide)
}
// MARK: - Selectors
#objc func showHideButtonTapped(_ sender: UIButton) {
print("👆 HIDE / SHOW BUTTON")
showHideView(isShow: isViewHide)
}
// MARK: - Functions
private func showHideView(isShow: Bool) {
if isShow {
UIView.animate(withDuration: 0.4) {
self.bottomView.transform = CGAffineTransform(translationX: 0, y: self.bottomViewHeight)
}
} else {
UIView.animate(withDuration: 0.4) {
self.bottomView.transform = CGAffineTransform(translationX: 0, y: 0)
}
}
isViewHide = !isViewHide
}

Resources