【问题标题】:how to change the color of a particular character/word in objective c如何在目标 c 中更改特定字符/单词的颜色
【发布时间】:2015-06-18 10:51:19
【问题描述】:

我有一个长字符串作为一个段落,并想在字符串中为特定的单词(字符串中出现的所有次数)着色。谁能帮我吗? 谢谢

【问题讨论】:

  • 你的字符串是静态的吗?

标签: ios objective-c


【解决方案1】:

将字符串转换为属性字符串(可变),然后搜索字符串以查找要更改/突出显示的部分的范围,并将属性添加到属性字符串的这些范围。

【讨论】:

    【解决方案2】:

    你可以使用 https://github.com/kovpas/BOString

    那么你的代码可以是这样的:

    NSString *string = @"String test String";
    NSAttributedString *result = [string bos_makeString:^(BOStringMaker *make) {
            make.foregroundColor(UIColor blackColor]);
            make.each.substring(@"String", ^{          
                   make.foregroundColor(UIColor greenColor]);
            });
        }];
    

    这将使文本中的每个“字符串”变为绿色。其余的都是黑色的。然后将此属性字符串放入您的 UILabel\UITextView 等。

    【讨论】:

      【解决方案3】:

      试试这个会成功的

      注意:我们不能“在一个标签或文本字段中显示不同的颜色”。因此请使用“等于一个单词中的字母数”的标签。并排查看为一个标签。

      在 Viewcontroller.h 中

      @property (strong, nonatomic) IBOutlet UITextField *tf1;
      @property (strong, nonatomic) IBOutlet UITextField *tf2;
      @property (strong, nonatomic) IBOutlet UITextField *tf3;
      @property (strong, nonatomic) IBOutlet UITextField *tf4;
      @property (strong, nonatomic) IBOutlet UITextField *tf5;
      

      在 ViewController.m 中

      - (void)viewDidLoad {
          [super viewDidLoad];
      
          NSMutableString *strName=[[NSMutableString alloc]initWithFormat:@"ABCDE"];
          for (int i=0; i<[strName length]; i++) {
              NSString *str=[strName substringWithRange:NSMakeRange(i, 1)];
      
              if (i==0) {
                  self.tf1.text=str;
                  self.tf1.textColor=[UIColor redColor];
              }
              else if (i==1){
                  self.tf2.text=str;
                  self.tf2.textColor=[UIColor greenColor];
              }
              else if (i==2){
                  self.tf3.text=str;
                  self.tf3.textColor=[UIColor orangeColor];
              }
              else if (i==3){
                  self.tf4.text=str;
                  self.tf4.textColor=[UIColor grayColor];
              }
              else if (i==4){
                  self.tf5.text=str;
                  self.tf5.textColor=[UIColor redColor];
              }
          }
      }
      

      【讨论】:

        【解决方案4】:

        为 UILabel 创建一个 Category 并在其中添加这些函数。

          - (void)boldRange:(NSRange)range {
            if (![self respondsToSelector:@selector(setAttributedText:)]) {
            return;
         }
            NSMutableAttributedString *attributedText;
            if (!self.attributedText) {
            attributedText = [[NSMutableAttributedString alloc] initWithString:self.text];
        } else {
            attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
        }
        
          [attributedText setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:self.font.pointSize] } range:range];
          self.attributedText = attributedText;
        
        }
        
        - (void)boldSubstring:(NSString*)substring {
          NSRange range = [self.text rangeOfString:substring];
          // [self boldRange:range];
          if (![self respondsToSelector:@selector(setAttributedText:)]) {
            return;
          }
          NSMutableAttributedString *attributedText;
          if (!self.attributedText) {
            attributedText = [[NSMutableAttributedString alloc] initWithString:self.text];
          } else {
              attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
          }
        
          while (range.location != NSNotFound)
          {
              [attributedText setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:self.font.pointSize] } range:range];
        
              NSRange rangeToSearch;
              rangeToSearch.location = range.location + range.length;
              rangeToSearch.length = self.text.length - rangeToSearch.location;
              range = [self.text rangeOfString:substring options:0 range:rangeToSearch];
              [attributedText setAttributes:@{NSForegroundColorAttributeName:    [UIColor redColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:self.font.pointSize] } range:range];
              self.attributedText = attributedText;
            }
        
        }
        

        按原样使用

         [someLabel boldSubstring: @"0"];
        

        我已经改变颜色和加粗特定字符。相应地改变它 希望对您有所帮助。

        【讨论】:

          【解决方案5】:

          在界面生成器中,您可以直接将其设置为属性,如图所示,

          您可以在此处选择特定世界并更改其颜色。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-01-02
            • 2011-06-05
            • 1970-01-01
            • 2018-03-01
            • 1970-01-01
            • 2015-10-19
            相关资源
            最近更新 更多