将属性字符串设置为iOS中的UILabel

时间:2015-09-03 12:12:38

标签: ios objective-c ios7

使用此代码创建contactAttributes时,应用程序崩溃了:

NSDictionary *contactAttributes =@{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),
                                   NSFontAttributeName :[UIFont fontWithName:@"Helvetica-Neue" size:17.0],
                                   NSForegroundColorAttributeName : [UIColor whiteColor]
                                   };

NSAttributedString * adminPhone  = [[NSAttributedString alloc] initWithString:[currentStaffMember phone] attributes:contactAttributes];
NSAttributedString * adminEmail  = [[NSAttributedString alloc] initWithString:[currentStaffMember email] attributes:contactAttributes];
[customCell.lblAdminContact setAttributedText:adminPhone];
[customCell.lblAdminEmail setAttributedText:adminEmail];

这是日志:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException',     reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]'
*** First throw call stack:
(
0   CoreFoundation                      0x0000000113085c65 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x000000011295ebb7 objc_exception_throw + 45
2   CoreFoundation                      0x0000000112f8f84f -[__NSPlaceholderDictionary initWithObjects:forKeys:count:] + 383
3   CoreFoundation                      0x0000000112fa259b +[NSDictionary dictionaryWithObjects:forKeys:count:] + 59
4   GolfersProject                      0x000000010f6fe861 -[ContactUsViewController collectionView:cellForItemAtIndexPath:] + 1137
5   UIKit                               0x00000001118740c7 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 235
6   UIKit                               0x0000000111875ab9 -[UICollectionView _updateVisibleCellsNow:] + 3467
7   UIKit                               0x00000001118799f9 -[UICollectionView layoutSubviews] + 267
8   UIKit                               0x00000001112d5a2b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536
9   QuartzCore                          0x0000000113575ec2 -[CALayer layoutSublayers] + 146
10  QuartzCore                          0x000000011356a6d6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
11  QuartzCore                          0x000000011356a546 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
12  QuartzCore                          0x00000001134d6886 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
13  QuartzCore                          0x00000001134d7a3a _ZN2CA11Transaction6commitEv + 462
14  QuartzCore                          0x00000001134d80eb _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
15  CoreFoundation                      0x0000000112fb8ca7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
16  CoreFoundation                      0x0000000112fb8c00 __CFRunLoopDoObservers + 368
17  CoreFoundation                      0x0000000112faea33 __CFRunLoopRun + 1123
18  CoreFoundation                      0x0000000112fae366 CFRunLoopRunSpecific + 470
19  GraphicsServices                    0x0000000113dcaa3e GSEventRunModal + 161
20  UIKit                               0x0000000111255900 UIApplicationMain + 1282
21  GolfersProject                      0x000000010f6f504f main + 111
22  libdyld.dylib                       0x0000000113832145 start + 1
23  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

1 个答案:

答案 0 :(得分:4)

[UIFont fontWithName:@"Helvetica-Neue" size:17.0]返回nil。这就是它崩溃应用程序的原因。仔细检查字体是不是nil,它应该可以正常工作。