【问题标题】:How to make a single UILabel with multiple colors如何制作具有多种颜色的单个 UILabel
【发布时间】:2014-07-11 08:26:15
【问题描述】:

我想创建一个具有 2 种颜色的 uilabel。第一种颜色是黑色,另一种是蓝色。

我可以在其中使用多个 uilabel,但我只想有一个 uilabel。有什么办法可以实现吗?

这应该是输出。

这是我的代码:

UILabel * lblPostContent = [[UILabel alloc] initWithFrame:CGRectMake((ICON_PADDING*1.5), 42, container.frame.size.width-30, 34)];
lblPostContent.numberOfLines =0;
[lblPostContent setFont:[UIFont systemFontOfSize:11]];
[lblPostContent setText:[NSString stringWithFormat:@"I just scored %d points at %@ using the iBowl app", score, LuckyStrikes]];
[container addSubview:lblPostContent];

【问题讨论】:

  • 查看NSMutableAttributedString 的文档

标签: ios uilabel nsattributedstring uicolor textcolor


【解决方案1】:

你应该使用属性......像这样

NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:yourString];
[text addAttribute: NSForegroundColorAttributeName value: [UIColor blackColor] range: NSMakeRange(0, TXTTOBEBLACKLENGTH)];
[text addAttribute: NSForegroundColorAttributeName value: [UIColor blueColor] range: NSMakeRange(TXTTOBEBLACKLENGTH, TXTTOBEBLBLUELENGTH)];
[lblPostContent setAttributedText: text];

【讨论】:

    【解决方案2】:

    为避免计算部分文本的长度并生成NSRanges,您可以使用appendAttributedString: 从组件NSAttributedString 对象组成最终的NSMutableAttributedString,如下所示:

    UIColor *normalColor = [UIColor blackColor];
    UIColor *highlightColor = [UIColor blueColor];
    UIFont *font = [UIFont systemFontOfSize:12.0];        
    NSDictionary *normalAttributes = @{NSFontAttributeName:font, NSForegroundColorAttributeName:normalColor};
    NSDictionary *highlightAttributes = @{NSFontAttributeName:font, NSForegroundColorAttributeName:highlightColor};
    
    NSAttributedString *normalText = [[NSAttributedString alloc] initWithString:@"Normal " attributes:normalAttributes];
    NSAttributedString *highlightedText = [[NSAttributedString alloc] initWithString:@"Highlighted" attributes:highlightAttributes];
    
    NSMutableAttributedString *finalAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:normalText];
    [finalAttributedString appendAttributedString:highlightedText];
    
    self.label.attributedText = finalAttributedString;
    

    【讨论】:

      【解决方案3】:

      这里是 AttributedString 的扩展函数:

      extension NSMutableAttributedString {
      
          func setColorForText(textToFind: String, withColor color: UIColor) {
              let range: NSRange = self.mutableString.range(of: textToFind, options: .caseInsensitive)
              self.addAttribute(NSAttributedStringKey.foregroundColor, value: color, range: range)
          }
      
      }
      

      使用标签试试:

      let label = UILabel()
      label.frame = CGRect(x: 40, y: 100, width: 280, height: 200)
      let first = "89"
      let second = "Lucky Strikes"
      let third = "iBowl app"
      let stringValue = "I just scored \(first) points at \(second) using the \(third)"  // or direct assign single string value like "firstsecondthird"
      label.textColor = UIColor.lightGray
      label.numberOfLines = 0
      let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: stringValue)
      attributedString.setColorForText(textToFind: first, withColor: UIColor.blue)   // use variable for string "89"
      attributedString.setColorForText(textToFind: second, withColor: UIColor.blue) // or direct string like this "Lucky Strikes"
      attributedString.setColorForText(textToFind: third, withColor: UIColor.blue)
      label.font = UIFont.systemFont(ofSize: 26)
      label.attributedText = attributedString
      self.view.addSubview(label)
      

      结果如下:

      【讨论】:

        猜你喜欢
        • 2014-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多