如何设置 UISearchBar 的占位符的文本颜色 [英] How to set the text color of the UISearchBar's placeholder

查看:32
本文介绍了如何设置 UISearchBar 的占位符的文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个深色背景的 UISearchBar,所以我试图更改 UISearchBar 的占位符文本颜色(默认情况下为灰色),但我没有找到设置它的方法.所以我想寻求一些帮助 :) 请提前告诉我如何实现这一点 :)

I have a UISearchBar with a dark background color so I was trying to change the place holder text color of UISearchBar (which will be gray by default) but I didn't find a way to set it. So I thought of getting some help :) please suggest me how this can be achieved thanks in advance :)

推荐答案

注意:在我写这个答案的时候,我正在研究 iOS 7 &此解决方法也适用于 iOS 8.它可能不适用于 iOS 9.

Note: At the time I wrote this answer I was working on iOS 7 & this workaround worked on iOS 8 as well. It may not work on iOS 9.

好的,我发布这个问题已经两天了.虽然我没有得到答案,但我找到了解决此问题的方法.

Ok it's been two days since I posted this question. Though I din't get the answer I got a workaround for this problem.

注意:我正在为我的应用程序使用故事板 &我已经对 UISearchBar 进行了子类化,这个解决方法对我来说就像一个宝石.

Note : I am using storyboard for my application & I have subclassed the UISearchBar and this workaround working like a gem for me.

首先,当 UILabel 包含在 UISearch 栏类中时,首先向 UILabel 添加一个外观代理,如下所示:

First and foremost add an appearance proxy to UILabel when its contained in the UISearch bar class like this :

[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor whiteColor]];//change the color to whichever color needed

那么最重要的是,如果您使用故事板,您必须并且应该像这样在属性检查器的占位符字段中编写一些文本

Then the most important thing is If you're using storyboard you must and should write some text in the placeholder field of the attribute inspector like this

如果您忘记在占位符字段中写一些东西,那么当您运行应用程序时,这个技巧肯定不起作用,并且占位符文本将显示为通常的灰色.

If you forget to write something in the placeholder field, definitely this trick will not work when you run the application and the placeholder text will look in usual gray color.

在占位符字段中写完一些文本后,像这样在 UISearchBar 子类中设置占位符文本:

Once you're done with writing some text in placeholder field, set the place holder text in your UISearchBar sub class like this :

self.placeholder = @"My Placeholder text";

现在运行应用程序!!!您将获得您在 appdelegate 中设置的颜色的占位符文本:) 希望这对某人有所帮助:)

Now run the application !!! You'll get the place holder text in the color which you have set in appdelegate:) Hope this helps someone :)

这篇关于如何设置 UISearchBar 的占位符的文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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