如何给uialertview或UIAlertController中的特定单词上色? (迅速) [英] how to color specific words in a uialertview or UIAlertController ? (SWIFT)

查看:101
本文介绍了如何给uialertview或UIAlertController中的特定单词上色? (迅速)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在uialertview中为某些特定的单词着色?
我的意思是,我可以将单词RED涂成红色,同时将GREEN涂成绿色吗?

is there a way to color some specific words in uialertview ? I mean, can I color the word RED as red and at the same time GREEN as green ?

有人可以用正确的方式带我走吗?

Can someone bring me on the right way?

推荐答案

您可以使用 NSAttributedString UIAlertController

    let strings = [["text" : "My String red\n", "color" : UIColor.redColor()], ["text" : "My string green", "color" : UIColor.greenColor()]];

    var attributedString = NSMutableAttributedString()

    for configDict in strings {
        if let color = configDict["color"] as? UIColor, text = configDict["text"] as? String {
            attributedString.appendAttributedString(NSAttributedString(string: text, attributes: [NSForegroundColorAttributeName : color]))
        }
    }

    let alert = UIAlertController(title: "Title", message: "", preferredStyle: .Alert)

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

    let cancelAction = UIAlertAction(title: "Cancel",
        style: .Default) { (action: UIAlertAction!) -> Void in
    }

    presentViewController(alert,
        animated: true,
        completion: nil)

您可以使用键 attributedMessage 为消息分配属性字符串,并使用键 attributedTitle 作为标题。

You use the key attributedMessage to assign an attributed string for the message and the key attributedTitle for the title.

我找不到有效的 UIAlertView 解决方案但是它们都使用私有变量,这不是一个好习惯。您应该知道 UIAlertView 是iOS 8以后的版本,因此,如果您的目标不是较低版本,则不要使用它,而应使用 UIAlertController

I couldn't find a solution for UIAlertView that works but all of them use private variables which is not good practice. You should know that UIAlertView is deprecate from iOS 8 on, so if you are not targeting lower, you should not use it, instead use UIAlertController

这篇关于如何给uialertview或UIAlertController中的特定单词上色? (迅速)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆