简体   繁体   English

[__NSCFType set]:使用 NSAttributedString:draw() 的无法识别的选择器被调用,基于 position 的宽度

[英][__NSCFType set]: unrecognized selector with NSAttributedString:draw() gets called, based on width of position

If the code below is run it causes an exception如果运行下面的代码,它会导致异常

'NSInvalidArgumentException', reason: '-[__NSCFType set]: unrecognized selector sent to instance 0x283130be0' 'NSInvalidArgumentException',原因:'-[__NSCFType set]:无法识别的选择器发送到实例 0x283130be0'

However, the exception can be removed by reducing the width value from 100.0 in positionRect , for example setting it to something like 50.0 removes the exception.但是,可以通过将positionRect中的宽度值从 100.0 减小来移除异常,例如将其设置为 50.0 之类的值可以移除异常。

But why is this?但这是为什么呢? With a width value of 100.0 specified for positionRect , that's well short of the width of imageSize .positionRect指定的宽度值为 100.0,这远远imageSize的宽度。 And even if there is some size issue, why is there an unrecognized selector exception?即使存在一些大小问题,为什么会出现无法识别的选择器异常?

let imageSize = CGSize(width: 400, height: 100)
let testRenderer = UIGraphicsImageRenderer(size: imageSize)
let testImage = testRenderer.image { context in
    context.cgContext.setFillColor(UIColor.black.cgColor)
    context.fill(CGRect(origin: .zero, size: imageSize))
    
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = .left
    let attributes: [NSAttributedString.Key: Any] = [
        .font: UIFont.systemFont(ofSize: 8.0),
        .paragraphStyle: paragraphStyle,
        .foregroundColor: UIColor.white.cgColor
    ]
    
    let attributedString = NSAttributedString(string: "This is some text", attributes: attributes)
    let positionRect = CGRect(x: 10, y: 10, width: 100.0, height: 8.0)
    attributedString.draw(with: positionRect, options: .usesLineFragmentOrigin, context: nil)
}

The issue is due to passing a CGColor instead of a UIColor for the .foregroundColor key in your attributes dictionary.问题是由于为attributes字典中的.foregroundColor键传递了CGColor而不是UIColor The documentation states: 文件指出:

In macOS, the value of this attribute is an NSColor instance.在 macOS 中,这个属性的值是一个 NSColor 实例。 In iOS, tvOS, watchOS, and Mac Catalyst, the value of this attribute is a UIColor instance.在 iOS、tvOS、watchOS 和 Mac Catalyst 中,该属性的值是一个 UIColor 实例。 Use this attribute to specify the color of the text during rendering.使用此属性指定呈现期间文本的颜色。 If you don't specify this attribute, the text renders in black.如果您不指定此属性,则文本呈现为黑色。

The NSAttributedString code is attempting to call set on the provided color as indicated by the error. NSAttributedString代码正尝试调用set错误指示的提供的颜色。 This is indicated in the error message showing the Objective-C syntax -[__NSCFType set] .这在显示 Objective-C 语法-[__NSCFType set]的错误消息中指出。 This shows that set is being called on an object of type __NSCFType which is an internal type representing many Core Foundation (and Core Graphics) types, such as CGColor .这表明set正在 __NSCFType 类型的__NSCFType上被调用,它是代表许多 Core Foundation(和 Core Graphics)类型的内部类型,例如CGColor

In short, change the line:简而言之,更改行:

.foregroundColor: UIColor.white.cgColor

to:到:

.foregroundColor: UIColor.white

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (NSCFType set) - iOS 6中无法识别的选择器 - (NSCFType set) - Unrecognized selector in iOS 6 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[__ NSCFType next]:无法识别的选择器已发送至实例' - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType next]: unrecognized selector sent to instance ' 无法识别的选择器设置为实例 - Unrecognized selector set to instance 在 Flutter 插件上调用的无法识别的选择器 - Unrecognized selector called on Flutter plugin NSInvocation:虽然设置了无法识别的选择器 - NSInvocation: unrecognized selector though it is set 从NSAttributedString子类调用父级的initWithAttributedString:方法时出现无法识别的选择器错误 - Unrecognized selector error when calling a super's initWithAttributedString: method from an NSAttributedString subclass CoreData问题-无法识别的选择器设置为实例 - CoreData issue - unrecognized selector set to instance 调用sizeToFit时,ios应用崩溃,无法识别的选择器发送到实例 - ios app crashing with unrecognized selector sent to instance when sizeToFit is called 设置 UIPageViewController.dataSource 时,无法识别的选择器发送到实例? - Unrecognized selector sent to instance when UIPageViewController.dataSource is set? UILabel [__ NSCFString set]:无法识别的选择器发送到实例崩溃 - UILabel [__NSCFString set]: unrecognized selector sent to instance crash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM