2017-05-10 27 views

Odpowiedz

7

Zastosowanie poniżej kategorii i upewnij się, że wyrównanie tekstu powinien mieć rację :)

@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 
2

Chociaż ja polecam sprawdzić this answer do obsługi lewy-prawy App językami, jako obejście można śledzić userar's answer, następujący fragment kodu jest Swift 3 wersja jego odpowiedź:

Tworzenie niestandardowego UITextField klasy, w następujący sposób:

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) 
    } 
} 

wyjście będzie:

enter image description here

0

SWIFT 3 składnia:

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) 
} 

}

nadzieję, że to działa