此代码位于UIView
对象的子类的初始化中。 UIView
的形状为圆形,并使用嵌套的UILabel
显示percentChange值。以下代码调整了UILabel
的大小和字体大小以适合UIView
中的文本。 percentChange是一些Int。
let stringAttrs: NSMutableDictionary = [NSFontAttributeName: stringFont, NSForegroundColorAttributeName: self.GreenColor]
if percentChange > 0 {
self.layer.borderColor = self.GreenColor.CGColor
stringAttrs.setValue(self.GreenColor, forKey: NSForegroundColorAttributeName)
indicator = ""
}
else if percentChange < 0 {
self.layer.borderColor = UIColor.redColor().CGColor
stringAttrs.setValue(UIColor.redColor().CGColor, forKey: NSForegroundColorAttributeName)
indicator = "▼"
}
else {
self.layer.borderColor = UIColor.grayColor().CGColor
stringAttrs.setValue(UIColor.grayColor().CGColor, forKey: NSForegroundColorAttributeName)
indicator = ""
}
do {
fontPointSize -= 0.5
stringFont = UIFont(name: "AmericanTypewriter", size: fontPointSize)!
stringAttrs.setValue(stringFont, forKey: NSFontAttributeName)
percentChangeLabel.attributedText = NSAttributedString(string: "\(indicator)\(abs(Int(percentChange)))%", attributes: stringAttrs)
percentChangeLabel.sizeToFit()
} while percentChangeLabel.frame.width > (self.frame.width - PaddingLeft - PaddingRight)
三个条件设置指示符为“▲”(percentChange> 0),“▼”(percentChange&lt; 0)或“”(percentChange == 0)。这是奇怪的事情,只有当percentChange为0且指示符!=“▲”或!=“▼”时,应用程序才会崩溃。如果我在else语句中将指示符更改为“▼”或“▲”,则应用程序正常工作。到目前为止我尝试的每个其他字符/字符串都会导致同样的崩溃。将指示器设置为“”以进行百分比变换&gt; 0或percentChange&lt; 0也不会造成任何问题。我附上了崩溃日志,并希望有人能帮助我弄清楚发生了什么,或者可能引导我朝着正确的方向前进。
2014-11-04 22:09:48.208 Cassandra[796:11817] -[__NSCFType set]: unrecognized selector sent to instance 0x7ff951b475f0
2014-11-04 22:09:48.210 Cassandra[796:11817] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType set]: unrecognized selector sent to instance 0x7ff951b475f0'
*** First throw call stack:
(
0 CoreFoundation 0x000000010937af35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010b36fbb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010938204d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00000001092da27c ___forwarding___ + 988
4 CoreFoundation 0x00000001092d9e18 _CF_forwarding_prep_0 + 120
5 UIFoundation 0x000000010f42dcf3 __NSStringDrawingEngine + 28766
6 UIFoundation 0x000000010f42e1ec -[NSAttributedString(NSExtendedStringDrawing) drawWithRect:options:context:] + 561
7 UIKit 0x000000010a282a4c -[UILabel _drawTextInRect:baselineCalculationOnly:] + 5549
8 UIKit 0x000000010a280cab -[UILabel drawTextInRect:] + 624
9 UIKit 0x000000010a282c60 -[UILabel drawRect:] + 100
10 UIKit 0x000000010a130569 -[UIView(CALayerDelegate) drawLayer:inContext:] + 496
11 QuartzCore 0x0000000109f42942 -[CALayer drawInContext:] + 113
12 QuartzCore 0x0000000109e3fd5b CABackingStoreUpdate_ + 2793
13 QuartzCore 0x0000000109f42851 ___ZN2CA5Layer8display_Ev_block_invoke + 59
14 QuartzCore 0x0000000109f426e9 _ZN2CA5Layer8display_Ev + 1469
15 QuartzCore 0x0000000109f37801 _ZN2CA5Layer17display_if_neededEPNS_11TransactionE + 301
16 QuartzCore 0x0000000109f37889 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 35
17 QuartzCore 0x0000000109ea563e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
18 QuartzCore 0x0000000109ea674a _ZN2CA11Transaction6commitEv + 390
19 QuartzCore 0x0000000109ea6db5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
20 CoreFoundation 0x00000001092afdc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
21 CoreFoundation 0x00000001092afd20 __CFRunLoopDoObservers + 368
22 CoreFoundation 0x00000001092a5b53 __CFRunLoopRun + 1123
23 CoreFoundation 0x00000001092a5486 CFRunLoopRunSpecific + 470
24 GraphicsServices 0x000000010e05e9f0 GSEventRunModal + 161
25 UIKit 0x000000010a0b7420 UIApplicationMain + 1282
26 Cassandra 0x0000000108da8ade top_level_code + 78
27 Cassandra 0x0000000108da8b1a main + 42
28 libdyld.dylib 0x000000010bb49145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
更新:上面的UIView
嵌套在UITableCellView
的子类中,该子类正被添加到UITableView
。似乎在最后UITableViewCell
添加后,崩溃一直发生。
答案 0 :(得分:0)
我不使用此功能:stringAttrs.setValue(stringFont, forKey: NSFontAttributeName)
不要像你那样设置字体。看看这个例子:
let stringFont = [NSFontAttributeName:UIFont(name: "AmericanTypewriter", size: 18.0)]
let stringAttrs= NSAttributedString(string: "Hello Swift! This is a tutorial looking at ", attributes:textFont)
请查看此链接以了解用法:http://sketchytech.blogspot.com/2014/08/swift-creating-styled-and-attributed.html
答案 1 :(得分:0)
我仍然不知道什么是错的,但我通过使用UILabel属性来设置text,font和textColor而不是使用attributedText字符串来规避问题。