Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display clear button in left side of UITextField?

I'm developing an app in Arabic language and I have UITextField with textAlignment right. Now I want to show the clear button of the textField in left side. Is it possible to do this without adding a custom button?

Current position

clear button position

Desired position

clear button position

like image 423
Aruldevi R Avatar asked May 10 '17 06:05

Aruldevi R


3 Answers

SWIFT 3 syntax:

 class TextFields: UITextField {

  // You will need this
private var firstPlace = CGRect.zero

override func awakeFromNib() {
    super.awakeFromNib()

    firstPlace = super.clearButtonRect(forBounds: bounds) // to access clear button properties

/* uncomment these following lines if you want but you can change them in main.storyboard too 

    clearButtonMode = .whileEditing // to show the clear button only when typing starts

    textAlignment = .right // to put the text to right side
*/
}
   // Function to change the clear button
override func clearButtonRect(forBounds bounds: CGRect) -> CGRect {

    return firstPlace.offsetBy(dx: -firstPlace.origin.x + 5, dy: 0)
}

}

hope it works

like image 121
Atrin Noori Avatar answered Nov 06 '22 22:11

Atrin Noori


Use below category and make sure your text alignment should be right :)

@interface UICrossButtonTextField:UITextField
- (CGRect)clearButtonRectForBounds:(CGRect)bounds;
@end

@implementation UICrossButtonTextField
- (CGRect)clearButtonRectForBounds:(CGRect)bounds {
    CGRect originalRect = [super clearButtonRectForBounds:bounds];
    return CGRectOffset(originalRect, -originalRect.origin.x+5, 0); }


- (CGRect)editingRectForBounds:(CGRect)bounds {
    CGRect originalRect = [super clearButtonRectForBounds:bounds];
    bounds = CGRectMake(originalRect.size.width, bounds.origin.y, bounds.size.width-originalRect.size.width, bounds.size.height);
    return CGRectInset(bounds, 13, 3);
}

@end
like image 43
Arun Avatar answered Nov 06 '22 22:11

Arun


Although I would recommend to check this answer for handling Left-to-Right App languages, as a workaround you could follow userar's answer, the following code snippet is a Swift 3 version of his answer:

Create a custom UITextField class, as follows:

class CustomTextField: UITextField {
    private var originalRect = CGRect.zero

    override func awakeFromNib() {
        super.awakeFromNib()

        originalRect = super.clearButtonRect(forBounds: bounds)
        clearButtonMode = .whileEditing
        textAlignment = .right
    }

    override func clearButtonRect(forBounds bounds: CGRect) -> CGRect {
        return originalRect.offsetBy(dx: -originalRect.origin.x + 5, dy: 0)
    }

    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        let bounds = CGRect(x: originalRect.size.width, y: bounds.origin.y, width: bounds.size.width-originalRect.size.width, height: bounds.size.height)
        return bounds.insetBy(dx: 13, dy: 3)
    }
}

The output would be:

enter image description here

like image 29
Ahmad F Avatar answered Nov 06 '22 23:11

Ahmad F