Swift 3 错误 UILabel:不支持将 CGColor 用于颜色属性或内部属性字符串
Swift 3 Error UILabel: the use of CGColor for color properties or inside attributed strings is not supported
我的应用程序在尝试创建 NSAttributedString
时崩溃了。我收到错误:
'NSInternalInconsistencyException',原因:'UILabel: the use of CGColor for color properties or inside attributed strings is not supported.'
这在升级到 Swift 3、Xcode 8
之前没有发生
这是我的代码:
let encodedData = someHtmlString.data(using: String.Encoding.utf8, allowLossyConversion: true)!
let attributedOptions: [String:Any] = [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue]
do {
let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
}
我无法理解 CGColor
与此有什么关系。
编辑:我像这样使用 attributedString:
let label = UILabel()
label.text = attributedString.string
但是这段代码没有执行。应用程序在 'try' 范围内崩溃。
我传入的字符串 (someHtmlString
) 有:
<ul> <li>Up to 2 children 11 years old and younger stay free when occupying the parent or guardian\'s room, using existing bedding. </li><li>Only registered guests are allowed in the guestrooms. </li> <li>The property has connecting/adjoining rooms, which are subject to availability and can be requested by contacting the property using the number on the booking confirmation. </li><li>Some facilities may have restricted access. Guests can contact the property for details using the contact information on the booking confirmation. </li> </ul>
编辑 2
奇怪的事情正在发生。我注释掉了对这段代码的调用,应用程序仍然因同样的错误而崩溃。所以 "All exceptions" 断点将我指向了错误的位置。
编辑 3 请参阅下面我的解决方案
编辑:
勾选Sample
使用
label.attributedText = attributedString
我的输出是您自己的代码,只需使用我上面的行代替您的 label.text = attributedString.string
。只需将标签的行保持为 0,并提供引导和跟踪约束。
我的代码
let someHtmlString = "<ul> <li>Up to 2 children 11 years old and younger stay free when occupying the parent or guardian\'s room, using existing bedding. </li><li>Only registered guests are allowed in the guestrooms. </li> <li>The property has connecting/adjoining rooms, which are subject to availability and can be requested by contacting the property using the number on the booking confirmation. </li><li>Some facilities may have restricted access. Guests can contact the property for details using the contact information on the booking confirmation. </li> </ul>"
let encodedData = someHtmlString.data(using: String.Encoding.utf8, allowLossyConversion: true)!
let attributedOptions: [String:Any] = [
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue
]
do {
let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
myLabel.attributedText = attributedString
//or use myLabel.text = attributedString.string
} catch {
}
经过一些注释,我发现问题出在这段代码中:
let mutableAttributedStr = NSMutableAttributedString
let textColor = label.textColor
let textRange = NSMakeRange(0, mutableAttributedStr.length)
mutableAttributedStr.addAttributes([NSForegroundColorAttributeName: textColor], range: textRange)
textColor
产生了一个 Optional 类型(有一个值,不是 nil)。
一旦我将 !
添加到下一行,它就起作用了。不知道为什么这突然成为 Swift 3/iOS 10.
中的一个问题
let textColor = label.textColor!
快点!因为我刚刚有错字,所以在颜色属性上添加了 UIFont
,例如:
[NSForegroundColorAttributeName: UIFont]
我的应用程序在尝试创建 NSAttributedString
时崩溃了。我收到错误:
'NSInternalInconsistencyException',原因:'UILabel: the use of CGColor for color properties or inside attributed strings is not supported.'
这在升级到 Swift 3、Xcode 8
之前没有发生这是我的代码:
let encodedData = someHtmlString.data(using: String.Encoding.utf8, allowLossyConversion: true)!
let attributedOptions: [String:Any] = [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue]
do {
let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
}
我无法理解 CGColor
与此有什么关系。
编辑:我像这样使用 attributedString:
let label = UILabel()
label.text = attributedString.string
但是这段代码没有执行。应用程序在 'try' 范围内崩溃。
我传入的字符串 (someHtmlString
) 有:
<ul> <li>Up to 2 children 11 years old and younger stay free when occupying the parent or guardian\'s room, using existing bedding. </li><li>Only registered guests are allowed in the guestrooms. </li> <li>The property has connecting/adjoining rooms, which are subject to availability and can be requested by contacting the property using the number on the booking confirmation. </li><li>Some facilities may have restricted access. Guests can contact the property for details using the contact information on the booking confirmation. </li> </ul>
编辑 2 奇怪的事情正在发生。我注释掉了对这段代码的调用,应用程序仍然因同样的错误而崩溃。所以 "All exceptions" 断点将我指向了错误的位置。
编辑 3 请参阅下面我的解决方案
编辑:
勾选Sample
使用
label.attributedText = attributedString
我的输出是您自己的代码,只需使用我上面的行代替您的 label.text = attributedString.string
。只需将标签的行保持为 0,并提供引导和跟踪约束。
我的代码
let someHtmlString = "<ul> <li>Up to 2 children 11 years old and younger stay free when occupying the parent or guardian\'s room, using existing bedding. </li><li>Only registered guests are allowed in the guestrooms. </li> <li>The property has connecting/adjoining rooms, which are subject to availability and can be requested by contacting the property using the number on the booking confirmation. </li><li>Some facilities may have restricted access. Guests can contact the property for details using the contact information on the booking confirmation. </li> </ul>"
let encodedData = someHtmlString.data(using: String.Encoding.utf8, allowLossyConversion: true)!
let attributedOptions: [String:Any] = [
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue
]
do {
let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
myLabel.attributedText = attributedString
//or use myLabel.text = attributedString.string
} catch {
}
经过一些注释,我发现问题出在这段代码中:
let mutableAttributedStr = NSMutableAttributedString
let textColor = label.textColor
let textRange = NSMakeRange(0, mutableAttributedStr.length)
mutableAttributedStr.addAttributes([NSForegroundColorAttributeName: textColor], range: textRange)
textColor
产生了一个 Optional 类型(有一个值,不是 nil)。
一旦我将 !
添加到下一行,它就起作用了。不知道为什么这突然成为 Swift 3/iOS 10.
let textColor = label.textColor!
快点!因为我刚刚有错字,所以在颜色属性上添加了 UIFont
,例如:
[NSForegroundColorAttributeName: UIFont]