如何改变单个字母或一个单词的颜色?

时间:2021-02-03 21:16:39

How to change color of word in "message" in AlertView

如何在AlertView中改变“message”中的文字颜色

@@IBAction func btn_instructions(sender: UIButton) {
    let alertView = UNAlertView(title: "MyTitle", message: "Here green text, here red text")
 alertView.show()

Sorry if question is not correct.

如果问题不正确,请见谅。

2 个解决方案

#1


1  

As I've written in my comment above, UIAlertView is deprecated, so you'll have to use UIAlertController instead. You can, however, set attributed strings for the message, using (non-swifty) key-value coding (since UIAlertView is a subclass of NSObject): setting an attributed string for key "attributedMessage". The associated key for the title is "attributedTitle".

正如我在上面的注释中所写的,UIAlertView已经被弃用了,所以你必须使用UIAlertController。但是,可以使用(非swifty)键值编码(因为UIAlertView是NSObject的一个子类)为消息设置带属性字符串:为键“attributedMessage”设置带属性字符串。标题的关联键是“attributedTitle”。

Note, however, that these features seems---as far as I can find---undocumented by Apple, referenced only as derived by users via runtime introspection.

但是请注意,在我看来,这些特性并没有得到苹果的正式证明,只有用户通过运行时内省来引用它们。

An example follows below:

一个示例如下所示:

import UIKit

class ViewController: UIViewController {

    // ...

    @IBAction func showAlert(sender: UIButton) {

        let alertController = UIAlertController(title: "Foo", message: "", preferredStyle: UIAlertControllerStyle.Alert)

        /* attributed string for alertController message */
        let attributedString = NSMutableAttributedString(string: "Bar Bar Bar!")

        attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(),
            range: NSRange(location:0,length:3))
        attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.greenColor(),
            range: NSRange(location:4,length:3))
        attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(),
            range: NSRange(location:8,length:3))

        alertController.setValue(attributedString, forKey: "attributedMessage")

        /* action: OK */
        alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        self.presentViewController(alertController, animated: true, completion: nil)
    }
}

Producing the following result:

产生以下结果:

如何改变单个字母或一个单词的颜色?

#2


0  

This is not possible with UIAlertController (UIAlertView is deprecated). UIAlertController accepts an Optional String for the title and message parameters (see Apple's Documentation on UIAlertController).

使用UIAlertController(不赞成使用UIAlertView)是不可能的。UIAlertController接受标题和消息参数的可选字符串(参见苹果关于UIAlertController的文档)。

In order to use multiple colors in a line of text, you'll need to use NSAttributedString (here's a good NSAttributedString Tutorial).

为了在一行文本中使用多种颜色,您需要使用NSAttributedString(这里有一个很好的NSAttributedString教程)。

Since String can't hold any attributes, you won't be able to use NSAttributedString on the UIAlertController. You will have to create your own modal ViewController with a NSAttributedString label in order to display what you are asking for.

因为String不能保存任何属性,所以您不能在UIAlertController上使用NSAttributedString。您将必须使用NSAttributedString标签创建自己的模态视图控制器,以便显示您所要求的内容。

#1


1  

As I've written in my comment above, UIAlertView is deprecated, so you'll have to use UIAlertController instead. You can, however, set attributed strings for the message, using (non-swifty) key-value coding (since UIAlertView is a subclass of NSObject): setting an attributed string for key "attributedMessage". The associated key for the title is "attributedTitle".

正如我在上面的注释中所写的,UIAlertView已经被弃用了,所以你必须使用UIAlertController。但是,可以使用(非swifty)键值编码(因为UIAlertView是NSObject的一个子类)为消息设置带属性字符串:为键“attributedMessage”设置带属性字符串。标题的关联键是“attributedTitle”。

Note, however, that these features seems---as far as I can find---undocumented by Apple, referenced only as derived by users via runtime introspection.

但是请注意,在我看来,这些特性并没有得到苹果的正式证明,只有用户通过运行时内省来引用它们。

An example follows below:

一个示例如下所示:

import UIKit

class ViewController: UIViewController {

    // ...

    @IBAction func showAlert(sender: UIButton) {

        let alertController = UIAlertController(title: "Foo", message: "", preferredStyle: UIAlertControllerStyle.Alert)

        /* attributed string for alertController message */
        let attributedString = NSMutableAttributedString(string: "Bar Bar Bar!")

        attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(),
            range: NSRange(location:0,length:3))
        attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.greenColor(),
            range: NSRange(location:4,length:3))
        attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(),
            range: NSRange(location:8,length:3))

        alertController.setValue(attributedString, forKey: "attributedMessage")

        /* action: OK */
        alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        self.presentViewController(alertController, animated: true, completion: nil)
    }
}

Producing the following result:

产生以下结果:

如何改变单个字母或一个单词的颜色?

#2


0  

This is not possible with UIAlertController (UIAlertView is deprecated). UIAlertController accepts an Optional String for the title and message parameters (see Apple's Documentation on UIAlertController).

使用UIAlertController(不赞成使用UIAlertView)是不可能的。UIAlertController接受标题和消息参数的可选字符串(参见苹果关于UIAlertController的文档)。

In order to use multiple colors in a line of text, you'll need to use NSAttributedString (here's a good NSAttributedString Tutorial).

为了在一行文本中使用多种颜色,您需要使用NSAttributedString(这里有一个很好的NSAttributedString教程)。

Since String can't hold any attributes, you won't be able to use NSAttributedString on the UIAlertController. You will have to create your own modal ViewController with a NSAttributedString label in order to display what you are asking for.

因为String不能保存任何属性,所以您不能在UIAlertController上使用NSAttributedString。您将必须使用NSAttributedString标签创建自己的模态视图控制器,以便显示您所要求的内容。