在UIRefreshControl中将颜色更改为attributionTitle

Fry*_*Fry 6 nsattributedstring ios ios6 uirefreshcontrol

我正在寻找一种改变颜色的方法UIRefreshControl.文本显示在一个NSAttributedString,所以我尝试使用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;
Run Code Online (Sandbox Code Playgroud)

文本显示正确,但颜色始终为默认灰色.有任何想法吗 ?

Dav*_*ong 11

你应该使用NSForegroundColorAttributeName,而不是kCTForegroundColorAttributeName.


此外,通过的范围应该是NSMakeRange(0, [s length]);.


Bab*_*emi 10

在Swift中,您可以设置attributesTitle的颜色,如下所示:

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)])
Run Code Online (Sandbox Code Playgroud)


小智 8

简单版本:

NSAttributedString *title = [[NSAttributedString alloc] initWithString:@"Refresh…" 
attributes: @{NSForegroundColorAttributeName:[UIColor redColor]}];
refreshControl.attributedTitle = [[NSAttributedString alloc]initWithAttributedString:title];
Run Code Online (Sandbox Code Playgroud)


小智 5

你传递给"addAttribute"方法的"value"参数是一个CGColor,改为使用UIColor它会起作用![UIColor redColor] .CGColor

NSString *s = @"Hello";  
NSMutableAttributedString *a = [[NSMutableAttributedString alloc] initWithString:s];  
[a addAttribute:kCTForegroundColorAttributeName value:[UIColor redColor] range:NSRangeFromString(s)]; 
refreshControl.attributedTitle = a;
Run Code Online (Sandbox Code Playgroud)