简体   繁体   中英

Set attributed string to an UILabel in iOS

Application is crashing on creating contactAttributes using this code:

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];

Here is the log:

*** 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

[UIFont fontWithName:@"Helvetica-Neue" size:17.0] returns nil. That's why it's crashing the app. Double check that the font is not nil, and it should work just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM