EnhancedFeedbackExample.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // EnhancedFeedbackExample.m
  3. // MiMoLive
  4. //
  5. // 增强版使用示例:展示修正后的多样式文本功能
  6. //
  7. #import "MOBaseLevelView.h"
  8. @interface EnhancedFeedbackExample : UIViewController
  9. @end
  10. @implementation EnhancedFeedbackExample
  11. - (void)viewDidLoad {
  12. [super viewDidLoad];
  13. self.view.backgroundColor = [UIColor whiteColor];
  14. [self setupEnhancedExamples];
  15. }
  16. - (void)setupEnhancedExamples {
  17. // 示例1:多个不同样式的文本(修正后的版本)
  18. UILabel *multiStyleLabel = [[UILabel alloc] init];
  19. multiStyleLabel.numberOfLines = 0;
  20. multiStyleLabel.frame = CGRectMake(20, 100, self.view.frame.size.width - 40, 80);
  21. NSString *multiText = @"Contact us via Email or call Support hotline for Help";
  22. // 第一次调用:设置Email样式(蓝色下划线)
  23. [MOBaseLevelView setStyledTextForLabel:multiStyleLabel
  24. fullText:multiText
  25. targetText:@"Email"
  26. color:[UIColor blueColor]
  27. hasUnderline:YES];
  28. // 第二次调用:设置Support样式(红色无下划线)- 不会覆盖Email的样式
  29. [MOBaseLevelView setStyledTextForLabel:multiStyleLabel
  30. fullText:multiText
  31. targetText:@"Support"
  32. color:[UIColor redColor]
  33. hasUnderline:NO];
  34. // 第三次调用:设置Help样式(绿色下划线)- 不会覆盖之前的样式
  35. [MOBaseLevelView setStyledTextForLabel:multiStyleLabel
  36. fullText:multiText
  37. targetText:@"Help"
  38. color:[UIColor greenColor]
  39. hasUnderline:YES];
  40. [self.view addSubview:multiStyleLabel];
  41. // 示例2:动态修改样式
  42. UILabel *dynamicLabel = [[UILabel alloc] init];
  43. dynamicLabel.numberOfLines = 0;
  44. dynamicLabel.frame = CGRectMake(20, 200, self.view.frame.size.width - 40, 60);
  45. NSString *dynamicText = @"Click Terms or Privacy to learn more";
  46. // 初始设置
  47. [MOBaseLevelView setStyledTextForLabel:dynamicLabel
  48. fullText:dynamicText
  49. targetText:@"Terms"
  50. color:[UIColor blueColor]
  51. hasUnderline:YES];
  52. [MOBaseLevelView setStyledTextForLabel:dynamicLabel
  53. fullText:dynamicText
  54. targetText:@"Privacy"
  55. color:[UIColor blueColor]
  56. hasUnderline:YES];
  57. [self.view addSubview:dynamicLabel];
  58. // 添加按钮来动态修改样式
  59. UIButton *changeStyleButton = [UIButton buttonWithType:UIButtonTypeSystem];
  60. changeStyleButton.frame = CGRectMake(20, 280, 200, 40);
  61. [changeStyleButton setTitle:@"修改Privacy样式" forState:UIControlStateNormal];
  62. [changeStyleButton addTarget:self action:@selector(changePrivacyStyle:) forControlEvents:UIControlEventTouchUpInside];
  63. [self.view addSubview:changeStyleButton];
  64. // 存储label引用以便后续修改
  65. objc_setAssociatedObject(changeStyleButton, @"targetLabel", dynamicLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  66. // 示例3:错误处理演示
  67. UILabel *errorTestLabel = [[UILabel alloc] init];
  68. errorTestLabel.numberOfLines = 0;
  69. errorTestLabel.frame = CGRectMake(20, 340, self.view.frame.size.width - 40, 60);
  70. NSString *originalText = @"Original text with Keyword";
  71. NSString *newText = @"Different text with NewKeyword";
  72. // 设置原始文本样式
  73. [MOBaseLevelView setStyledTextForLabel:errorTestLabel
  74. fullText:originalText
  75. targetText:@"Keyword"
  76. color:[UIColor purpleColor]
  77. hasUnderline:YES];
  78. // 尝试在不同文本上设置样式(会重新创建富文本)
  79. [MOBaseLevelView setStyledTextForLabel:errorTestLabel
  80. fullText:newText
  81. targetText:@"NewKeyword"
  82. color:[UIColor orangeColor]
  83. hasUnderline:NO];
  84. [self.view addSubview:errorTestLabel];
  85. // 示例4:使用便利方法一次性设置多个样式
  86. UILabel *convenienceLabel = [[UILabel alloc] init];
  87. convenienceLabel.numberOfLines = 0;
  88. convenienceLabel.frame = CGRectMake(20, 420, self.view.frame.size.width - 40, 60);
  89. NSString *convenienceText = @"Visit our Website, read FAQ, or contact Support team";
  90. // 使用便利方法一次性设置多个样式
  91. NSArray *styleConfigs = @[
  92. @{
  93. @"targetText": @"Website",
  94. @"color": [UIColor blueColor],
  95. @"hasUnderline": @YES
  96. },
  97. @{
  98. @"targetText": @"FAQ",
  99. @"color": [UIColor purpleColor],
  100. @"hasUnderline": @NO
  101. },
  102. @{
  103. @"targetText": @"Support",
  104. @"color": [UIColor redColor],
  105. @"hasUnderline": @YES
  106. }
  107. ];
  108. [MOBaseLevelView setMultipleStyledTextForLabel:convenienceLabel
  109. fullText:convenienceText
  110. styleConfigs:styleConfigs];
  111. [self.view addSubview:convenienceLabel];
  112. // 添加说明标签
  113. UILabel *descriptionLabel = [[UILabel alloc] init];
  114. descriptionLabel.numberOfLines = 0;
  115. descriptionLabel.frame = CGRectMake(20, 500, self.view.frame.size.width - 40, 120);
  116. descriptionLabel.font = [UIFont systemFontOfSize:12.0];
  117. descriptionLabel.textColor = [UIColor grayColor];
  118. descriptionLabel.text = @"说明:\n1. 第一个标签:多次调用不会相互覆盖\n2. 第二个标签:可以动态修改样式\n3. 第三个标签:文本内容变化时的处理\n4. 第四个标签:便利方法一次性设置多个样式";
  119. [self.view addSubview:descriptionLabel];
  120. }
  121. #pragma mark - Action Methods
  122. - (void)changePrivacyStyle:(UIButton *)sender {
  123. UILabel *targetLabel = objc_getAssociatedObject(sender, @"targetLabel");
  124. if (targetLabel) {
  125. // 修改Privacy的样式为红色无下划线
  126. [MOBaseLevelView setStyledTextForLabel:targetLabel
  127. fullText:@"Click Terms or Privacy to learn more"
  128. targetText:@"Privacy"
  129. color:[UIColor redColor]
  130. hasUnderline:NO];
  131. [sender setTitle:@"样式已修改" forState:UIControlStateNormal];
  132. sender.enabled = NO;
  133. }
  134. }
  135. @end