FLEXSystemLogCell.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // FLEXSystemLogCell.m
  3. // FLEX
  4. //
  5. // Created by Ryan Olson on 1/25/15.
  6. // Copyright (c) 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXSystemLogCell.h"
  9. #import "FLEXSystemLogMessage.h"
  10. #import "UIFont+FLEX.h"
  11. NSString *const kFLEXSystemLogCellIdentifier = @"FLEXSystemLogCellIdentifier";
  12. @interface FLEXSystemLogCell ()
  13. @property (nonatomic) UILabel *logMessageLabel;
  14. @property (nonatomic) NSAttributedString *logMessageAttributedText;
  15. @end
  16. @implementation FLEXSystemLogCell
  17. - (void)postInit {
  18. [super postInit];
  19. self.logMessageLabel = [UILabel new];
  20. self.logMessageLabel.numberOfLines = 0;
  21. #if !TARGET_OS_TV
  22. self.separatorInset = UIEdgeInsetsZero;
  23. self.selectionStyle = UITableViewCellSelectionStyleNone;
  24. #endif
  25. [self.contentView addSubview:self.logMessageLabel];
  26. }
  27. - (void)setLogMessage:(FLEXSystemLogMessage *)logMessage {
  28. if (![_logMessage isEqual:logMessage]) {
  29. _logMessage = logMessage;
  30. self.logMessageAttributedText = nil;
  31. [self setNeedsLayout];
  32. }
  33. }
  34. - (void)setHighlightedText:(NSString *)highlightedText {
  35. if (![_highlightedText isEqual:highlightedText]) {
  36. _highlightedText = highlightedText;
  37. self.logMessageAttributedText = nil;
  38. [self setNeedsLayout];
  39. }
  40. }
  41. - (NSAttributedString *)logMessageAttributedText {
  42. if (!_logMessageAttributedText) {
  43. _logMessageAttributedText = [[self class] attributedTextForLogMessage:self.logMessage highlightedText:self.highlightedText];
  44. }
  45. return _logMessageAttributedText;
  46. }
  47. static const UIEdgeInsets kFLEXLogMessageCellInsets = {10.0, 10.0, 10.0, 10.0};
  48. - (void)layoutSubviews {
  49. [super layoutSubviews];
  50. self.logMessageLabel.attributedText = self.logMessageAttributedText;
  51. self.logMessageLabel.frame = UIEdgeInsetsInsetRect(self.contentView.bounds, kFLEXLogMessageCellInsets);
  52. }
  53. #pragma mark - Stateless helpers
  54. + (NSAttributedString *)attributedTextForLogMessage:(FLEXSystemLogMessage *)logMessage highlightedText:(NSString *)highlightedText {
  55. NSString *text = [self displayedTextForLogMessage:logMessage];
  56. NSDictionary<NSString *, id> *attributes = @{ NSFontAttributeName : UIFont.flex_codeFont };
  57. NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:attributes];
  58. if (highlightedText.length > 0) {
  59. NSMutableAttributedString *mutableAttributedText = attributedText.mutableCopy;
  60. NSMutableDictionary<NSString *, id> *highlightAttributes = attributes.mutableCopy;
  61. highlightAttributes[NSBackgroundColorAttributeName] = UIColor.yellowColor;
  62. NSRange remainingSearchRange = NSMakeRange(0, text.length);
  63. while (remainingSearchRange.location < text.length) {
  64. remainingSearchRange.length = text.length - remainingSearchRange.location;
  65. NSRange foundRange = [text rangeOfString:highlightedText options:NSCaseInsensitiveSearch range:remainingSearchRange];
  66. if (foundRange.location != NSNotFound) {
  67. remainingSearchRange.location = foundRange.location + foundRange.length;
  68. [mutableAttributedText setAttributes:highlightAttributes range:foundRange];
  69. } else {
  70. break;
  71. }
  72. }
  73. attributedText = mutableAttributedText;
  74. }
  75. return attributedText;
  76. }
  77. + (NSString *)displayedTextForLogMessage:(FLEXSystemLogMessage *)logMessage {
  78. return [NSString stringWithFormat:@"%@: %@", [self logTimeStringFromDate:logMessage.date], logMessage.messageText];
  79. }
  80. + (CGFloat)preferredHeightForLogMessage:(FLEXSystemLogMessage *)logMessage inWidth:(CGFloat)width {
  81. UIEdgeInsets insets = kFLEXLogMessageCellInsets;
  82. CGFloat availableWidth = width - insets.left - insets.right;
  83. NSAttributedString *attributedLogText = [self attributedTextForLogMessage:logMessage highlightedText:nil];
  84. CGSize labelSize = [attributedLogText boundingRectWithSize:CGSizeMake(availableWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil].size;
  85. return labelSize.height + insets.top + insets.bottom;
  86. }
  87. + (NSString *)logTimeStringFromDate:(NSDate *)date {
  88. static NSDateFormatter *formatter = nil;
  89. static dispatch_once_t onceToken;
  90. dispatch_once(&onceToken, ^{
  91. formatter = [NSDateFormatter new];
  92. formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss.SSS";
  93. });
  94. return [formatter stringFromDate:date];
  95. }
  96. @end