在 iOS 中将属性字符串设置为 UILabel
Posted
技术标签:
【中文标题】在 iOS 中将属性字符串设置为 UILabel【英文标题】:Set attributed string to an UILabel in iOS 【发布时间】:2015-09-03 12:12:38 【问题描述】:应用程序在使用此代码创建 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】:[UIFont fontWithName:@"Helvetica-Neue" size:17.0]
返回零。这就是它使应用程序崩溃的原因。仔细检查字体不为零,它应该可以正常工作。
【讨论】:
以上是关于在 iOS 中将属性字符串设置为 UILabel的主要内容,如果未能解决你的问题,请参考以下文章