iOS - Using TTTAttributedLabel to set two color text

后端 未结 2 1376
抹茶落季
抹茶落季 2021-02-06 06:22

I\'m creating iOS app that has a label. I want to set two colors. One for first part and other color for remaining part.
I\'ve seen some messages in Stack over flow that, TT

2条回答
  •  孤街浪徒
    2021-02-06 06:25

    You could use TTTRegexAttributedLabel available at : https://github.com/kwent/TTTRegexAttributedLabel. (TTTAttributedLabel based but more easier to use with regular expressions)

      //SET FONT ONLY ON FIRST MATCH REGEX
      TTTRegexAttributedLabel *label = [[TTTRegexAttributedLabel alloc] init];
      label.textColor = [UIColor whiteColor];
      NSString *s = @"ABC > def";
      [self.label setText:s withFirstMatchRegex:@"^[a-zA-Z ]*>"
                            withFont:[UIFont systemFontOfSize:12]
                            withColor:[UIColor brownColor]];
    

提交回复
热议问题