| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- //
- // EnhancedFeedbackExample.m
- // MiMoLive
- //
- // 增强版使用示例:展示修正后的多样式文本功能
- //
- #import "MOBaseLevelView.h"
- @interface EnhancedFeedbackExample : UIViewController
- @end
- @implementation EnhancedFeedbackExample
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor whiteColor];
-
- [self setupEnhancedExamples];
- }
- - (void)setupEnhancedExamples {
-
- // 示例1:多个不同样式的文本(修正后的版本)
- UILabel *multiStyleLabel = [[UILabel alloc] init];
- multiStyleLabel.numberOfLines = 0;
- multiStyleLabel.frame = CGRectMake(20, 100, self.view.frame.size.width - 40, 80);
-
- NSString *multiText = @"Contact us via Email or call Support hotline for Help";
-
- // 第一次调用:设置Email样式(蓝色下划线)
- [MOBaseLevelView setStyledTextForLabel:multiStyleLabel
- fullText:multiText
- targetText:@"Email"
- color:[UIColor blueColor]
- hasUnderline:YES];
-
- // 第二次调用:设置Support样式(红色无下划线)- 不会覆盖Email的样式
- [MOBaseLevelView setStyledTextForLabel:multiStyleLabel
- fullText:multiText
- targetText:@"Support"
- color:[UIColor redColor]
- hasUnderline:NO];
-
- // 第三次调用:设置Help样式(绿色下划线)- 不会覆盖之前的样式
- [MOBaseLevelView setStyledTextForLabel:multiStyleLabel
- fullText:multiText
- targetText:@"Help"
- color:[UIColor greenColor]
- hasUnderline:YES];
-
- [self.view addSubview:multiStyleLabel];
-
- // 示例2:动态修改样式
- UILabel *dynamicLabel = [[UILabel alloc] init];
- dynamicLabel.numberOfLines = 0;
- dynamicLabel.frame = CGRectMake(20, 200, self.view.frame.size.width - 40, 60);
-
- NSString *dynamicText = @"Click Terms or Privacy to learn more";
-
- // 初始设置
- [MOBaseLevelView setStyledTextForLabel:dynamicLabel
- fullText:dynamicText
- targetText:@"Terms"
- color:[UIColor blueColor]
- hasUnderline:YES];
-
- [MOBaseLevelView setStyledTextForLabel:dynamicLabel
- fullText:dynamicText
- targetText:@"Privacy"
- color:[UIColor blueColor]
- hasUnderline:YES];
-
- [self.view addSubview:dynamicLabel];
-
- // 添加按钮来动态修改样式
- UIButton *changeStyleButton = [UIButton buttonWithType:UIButtonTypeSystem];
- changeStyleButton.frame = CGRectMake(20, 280, 200, 40);
- [changeStyleButton setTitle:@"修改Privacy样式" forState:UIControlStateNormal];
- [changeStyleButton addTarget:self action:@selector(changePrivacyStyle:) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:changeStyleButton];
-
- // 存储label引用以便后续修改
- objc_setAssociatedObject(changeStyleButton, @"targetLabel", dynamicLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
-
- // 示例3:错误处理演示
- UILabel *errorTestLabel = [[UILabel alloc] init];
- errorTestLabel.numberOfLines = 0;
- errorTestLabel.frame = CGRectMake(20, 340, self.view.frame.size.width - 40, 60);
-
- NSString *originalText = @"Original text with Keyword";
- NSString *newText = @"Different text with NewKeyword";
-
- // 设置原始文本样式
- [MOBaseLevelView setStyledTextForLabel:errorTestLabel
- fullText:originalText
- targetText:@"Keyword"
- color:[UIColor purpleColor]
- hasUnderline:YES];
-
- // 尝试在不同文本上设置样式(会重新创建富文本)
- [MOBaseLevelView setStyledTextForLabel:errorTestLabel
- fullText:newText
- targetText:@"NewKeyword"
- color:[UIColor orangeColor]
- hasUnderline:NO];
-
- [self.view addSubview:errorTestLabel];
-
- // 示例4:使用便利方法一次性设置多个样式
- UILabel *convenienceLabel = [[UILabel alloc] init];
- convenienceLabel.numberOfLines = 0;
- convenienceLabel.frame = CGRectMake(20, 420, self.view.frame.size.width - 40, 60);
-
- NSString *convenienceText = @"Visit our Website, read FAQ, or contact Support team";
-
- // 使用便利方法一次性设置多个样式
- NSArray *styleConfigs = @[
- @{
- @"targetText": @"Website",
- @"color": [UIColor blueColor],
- @"hasUnderline": @YES
- },
- @{
- @"targetText": @"FAQ",
- @"color": [UIColor purpleColor],
- @"hasUnderline": @NO
- },
- @{
- @"targetText": @"Support",
- @"color": [UIColor redColor],
- @"hasUnderline": @YES
- }
- ];
-
- [MOBaseLevelView setMultipleStyledTextForLabel:convenienceLabel
- fullText:convenienceText
- styleConfigs:styleConfigs];
-
- [self.view addSubview:convenienceLabel];
-
- // 添加说明标签
- UILabel *descriptionLabel = [[UILabel alloc] init];
- descriptionLabel.numberOfLines = 0;
- descriptionLabel.frame = CGRectMake(20, 500, self.view.frame.size.width - 40, 120);
- descriptionLabel.font = [UIFont systemFontOfSize:12.0];
- descriptionLabel.textColor = [UIColor grayColor];
- descriptionLabel.text = @"说明:\n1. 第一个标签:多次调用不会相互覆盖\n2. 第二个标签:可以动态修改样式\n3. 第三个标签:文本内容变化时的处理\n4. 第四个标签:便利方法一次性设置多个样式";
- [self.view addSubview:descriptionLabel];
- }
- #pragma mark - Action Methods
- - (void)changePrivacyStyle:(UIButton *)sender {
- UILabel *targetLabel = objc_getAssociatedObject(sender, @"targetLabel");
- if (targetLabel) {
- // 修改Privacy的样式为红色无下划线
- [MOBaseLevelView setStyledTextForLabel:targetLabel
- fullText:@"Click Terms or Privacy to learn more"
- targetText:@"Privacy"
- color:[UIColor redColor]
- hasUnderline:NO];
-
- [sender setTitle:@"样式已修改" forState:UIControlStateNormal];
- sender.enabled = NO;
- }
- }
- @end
|