2013-10-29 15 views
14

आईओएस 7 पर UISearchBar प्लेसहोल्डर टेक्स्ट केंद्रित है और मैं इसे अक्षम करना चाहता हूं और इसे हमेशा बाईं ओर चिपकाना चाहता हूं जैसा कि पहले था।आईओएस 7 पर UISearchBar प्लेसहोल्डर पाठ संरेखित छोड़ दिया?

enter image description here

एक निजी विधि setCenterPlaceholder, नहीं है डिफ पर और एक BOOL के साथ इस फोन कर यात्रा कर देगा। https://gist.github.com/nscoding/7220368

हैडर फ़ाइल

@interface NSCodingSearchBar : UISearchBar 

// Default by the system is YES. 
// https://github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/UIKit.framework/UISearchBar.h 
@property (nonatomic, assign, setter = setHasCentredPlaceholder:) BOOL hasCentredPlaceholder; 

@end 

कार्यान्वयन फ़ाइल

#import "NSCodingSearchBar.h" 


// ------------------------------------------------------------------------------------------ 


@implementation NSCodingSearchBar 


// ------------------------------------------------------------------------------------------ 
#pragma mark - Initializers 
// ------------------------------------------------------------------------------------------ 
- (instancetype)initWithFrame:(CGRect)frame 
{ 
    if ((self = [super initWithFrame:frame])) 
    { 
     self.hasCentredPlaceholder = YES; 
    } 

    return self; 
} 


// ------------------------------------------------------------------------------------------ 
#pragma mark - Methods 
// ------------------------------------------------------------------------------------------ 
- (void)setHasCentredPlaceholder:(BOOL)hasCentredPlaceholder 
{ 
    _hasCentredPlaceholder = hasCentredPlaceholder; 

    SEL centerSelector = NSSelectorFromString([NSString stringWithFormat:@"%@%@", @"setCenter", @"Placeholder:"]); 
    if ([self respondsToSelector:centerSelector]) 
    { 
     NSMethodSignature *signature = [[UISearchBar class] instanceMethodSignatureForSelector:centerSelector]; 
     NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; 
     [invocation setTarget:self]; 
     [invocation setSelector:centerSelector]; 
     [invocation setArgument:&_hasCentredPlaceholder atIndex:2]; 
     [invocation invoke]; 
    } 

} 


@end 

यदि आपका कोई और नहीं, "Hacky रास्ता" वही चीज़ है मैं सोच रहा हूँ।

+0

यह बेहतर यदि आप विचाराधीन कोड शामिल है, के बजाय Searchbar के पाठ फ़ील्ड फ्रेम के आकार को पढ़ने के लिए असंभव लगता है बस इसे जोड़ने के लिए। –

+0

क्या आपने इसे आजमाया? http://stackoverflow.com/a/19555305/2155985 –

+0

मुझे यह अच्छा विचार नहीं लगता है। आपको जितना संभव हो मानकों के साथ प्रयास करना चाहिए। – anders

उत्तर

0

मैंने UISearchBar के साथ इसे आजमाया नहीं है लेकिन यह UITextField के साथ काम करता है। मैं इसे subclassed और leftViewRectForBounds:, textRectForBounds: और editingRectForBounds: overrode, यहाँ कोड है:

- (CGRect)textRectForBounds:(CGRect)bounds { 
    CGRect inset = CGRectMake(bounds.origin.x + kLeftTextPadding, 
         bounds.origin.y, 
         bounds.size.width - kLeftTextPadding, 
         bounds.size.height); 
    return inset; 
} 

आशा है कि यह मदद करता है।

3

आईओएस 7 में प्लेसहोल्डर के बाएं संरेखण प्राप्त करने के लिए आप अपने प्लेसहोल्डर स्ट्रिंग के अंत में सफेद रिक्त स्थान जोड़ सकते हैं। उदाहरण के लिए:

_searchBar.placeholder = @"Search     "; 

ध्यान दें कि यह प्रत्येक खोज बार और प्रत्येक भाषा के लिए अलग से किया जाना चाहिए।

एक किसी भी भाषा के लिए पाठ के बाद जरूरत रिक्त स्थान का स्वत: गणना लिखने के लिए कोशिश कर सकते हैं लेकिन यह

<UISearchBarTextField: 0x1349c160; frame = (0 0; 0 0); 
+0

क्या गलत है? यह वास्तव में काम करता है और बस मूर्खतापूर्ण दिखता है। – malex

+0

मैंने इसे अपने स्वयं का इस्तेमाल किया। अब, उन सफेद रिक्त स्थानों को कैसे जोड़ें? –

+0

आप सामान्य रिक्त स्थान टाइप कर सकते हैं जैसे उदाहरण के बाद ऊपर और पहले " या आप विभिन्न सफेद रिक्त स्थान के utf-8 कोड लिख सकते हैं \ uXXXX – malex

संबंधित मुद्दे