2017-05-10 11 views
6

मैं अरबी भाषा में एक ऐप विकसित कर रहा हूं और मेरे पास टेक्स्ट एलाइनमेंट के साथ UITextField है। अब मैं बाईं तरफ टेक्स्ट फ़ील्ड का स्पष्ट बटन दिखाना चाहता हूं। कस्टम बटन जोड़ने के बिना ऐसा करना संभव है?UITextField के बाईं ओर स्पष्ट बटन कैसे प्रदर्शित करें?

वर्तमान स्थिति

clear button position

इच्छित स्थान

clear button position

उत्तर

7
श्रेणी से नीचे का प्रयोग करें

और सुनिश्चित करें कि आपके पाठ संरेखण सही :) होना चाहिए

@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

हालांकि मैं वाम से दाएं अनुप्रयोग भाषाओं से निपटने के लिए this answer जाँच करने के लिए, एक समाधान के रूप में आप userar's answer का पालन कर सकता है की सिफारिश करेंगे, निम्नलिखित कोड का टुकड़ा उसके जवाब का एक स्विफ्ट 3 संस्करण है:

एक कस्टम बनाएं UITextField वर्ग, के रूप में इस प्रकार है:

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

उत्पादन होगा:

enter image description here

0

स्विफ्ट 3 वाक्य रचना:

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

}

यह

काम करता है आशा
संबंधित मुद्दे