Xcode 每个字母都有特定的颜色?

Xcode 每个字母都有特定的颜色?,xcode,swift,xcode6,swift-playground,generics,Xcode,Swift,Xcode6,Swift Playground,Generics,我想知道是否有一种方法可以使用Swift编程语言使每个单词中的字母都着色。我正在寻找想法和线索 例如: println("This is a string") 看起来是这样的: 为了以防万一,我发现其他语言可以通过javascript和microsoft word专门为每个字母着色。我没有线索了,任何人都会有帮助的。谢谢 示例: 假设您正在使用名为myLabel的UILabel: let str = "This is a string" let colors:[UIColor] = [

我想知道是否有一种方法可以使用Swift编程语言使每个单词中的字母都着色。我正在寻找想法和线索

例如:

println("This is a string")
看起来是这样的:

为了以防万一,我发现其他语言可以通过javascript和microsoft word专门为每个字母着色。我没有线索了,任何人都会有帮助的。谢谢

示例:

假设您正在使用名为
myLabel的
UILabel

let str = "This is a string"
let colors:[UIColor] = [
    .blackColor(),     // T
    .darkGrayColor(),  // h
    .lightGrayColor(), // i
    .grayColor(),      // s

    .clearColor(),     //

    .redColor(),       // i
    .greenColor(),     // s

    .clearColor(),     //

    .blueColor(),      // a

    .clearColor(),     //

    .cyanColor(),      // S
    .yellowColor(),    // t
    .magentaColor(),   // r
    .orangeColor(),    // i
    .purpleColor(),    // n
    .brownColor(),     // g
]

let attrStr = NSMutableAttributedString()
for (chr, color) in Zip2(str, colors) {
    attrStr.appendAttributedString(
        NSAttributedString(string: String(chr), attributes: [NSForegroundColorAttributeName:color])
    )
}

myLabel.attributedText = attrStr

没问题,只需使用NSAttributed字符串并为每个字符指定不同的颜色。请参见我的答案并注意图示:有些字母有不同的颜色。你的只是一个极端的例子!:)