iOS 修改SearchBar的Textfiled的Cursor游标颜色和字体样式和大小;Disable输入框

因为Search Bar的游标颜色和Tint颜色是统一的,如果要想将游标的颜色设置为其他颜色,则需要到textfiled内部去修改,对于字体也是同样。

实现代码:

- (void)setSearchBarTextfiled:(UISearchBar *)searchBar{
    for (UIView *view in searchBar.subviews){
        for (id subview in view.subviews){
            if ( [subview isKindOfClass:[UITextField class]] ){
                [(UITextField *)subview setTintColor:[UIColor grayColor]];
                [(UITextField *)subview setFont:[UIFont fontWithName:GENERAL_FONT size:13]];
                return;
            }
        }
    }
}

如果要实现用户不能输入到搜索框中,则需要是textfiled的输入失效:

一种方法是对于整个Searchbar设置Disable,但是这样如果有Scope Button等其他组件的话,也会连同失效:

[searchBar setUserInteractionEnabled:NO];

实现代码:

- (void)enableTextInput:(UISearchBar *)searchBar{
    for (UIView *view in searchBar.subviews){
        for (id subview in view.subviews){
            if ( [subview isKindOfClass:[UITextField class]] ){
                [subview setEnabled:YES];
                return;
            }
        }
    }
}

- (void)disableTextInput:(UISearchBar *)searchBar{
    for (UIView *view in searchBar.subviews){
        for (id subview in view.subviews){
            if ( [subview isKindOfClass:[UITextField class]] ){
                [subview setEnabled:NO];
                return;
            }
        }
    }
}
参考链接:

http://stackoverflow.com/questions/9968595/how-to-enable-cancel-button-with-uisearchbar

http://stackoverflow.com/questions/11606007/change-uitextfield-and-uitextview-cursor-caret-color

http://stackoverflow.com/questions/2190352/change-color-of-cursor-in-text-field

http://stackoverflow.com/questions/9169049/disable-uisearchbar

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。