gpt4 book ai didi

ios - 在 iOS 7 UISearchBar 左对齐占位符文本?

转载 作者:技术小花猫 更新时间:2023-10-29 10:17:54 24 4
gpt4 key购买 nike

在 iOS 7 UISearchBar 上,占位符文本居中,我想禁用它并使其始终像以前一样位于左侧。

enter image description here

有一个私有(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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com