在一个标签中使用多种字体颜色

有没有办法在iOS中的单个标签中使用两种甚至三种字体颜色?

如果以“你好,你好吗”为例,“你好”是蓝色的,“你好”是绿色的?

这是可能的,它似乎比创build多个标签更容易?

有关更多信息, 请参阅我的博客文章

首先初始化你的NSStringNSMutableAttributedString ,如下所示。

var myString:NSString = "I AM KIRIT MODI" var myMutableString = NSMutableAttributedString() 

ViewDidLoad中

 override func viewDidLoad() { myMutableString = NSMutableAttributedString(string: myString, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!]) myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:2,length:4)) // set label Attribute labName.attributedText = myMutableString super.viewDidLoad() } 

你输出:

在这里输入图像说明

多种颜色

在行代码的下方添加ViewDidLoad以获取string中的多个颜色。

  myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.greenColor(), range: NSRange(location:10,length:5)) 

多彩色输出

在这里输入图像说明

更新了Swift 4的答案

您可以轻松使用UILabel的属性文字属性中的html来轻松地进行各种文本格式。

  let htmlString = "<font color=\"red\">This is </font> <font color=\"blue\"> some text!</font>" let encodedData = htmlString.data(using: String.Encoding.utf8)! let attributedOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType] do { let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil) label.attributedText = attributedString } catch _ { print("Cannot create attributed String") } 

在这里输入图像说明

更新了Swift 2的答案

 let htmlString = "<font color=\"red\">This is </font> <font color=\"blue\"> some text!</font>" let encodedData = htmlString.dataUsingEncoding(NSUTF8StringEncoding)! let attributedOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType] do { let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil) label.attributedText = attributedString } catch _ { print("Cannot create attributed String") } 

对于@赫姆斯摩拉迪亚

在这里输入图像说明

 let attrs1 = [NSFontAttributeName : UIFont.boldSystemFontOfSize(18), NSForegroundColorAttributeName : UIColor.greenColor()] let attrs2 = [NSFontAttributeName : UIFont.boldSystemFontOfSize(18), NSForegroundColorAttributeName : UIColor.whiteColor()] let attributedString1 = NSMutableAttributedString(string:"Drive", attributes:attrs1) let attributedString2 = NSMutableAttributedString(string:"safe", attributes:attrs2) attributedString1.appendAttributedString(attributedString2) self.lblText.attributedText = attributedString1 

斯威夫特4

  let attrs1 = [NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 18), NSAttributedStringKey.foregroundColor : UIColor.green] let attrs2 = [NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 18), NSAttributedStringKey.foregroundColor : UIColor.white] let attributedString1 = NSMutableAttributedString(string:"Drive", attributes:attrs1) let attributedString2 = NSMutableAttributedString(string:"safe", attributes:attrs2) attributedString1.append(attributedString2) self.lblText.attributedText = attributedString1 

使用rakeshbs的答案在Swift 2中创build一个扩展:

 // StringExtension.swift import UIKit import Foundation extension String { var attributedStringFromHtml: NSAttributedString? { do { return try NSAttributedString(data: self.dataUsingEncoding(NSUTF8StringEncoding)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) } catch _ { print("Cannot create attributed String") } return nil } } 

用法:

 let htmlString = "<font color=\"red\">This is </font> <font color=\"blue\"> some text!</font>" label.attributedText = htmlString.attributedStringFromHtml 

甚至是单线

 label.attributedText = "<font color=\"red\">This is </font> <font color=\"blue\"> some text!</font>".attributedStringFromHtml 

关于扩展的好处是,您将在整个应用程序中为所有String提供.attributedStringFromHtml属性。

使用NSMutableAttributedString

 myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:2,length:4)) 

在这里输入图像说明

在这里看到更多的细节swift使用归因string

Swift 3.0

 let myMutableString = NSMutableAttributedString( string: "your desired text", attributes: [:]) myMutableString.addAttribute( NSForegroundColorAttributeName, value: UIColor.blue, range: NSRange( location:6, length:7)) 

结果:

对于更多的颜色,你可以不断添加属性的可变string。 更多例子在这里 。

我喜欢这样

 let yourAttributes = [NSForegroundColorAttributeName: UIColor.black, NSFontAttributeName: UIFont.systemFontOfSize(15)] let yourOtherAttributes = [NSForegroundColorAttributeName: UIColor.red, NSFontAttributeName: UIFont.systemFontOfSize(25)] let partOne = NSMutableAttributedString(string: "This is an example ", attributes: yourAttributes) let partTwo = NSMutableAttributedString(string: "for the combination of Attributed String!", attributes: yourOtherAttributes) let combination = NSMutableAttributedString() combination.appendAttributedString(partOne) combination.appendAttributedString(partTwo) 

斯威夫特4

通过使用以下扩展function,可以直接将颜色属性设置为属性string,并将其应用于标签。

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

尝试以上的扩展名,使用标签:

 let label = UILabel() label.frame = CGRect(x: 60, y: 100, width: 260, height: 50) let stringValue = "stackoverflow" let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: stringValue) attributedString.setColorForText(textForAttribute: "stack", withColor: UIColor.black) attributedString.setColorForText(textForAttribute: "over", withColor: UIColor.orange) attributedString.setColorForText(textForAttribute: "flow", withColor: UIColor.red) label.font = UIFont.boldSystemFont(ofSize: 40) label.attributedText = attributedString self.view.addSubview(label) 

结果:

在这里输入图像说明

Swift 3使用HTML版本的例子。

 let encodedData = htmlString.data(using: String.Encoding.utf8)! let attributedOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType] do { let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil) label.attributedText = attributedString } catch _ { print("Cannot create attributed String") } 

这里是支持最新版本的Swift截至2017年3月的代码。

Swift 3.0

在这里我创build了一个Helper类和方法

 public class Helper { static func GetAttributedText(inputText:String, location:Int,length:Int) -> NSMutableAttributedString { let attributedText = NSMutableAttributedString(string: inputText, attributes: [NSFontAttributeName:UIFont(name: "Merriweather", size: 15.0)!]) attributedText.addAttribute(NSForegroundColorAttributeName, value: UIColor(red: 0.401107, green: 0.352791, blue: 0.503067, alpha: 1.0) , range: NSRange(location:location,length:length)) return attributedText } } 

在方法参数中,inputText:String – 要在标签位置显示的文本:Int – 样式应该是应用程序,“0”作为string的开始或某个有效值作为string的字符位置:Int – 从直到这个风格适用多less个字符的位置。

以其他方式消费:

 self.dateLabel?.attributedText = Helper.GetAttributedText(inputText: "Date : " + (self.myModel?.eventDate)!, location:0, length: 6) 

输出:

在这里输入图像说明

注意:UI颜色可以被定义为UIColor.red颜色或用户定义的颜色,如UIColor(red: 0.401107, green: 0.352791, blue: 0.503067, alpha: 1.0)

 func MultiStringColor(first:String,second:String) -> NSAttributedString { let MyString1 = [NSFontAttributeName : FontSet.MonsRegular(size: 14), NSForegroundColorAttributeName : FoodConstant.PUREBLACK] let MyString2 = [NSFontAttributeName : FontSet.MonsRegular(size: 14), NSForegroundColorAttributeName : FoodConstant.GREENCOLOR] let attributedString1 = NSMutableAttributedString(string:first, attributes:MyString1) let attributedString2 = NSMutableAttributedString(string:second, attributes:MyString2) MyString1.append(MyString2) return MyString1 } 

SWIFT 3

在我的代码中,我创build了一个扩展

 import UIKit import Foundation extension UILabel { func setDifferentColor(string: String, location: Int, length: Int){ let attText = NSMutableAttributedString(string: string) attText.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueApp, range: NSRange(location:5,length:4)) attributedText = attText } } 

和这个使用

 override func viewDidLoad() { super.viewDidLoad() titleLabel.setDifferentColor(string: titleLabel.text!, location: 5, length: 4) }