行计数功能不再适用于Swift 3.0

时间:2016-10-14 16:55:56

标签: uilabel swift3 ios10 xcode8

我有一个我在Swift 2.2中使用的功能,我从来没有遇到任何问题,现在我在尝试使用它时遇到错误。

代码是改变UILabel必须容纳字符串大小的行数。

func countLabelLines(label: UILabel) -> Int {

    if let text = label.text {
        let myText = text as NSString
        let attributes = [NSFontAttributeName: label.font]

        let width = label.bounds.width
        let height = CGFloat.greatestFiniteMagnitude
        let size = CGSize(width: width, height: height)

        let labelSize = myText.boundingRect(with: size, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: attributes, context: nil) //Code errors here
        let lines = ceil(CGFloat(labelSize.height) / label.font.lineHeight)

        return Int(lines)
    }

    return 0
}

这是我的错误信息:

> 2016-10-14 09:47:37.213 EZ Lists[23136:1997134] -[_SwiftValue pointSize]: unrecognized selector sent to instance 0x608000247380
2016-10-14 09:47:37.218 EZ Lists[23136:1997134] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue pointSize]: unrecognized selector sent to instance 0x608000247380'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010e2ef34b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010d93321e objc_exception_throw + 48
    2   CoreFoundation                      0x000000010e35ef34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x000000010e274c15 ___forwarding___ + 1013
    4   CoreFoundation                      0x000000010e274798 _CF_forwarding_prep_0 + 120
    5   UIFoundation                        0x000000011362a2ba __NSStringDrawingEngine + 3204
    6   UIFoundation                        0x0000000113629610 -[NSString(NSExtendedStringDrawing) boundingRectWithSize:options:attributes:context:] + 202
    7   EZ Lists                            0x000000010d2beaf2 _TZFC8EZ_Lists11CustomFuncs15countLabelLinesfT5labelCSo7UILabel_Si + 1218
    8   EZ Lists                            0x000000010d2be0f2 _TZFC8EZ_Lists11CustomFuncs21fitLabelTextForHeightfT5labelCSo7UILabel10constraintCSo18NSLayoutConstraint11extraHeightV12CoreGraphics7CGFloat_T_ + 82
    9   EZ Lists                            0x000000010d2ce445 _TFC8EZ_Lists9AddItemVC21viewDidLayoutSubviewsfT_T_ + 309
    10  EZ Lists                            0x000000010d2ce472 _TToFC8EZ_Lists9AddItemVC21viewDidLayoutSubviewsfT_T_ + 34
    11  UIKit                               0x000000010eee73a8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1337
    12  QuartzCore                          0x000000011478dcdc -[CALayer layoutSublayers] + 146
    13  QuartzCore                          0x00000001147817a0 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
    14  QuartzCore                          0x000000011478161e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    15  QuartzCore                          0x000000011470f62c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280
    16  QuartzCore                          0x000000011473c713 _ZN2CA11Transaction6commitEv + 475
    17  UIKit                               0x000000010ee1c067 _UIApplicationFlushRunLoopCATransactionIfTooLate + 206
    18  UIKit                               0x000000010f62bb30 __handleEventQueue + 5672
    19  CoreFoundation                      0x000000010e294311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    20  CoreFoundation                      0x000000010e27959c __CFRunLoopDoSources0 + 556
    21  CoreFoundation                      0x000000010e278a86 __CFRunLoopRun + 918
    22  CoreFoundation                      0x000000010e278494 CFRunLoopRunSpecific + 420
    23  GraphicsServices                    0x0000000113f9da6f GSEventRunModal + 161
    24  UIKit                               0x000000010ee22f34 UIApplicationMain + 159
    25  EZ Lists                            0x000000010d2caa4f main + 111
    26  libdyld.dylib                       0x00000001120ce68d start + 1
    27  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我在Xcode / Swift仍然很新,我明白我的一个论点是对的,但我不明白哪个是问题。在我第一次想到它与CGSize之后,但我把它分解成了它自己的路线并且没有问题。

1 个答案:

答案 0 :(得分:1)

它是字体。问题是这一行:

let attributes = [NSFontAttributeName: label.font]

试着写下这个:

let attributes = [NSFontAttributeName: label.font!]

我认为感叹号应该修复它。