在iOS 7 UISearchBar左对齐占位符文本? [英] On iOS 7 UISearchBar left align placeholder text?

查看:286
本文介绍了在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);

这篇关于在iOS 7 UISearchBar左对齐占位符文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆