【问题标题】:Colour of a substring in a string of text view文本视图字符串中子字符串的颜色
【发布时间】:2013-04-19 05:59:32
【问题描述】:

我有这样的文本视图:

    UITextview staticTxt = [[UITextView alloc]initWithFrame:CGRectMake(10, 80, 300, 400)];
        staticTxt.backgroundColor = [UIColor clearColor];
        staticTxt.text = @"this is a textview";
        staticTxt.textColor = [UIColor whiteColor];
[self.view addSubview:staticTxt];

现在我想要的是文本“textview”应该是一些不同的颜色。我怎样才能做到这一点? 谢谢!!

【问题讨论】:

标签: ios objective-c xcode nsstring uitextview


【解决方案1】:

您需要创建一个属性文本字符串,如本答案所述:

Underline text in a UITextView

【讨论】:

    【解决方案2】:

    首先,您必须将字符串分解为子字符串。正如您在评论中所说,““this is a”是白色的,“textview”是红色的”......我将在该参考中向您建议一种方式。

    staticTxt.text = @"this is a textview";
    
    NSString *yourString = staticTxt.text;
    
    NSString *str = [[yourString componentsSeparatedByString:@"a "] lastObject];
    

    现在你可以设置“str”子字符串的颜色了。

    让我很清楚地告诉你一件事。它只是改变静态字符串的子字符串颜色的方法。如果你要动态更改 textview 的文本……那么这种方法不是一个好的选择。 .

    【讨论】:

      【解决方案3】:

      您可以通过使用此类方法在控件(UILabel、UITextField 或 UItextView)中使用不同颜色、不同字体和不同大小的文本

      + (void)setMultiColorAndFontText:(NSString *)text rangeString:(NSArray *)rangeString inputView:(UITextView*) inputView font:(NSArray*) fontName color:(NSArray*) colorName
      {
          inputView.layer.sublayers = nil;
      
          NSMutableAttributedString *mutableAttributedString = [[ NSMutableAttributedString alloc]initWithString:text];
      
          for (int i =0 ; i<[rangeString count]; i++)
          {
              CTFontRef  ctFont = CTFontCreateWithName((__bridge CFStringRef) [UIFont fontWithName:[fontName objectAtIndex:i] size:14.0].fontName, [UIFont fontWithName:[fontName objectAtIndex:i] size:14.0].pointSize, NULL);
      
              NSRange whiteRange = [text rangeOfString:[rangeString objectAtIndex:i]];
      
              if (whiteRange.location != NSNotFound)
              {
                  [mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[colorName objectAtIndex:i] range:whiteRange];
                  [mutableAttributedString addAttribute:(NSString*)kCTFontAttributeName
                                                  value:( __bridge id)ctFont
                                                  range:whiteRange];
              }
          }
      
          CGSize expectedinputViewSize = [text sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14.0f] constrainedToSize:CGSizeMake(186,100) lineBreakMode:UILineBreakModeWordWrap];
      
          CATextLayer   *textLayer = [[CATextLayer alloc]init];
          textLayer.frame =CGRectMake(0,4, inputView.frame.size.width,expectedinputViewSize.height+4);
          textLayer.contentsScale = [[UIScreen mainScreen] scale];
          textLayer.string=mutableAttributedString;
          textLayer.opacity = 1.0;
          textLayer.alignmentMode = kCAAlignmentLeft;
          [inputView.layer addSublayer:textLayer];
          [textLayer setWrapped:TRUE];
      
          [inputView setText:@""];
      }
      

      在这里你可以传递你的文本、它的范围、字体和颜色作为参数。 示例:

      [CommonFunctions setMultiColorAndFontText:@"This is a textview" rangeString:[NSArray arrayWithObjects:@"This is a",@"textview", nil] textfield:txtEmailAddress font:[NSArray arrayWithObjects:@"Arial",@"Helvetica",nil] color:[NSArray  arrayWithObjects:(UIColor *)[UIColor whiteColor].CGColor,(UIColor *)[UIColor redColor].CGColor,nil]];
      

      它将显示不同颜色和不同字体的文本。

      注意:在这个方法中我们使用了 UITextView 。如果你需要 UILabel 或 UITextField 那么你只需要以同样的方式改变它:

      UILabel

      + (void)setMultiColorAndFontText:(NSString *)text rangeString:(NSArray *)rangeString inputView:(UILabel*) label font:(NSArray*) fontName color:(NSArray*) colorName;
      

      UITextField

      + (void)setMultiColorAndFontText:(NSString *)text rangeString:(NSArray *)rangeString inputView:(UITextField*) label font:(NSArray*) fontName color:(NSArray*) colorName;
      

      UITextView

      + (void)setMultiColorAndFontText:(NSString *)text rangeString:(NSArray *)rangeString inputView:(UITextView*) label font:(NSArray*) fontName color:(NSArray*) colorName;
      

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 2014-02-02
        • 1970-01-01
        • 2014-07-22
        • 2017-10-30
        • 2012-01-20
        • 2012-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多