Измените цвет на attributeTitle в 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;

Текст отображается правильно, но цвет всегда серый по умолчанию. Есть идеи?

7 ответов

Решение

Вы должны использовать NSForegroundColorAttributeNameне kCTForegroundColorAttributeName,


Кроме того, передаваемый диапазон должен быть NSMakeRange(0, [s length]);,

В Swift вы можете установить цвет атрибута атрибута следующим образом:

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)])

Простая версия:

NSAttributedString *title = [[NSAttributedString alloc] initWithString:@"Refresh…" 
attributes: @{NSForegroundColorAttributeName:[UIColor redColor]}];
refreshControl.attributedTitle = [[NSAttributedString alloc]initWithAttributedString:title];

Параметр "value", который вы передаете методу addAttribute, является 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;
NSString *s = @"Hello";

NSMutableAttributedString *a = [[NSMutableAttributedString alloc] initWithString:s];

NSDictionary *refreshAttributes = @{
  NSForegroundColorAttributeName: [UIColor blueColor],
};

[a setAttributes:refreshAttributes range:NSMakeRange(0, a.length)];

refreshControl.attributedTitle = a;

Я нашел ответ здесь: http://ioscreator.com/format-text-in-ios6-attributed-strings/

Swift Version 4 (iOS 11)

        let myString = "Pull to refresh"
        let myAttribute = [ NSAttributedString.Key.foregroundColor: UIColor.black ]
        let myAttrString = NSAttributedString(string: myString, attributes: myAttribute)
        refreshControl.attributedTitle = myAttrString

Использовать этот метод,

  • (id) initWithString: (NSString *) str атрибуты: (NSDictionary *) attrs;
  • (id) initWithAttributedString: (NSAttributedString *) attrStr;

следовательно,

NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:[UIColor whiteColor] forKey:NSBackgroundColorAttributeName]; //background color :optional
[attributes setObject:[UIColor redColor] forKey:NSForegroundColorAttributeName];  //title text color :optionala
NSAttributedString *title = [[NSAttributedString alloc] initWithString:@"Refresh!!" attributes:attributes];

_refreshcontrol.attributedTitle = [[NSAttributedString alloc]initWithAttributedString:title];
Другие вопросы по тегам