xcode 故事板中带有不同颜色文本的 UILabel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42224389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:47:18  来源:igfitidea点击:

UILabel with text of different color in Storyboard

iosobjective-cxcodeuilabeltextcolor

提问by Forte Zhu

I try to set UILabel with text of different colors programmatically using,

我尝试使用不同颜色的文本以编程方式设置 UILabel,

NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"firstsecondthird"];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(5,6)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(11,5)];
[self.resultLabel setAttributedText:string];

I get what I expected. But, I am interested in doing all these stuff in storyboard,in main.storyboard. Can anyone help me in doing this.

我得到了我的预期。但是,我对在main.storyboard中的 storyboard 中做所有这些事情很感兴趣。任何人都可以帮助我做到这一点。


This is what I want,


这就是我要的,

enter image description here

在此处输入图片说明

回答by iPeter

To achieve that in storyboard, make the label's text attributed, then select the range and change colors accordingly.

要在 中实现这一点storyboard,请对label的文本进行属性化,然后选择范围并相应地更改颜色。

See the image below:

见下图:

enter image description here

在此处输入图片说明

Hope this helps.

希望这可以帮助。

回答by Sergey

You need change text style to attributedthen you can select some characters and change their color. Please look on image

您需要将文本样式更改为属性,然后您可以选择一些字符并更改它们的颜色。请看图片

http://take.ms/kq36u

http://take.ms/kq36u

回答by Krunal

Using storyboard interface:

使用故事板界面:

enter image description here

在此处输入图片说明

with Swift 3 & 4:
Programatically you can set label colors very easily.

使用 Swift 3 & 4
您可以很容易地以编程方式设置标签颜色。

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

}


func setColoredLabel() {
        var string: NSMutableAttributedString = NSMutableAttributedString(string: "My label with red blue and green colored text")
        string.setColorForText(textToFind: "red", withColor: UIColor.red)
        string.setColorForText(textToFind: "blue", withColor: UIColor.blue)
        string.setColorForText(textToFind: "green", withColor: UIColor.green)
        yourLabel.attributedText = string
    }

Result:

结果:

enter image description here

在此处输入图片说明

回答by Elli Scharlin

If I am understanding your question correctly, then in storyboard most of the options have little plus buttons beside them so that you can customize the labels and attributes. Make sure that you are not using plain text but rather attributed text it is the first dropdown choice in the column.

如果我正确理解了您的问题,那么在故事板中,大多数选项旁边都有很少的加号按钮,以便您可以自定义标签和属性。确保您使用的不是纯文本,而是属性文本,它是列中的第一个下拉选项。

Image showing the attributes for different choices

显示不同选择的属性的图像

I hope this helps, let me know if I'm not understanding properly!

我希望这会有所帮助,如果我没有正确理解,请告诉我!

回答by Arun

enter image description hereCheckout @IBInspectable and @IBDesignable. Create a Subclass or Extension for UILabel.

在此处输入图片说明签出@IBInspectable 和@IBDesignable。为 UILabel 创建子类或扩展。

import UIKit

@IBDesignable class AttributedStringLabel: UILabel {

    @IBInspectable open var firstText: String = ""

    @IBInspectable open var firstColor: UIColor?
    @IBInspectable open var secondColor: UIColor?
    @IBInspectable open var thirdColor: UIColor?


    override func awakeFromNib() {
        super.awakeFromNib()

        let attrString :NSMutableAttributedString  = NSAttributedString([![string][1]][1]: firstText) as! NSMutableAttributedString

        attrString.addAttribute(NSForegroundColorAttributeName, value: firstColor! as UIColor, range: NSMakeRange(0, 5))
        attrString.addAttribute(NSForegroundColorAttributeName, value: secondColor! as UIColor, range: NSMakeRange(5, 6))
        attrString.addAttribute(NSForegroundColorAttributeName, value: thirdColor! as UIColor, range: NSMakeRange(11, 15))
        self.attributedText = attrString

    }


}

Below is the screenshot, you can customize code as you required, you can add Range whatever you want as IBInspectable variable.

下面是屏幕截图,您可以根据需要自定义代码,您可以添加任何您想要的 Range 作为 IBInspectable 变量。