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 ?
You should be using NSForegroundColorAttributeName
, not kCTForegroundColorAttributeName
.
Also, the range being passed should be NSMakeRange(0, [s length]);
.
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)])
Simple version:
NSAttributedString *title = [[NSAttributedString alloc] initWithString:@"Refresh…"
attributes: @{NSForegroundColorAttributeName:[UIColor redColor]}];
refreshControl.attributedTitle = [[NSAttributedString alloc]initWithAttributedString:title];
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With