Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change color to attributedTitle in UIRefreshControl

I'm looking for a way to change color in UIRefreshControl. The text is shown in an NSAttributedString, so I try to use CoreText.framework:

 NSString *s = @"Hello";
 NSMutableAttributedString *a = [[NSMutableAttributedString alloc] initWithString:s];
 [a addAttribute:(id)kCTForegroundColorAttributeName value:(id)[UIColor redColor].CGColor range:NSRangeFromString(s)];
 refreshControl.attributedTitle = a;

The text is shown correctly, but the color is always the default gray. Any ideas ?

like image 801
Fry Avatar asked Oct 09 '12 12:10

Fry


4 Answers

You should be using NSForegroundColorAttributeName, not kCTForegroundColorAttributeName.


Also, the range being passed should be NSMakeRange(0, [s length]);.

like image 157
Dave DeLong Avatar answered Oct 19 '22 05:10

Dave DeLong


In Swift you can set the color of the attributedTitle as follows:

self.refreshControl?.attributedTitle = NSAttributedString(string: "Pull to refresh", attributes: [NSForegroundColorAttributeName: UIColor(red: 255.0/255.0, green: 182.0/255.0, blue: 8.0/255.0, alpha: 1.0)])
like image 44
Babatunde Adeyemi Avatar answered Oct 19 '22 06:10

Babatunde Adeyemi


Simple version:

NSAttributedString *title = [[NSAttributedString alloc] initWithString:@"Refresh…" 
attributes: @{NSForegroundColorAttributeName:[UIColor redColor]}];
refreshControl.attributedTitle = [[NSAttributedString alloc]initWithAttributedString:title];
like image 8
movaction.com Avatar answered Oct 19 '22 05:10

movaction.com


The "value" parameter you are passing to the "addAttribute" method is a CGColor, use UIColor instead and it will work! [UIColor redColor].CGColor

NSString *s = @"Hello";  
NSMutableAttributedString *a = [[NSMutableAttributedString alloc] initWithString:s];  
[a addAttribute:kCTForegroundColorAttributeName value:[UIColor redColor] range:NSRangeFromString(s)]; 
refreshControl.attributedTitle = a;
like image 5
user1874570 Avatar answered Oct 19 '22 05:10

user1874570