【问题标题】:Specific letters that have each specific colors?具有每种特定颜色的特定字母?
【发布时间】:2014-11-19 05:46:06
【问题描述】:

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

例子:

println("This is a string")

看起来像这样:

如果这有帮助,我发现其他语言能够通过 javascript 和 microsoft word 专门用颜色为每个字母着色。我没有线索,任何会有所帮助。谢谢!

示例: https://superuser.com/questions/230665/how-do-i-change-the-font-color-of-a-specific-letter-throughout-the-entire-ms-wor Is there any clean CSS method to make each letter in a word a different color?

【问题讨论】:

标签: xcode swift xcode6 swift-playground generics


【解决方案1】:

假设您正在使用名为myLabelUILabel

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 2013-10-26
    • 2022-07-05
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    相关资源
    最近更新 更多