作者热门文章
- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
在 iOS 7 UISearchBar 上,占位符文本居中,我想禁用它并使其始终像以前一样位于左侧。
有一个私有(private)方法 setCenterPlaceholder,在 diffs 上用 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 方式”做同样的事情。
最佳答案
要在 iOS7 中实现占位符的左对齐,您可以在占位符字符串的末尾添加空格。例如:
_searchBar.placeholder = @"Search ";
请注意,它应该针对每个搜索栏和每种语言单独完成。
可以尝试为任何语言的文本后编写所需空间的自动计算,但似乎无法读取 searchBar 的 textField 框架的大小
<UISearchBarTextField: 0x1349c160; frame = (0 0; 0 0);
关于ios - 在 iOS 7 UISearchBar 左对齐占位符文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19666739/
我是一名优秀的程序员,十分优秀!