gpt4 book ai didi

iphone - 如何更改 UISearchbar 取消按钮的文本颜色

转载 作者:可可西里 更新时间:2023-11-01 03:24:38 28 4
gpt4 key购买 nike

我有一个 UISearchBar,如下所示。如何更改取消按钮的文本颜色?

enter image description here

最佳答案

这个问题之前有人问过,所以我认为问的人已经找到了解决方案。但以防万一其他人碰巧遇到同样的问题。这是我的解决方案。

我有一个带有取消按钮的 UISearchBar,该按钮仅在点击 UISearchBar 的文本字段时出现。因此,在 UISearchBar 的子类中覆盖 -(void)layoutSubviews 的解决方案对我来说不是一个选择。不管怎样,我做了一个 UISearchBar (CustomSearchBar) 的子类,它有一个公共(public)方法来设置取消按钮的字体和 textColor。当我创建 UISearchBar 时,我确保搜索栏的文本字段委托(delegate)设置为 self 并且创建搜索栏的类实现了 UITextFieldDelegate 协议(protocol)。当用户点击搜索栏的文本字段时,其委托(delegate)会收到通知并调用 CustomSearchBar 的方法。我之所以在这里这样做,是因为这是取消按钮出现的时刻,因此我知道它在 View 层次结构中,我可以对其进行自定义。

代码如下:

在 MyRootViewController 中创建 UISearchBar

CustomSearchBar *searchBar = [[CustomSearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 40)];
[searchBar setBarStyle:UIBarStyleDefault];
[searchBar setTintColor:[UIColor whiteColor]];

for (UIView *view in [searchBar subviews])
{
if ([view isKindOfClass:[UITextField class]])
{
UITextField *searchTextField = (UITextField *)view;
[searchTextField setDelegate:self];
}
}

self.searchBar = searchBar;
[searchBar release];

MyRootViewController 中的 UITextFieldDelegate(确保它实现了 UITextFieldDelegate 协议(protocol))

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self.searchBar setCloseButtonFont:[UIFont fontWithName:@"American Typewriter" size:14] textColor:[UIColor grayColor]];
}

这是 UISearchBar 子类中的公共(public)方法

- (void)setCloseButtonFont:(UIFont *)font textColor:(UIColor *)textColor
{
UIButton *cancelButton = nil;

for(UIView *subView in self.subviews)
{
if([subView isKindOfClass:[UIButton class]])
{
cancelButton = (UIButton*)subView;
}
}

if (cancelButton)
{
/* For some strange reason, this code changes the font but not the text color. I assume some other internal customizations make this not possible:

UILabel *titleLabel = [cancelButton titleLabel];
[titleLabel setFont:font];
[titleLabel setTextColor:[UIColor redColor]];*/

// Therefore I had to create view with a label on top:
UIView *overlay = [[UIView alloc] initWithFrame:CGRectMake(2, 2, kCancelButtonWidth, kCancelButtonLabelHeight)];
[overlay setBackgroundColor:[UIColor whiteColor]];
[overlay setUserInteractionEnabled:NO]; // This is important for the cancel button to work
[cancelButton addSubview:overlay];

UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, kCancelButtonWidth, kCancelButtonLabelHeight)];
[newLabel setFont:font];
[newLabel setTextColor:textColor];
// Text "Cancel" should be localized for other languages
[newLabel setText:@"Cancel"];
[newLabel setTextAlignment:UITextAlignmentCenter];
// This is important for the cancel button to work
[newLabel setUserInteractionEnabled:NO];
[overlay addSubview:newLabel];
[newLabel release];
[overlay release];
}
}

关于iphone - 如何更改 UISearchbar 取消按钮的文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6992917/

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