在iOS 7 UISearchBar左对齐占位符文本?
在iOS 7 UISearchBar中,占位符文本居中,我想禁用它,并使其始终保持向左,就像以前一样。
On iOS 7 UISearchBar the placeholder text is centred and I want to disable that and make it always stick to the left like it was before.
>
有一个私有方法setCenterPlaceholder,对diff和调用此与BOOL将使行程。 https://gist.github.com/nscoding/7220368
There is a private method setCenterPlaceholder, on the diffs and calling this with a BOOL will make the trip. 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
实施文件 p>
Implementation file
#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方式做同样的事情。
I am wondering if there is any other, not "Hacky way" that does the same thing.
p>要在iOS7中向左对齐占位符,可以在占位符字符串的末尾添加空格。例如:
To get left alignment of the placeholder in iOS7 you can add white spaces to the end of your placeholder string. For example:
_searchBar.placeholder = @"Search ";
请注意,每个搜索栏和每种语言都应单独完成。
Note that it is should be done separately for each search bar and each language.
可以尝试在任何语言的文本之后写入所需空格的自动计算,但似乎无法读取searchBar的textField框架的大小
One could try to write automatic calculation of needed spaces after text for any language but it seems impossible to read the size of searchBar's textField frame
<UISearchBarTextField: 0x1349c160; frame = (0 0; 0 0);