Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the font and color of UIRefreshControl

I'm trying to add a pull to refresh to my table view controller, so I have worked on the functionality and then I'm working on the layout. The functionality works perfect but I have some issues related to the layout, I want to change the font and font color of the title. I changed them on the Attribute of the refresh controller on the storyboard, but each time I run the project all my settings are back to default. So, I have tried to work on them using the code, and now I'm able to change the background and the tintColor, but I'm not able to change the font and color. Could you please help me, and this is my code:

    refresh.backgroundColor = UIColor(red: 220/255, green: 220/255, blue: 220/255, alpha: 1)
    refresh.tintColor = UIColor(red: 155/255, green: 155/255, blue: 154/255, alpha: 1)
    var label:NSAttributedString = NSAttributedString(string: "Refresh!!!")

    refresh.attributedTitle = label

Thanks,

like image 468
Mouhamad Kawas Avatar asked Feb 21 '15 21:02

Mouhamad Kawas


3 Answers

I see that this question is pretty old but just in case someone else stumbles upon this when searching for the answer here's a way.

The colour is part of the attributed string and is set through the attributes dictionary.

let attributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
let attributedTitle = NSAttributedString(string: "title", attributes: attributes)

refreshControl.attributedTitle = attributedTitle

The same is true for the font.

[NSFontAttributeName: UIFont(name: fontName, size: size)]
like image 74
Moriya Avatar answered Nov 13 '22 06:11

Moriya


Swift 4:

let attributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
refreshControl.attributedTitle = NSAttributedString(string: "Refreshing please wait", attributes: attributes)
like image 36
Code Ratchet Avatar answered Nov 13 '22 05:11

Code Ratchet


Swift 3:

let attributes = [NSForegroundColorAttributeName: UIColor.white, NSFontAttributeName: UIFont.systemFont(ofSize: 12)]
refreshControl.attributedTitle = NSAttributedString(string: "Test text", attributes: attributes)
like image 39
Roland Keesom Avatar answered Nov 13 '22 06:11

Roland Keesom