- (void)text:(NSString *)str color:(UIColor *)color font:(UIFont *)font

{

    if (!str)

        str = self.text;

    if (!color)

        color = self.textColor;

    if (!font)

        font = self.font;

    [self.attributeString setAttributes:@{NSForegroundColorAttributeName:color,

                                     NSFontAttributeName:font}

                             range:[self.text rangeOfString:str]];

}

 

- (void)changeColor

{

    self.attributedText = self.attributeString;

}

 

- (NSMutableAttributedString *)attributeString

{

    NSMutableAttributedString *attributeString = objc_getAssociatedObject(self, _cmd);

    if (attributeString && [attributeString.string isEqualToString:self.text]) {

        return attributeString;

    }

    attributeString = [[NSMutableAttributedString alloc] initWithString:self.text];

    objc_setAssociatedObject(self, _cmd, attributeString, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

    return attributeString;

}

 

相关文章:

  • 2022-12-23
  • 2021-11-17
  • 2021-12-13
  • 2021-05-03
  • 2021-05-24
  • 2022-01-18
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-09-11
  • 2021-07-04
  • 2022-12-23
  • 2021-09-09
相关资源
相似解决方案