如何改变UISearchBar字体大小&颜色?

时间:2018-02-26 07:33:29

标签: swift uisearchbar textcolor

我几个小时后谷歌如何更改我的UISearchBar字体大小&颜色,但我不能与之相关的任何文件。

这是我迄今为止在swift 4上所做的:

 searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: view.frame.width - (menuImage.frame.width + iconImage.frame.width + 55), height: 30))
 searchBar.placeholder = "SEARCH BAR"
 searchBar.tintColor = UIColor.gray
 searchBar.delegate = self
 searchBar.font = [UIFont fontWithName:@"Oswald" size:11];

但它给了我一个错误。

你能告诉我如何更改UISearchBar字体大小&颜色?

请帮助!!!

5 个答案:

答案 0 :(得分:8)

更改文字和占位符搜索栏:

#mpzmq_class.py

from multiprocessing import Process
import zmq


class Base(Process):
    """
    Inherit from Process and
    holds the zmq address.
    """
    def __init__(self, address):
        super().__init__()
        self.address = address


class Server(Base):
    def run(self):
        ctx = zmq.Context()
        socket = ctx.socket(zmq.PULL)
        socket.connect(self.address)
        msg = socket.recv_string()
        print(msg)


class Client(Base):
    def run(self):
        ctx = zmq.Context()
        socket = ctx.socket(zmq.PUSH)
        socket.bind(self.address)
        msg = "Hello World!"
        socket.send_string(msg)


if __name__ == "__main__":
    server_addr = "tcp://127.0.1:6068"
    client_addr = "tcp://*:6068"
    s = Server(server_addr)
    c = Client(client_addr)
    s.start()
    c.start()
    s.join()
    c.join()

答案 1 :(得分:4)

在iOS 13或最新版本中,UISearchBar具有一个名为searchTextField的属性。但更早之前,我们不能使用此属性。

因此,我们可以扩展UISearchBar:

extension UISearchBar {
    var textField: UITextField? {
        if #available(iOS 13.0, *) {
            return self.searchTextField
        } else {
            // Fallback on earlier versions
            for view in (self.subviews[0]).subviews {
                if let textField = view as? UITextField {
                    return textField
                }
            }
        }
        return nil
    }
}

并使用:

searchBar.textField?.font = UIFont.systemFont(ofSize: 12)

答案 2 :(得分:2)

以下是您可以在最新的Swift中更改字体和文本颜色的方法:

let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(15), NSAttributedString.Key.foregroundColor: UIColor.gray]
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = attributes

答案 3 :(得分:1)

您可以使用外观代理为public class PrimeNumber { public static void main(String []args){ int [] arr = {2,3,4,5,9,6,7,13,24,42,8,400,101}; int [] arr1 = new int[4]; int [] arr2 = new int[arr.length - arr1.length]; boolean flag = true; for(int i = 0; i < arr.length; i++){ if(arr[i] == 2 || arr[i] == 3 ) { System.out.println("Number is prime " + arr[i]); continue; } flag = true; for(int j = 2; j <= Math.sqrt( arr[i] ); j++){ if(arr[i] % j == 0){ System.out.println("Number is not prime " + arr[i]); flag = false; break; } } if ( flag ) { System.out.println("Number is prime " + arr[i]); } } } } 文本和占位符设置样式:

更改占位符:

UISearchBar

更改搜索文本:

let placeholderAppearance = UILabel.appearance(whenContainedInInstancesOf: [UISearchBar.self])
placeholderAppearance.font = UIFont(name: "Baskerville", size: 14)
placeholderAppearance.textColor = .red

答案 4 :(得分:0)

searchBarOutlet.setScopeBarButtonTitleTextAttributes([NSForegroundColorAttributeName:UIColor.whiteColor()],forState:UIControlState.Normal)