Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 应用NSMUTABEATTRIBUTED字符串崩溃_Ios_Swift_Nsmutableattributedstring - Fatal编程技术网

Ios 应用NSMUTABEATTRIBUTED字符串崩溃

Ios 应用NSMUTABEATTRIBUTED字符串崩溃,ios,swift,nsmutableattributedstring,Ios,Swift,Nsmutableattributedstring,我有一个ui按钮,我试图将标题设置为有两行,第二行的字体应该比第一行小。这是我的密码: self.startBtn.setTitle("First\nSecond Line", forState: .Normal) let string = NSMutableAttributedString() string.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(13), range: NSMakeRange(4, 5

我有一个
ui按钮
,我试图将标题设置为有两行,第二行的字体应该比第一行小。这是我的密码:

self.startBtn.setTitle("First\nSecond Line", forState: .Normal)

let string = NSMutableAttributedString()
string.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(13), range: NSMakeRange(4, 5))
self.startBtn.titleLabel?.attributedText = string
当我运行应用程序时,出现以下错误:

Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000106fbbc65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000106c54bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x0000000106fbbb9d +[NSException raise:format:] + 205
    3   Foundation                          0x00000001067e26ae -[NSRLEArray objectAtIndex:effectiveRange:] + 142
    4   Foundation                          0x00000001067fa029 -[NSConcreteMutableAttributedString addAttribute:value:range:] + 209
    5   myApp                             0x00000001064e0a93 _TFC7myApp18MainViewController9startTimefS0_FCSo8UIButtonT_ + 1363
    6   myApp                             0x00000001064e0eaa _TToFC7myApp18MainViewController9startTimefS0_FCSo8UIButtonT_ + 58
    7   UIKit                               0x0000000107561da2 -[UIApplication sendAction:to:from:forEvent:] + 75
    8   UIKit                               0x000000010767354a -[UIControl _sendActionsForEvents:withEvent:] + 467
    9   UIKit                               0x0000000107672919 -[UIControl touchesEnded:withEvent:] + 522
    10  UIKit                               0x00000001075ae998 -[UIWindow _sendTouchesForEvent:] + 735
    11  UIKit                               0x00000001075af2c2 -[UIWindow sendEvent:] + 682
    12  UIKit                               0x0000000107575581 -[UIApplication sendEvent:] + 246
    13  UIKit                               0x0000000107582d1c _UIApplicationHandleEventFromQueueEvent + 18265
    14  UIKit                               0x000000010755d5dc _UIApplicationHandleEventQueue + 2066
    15  CoreFoundation                      0x0000000106eef431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    16  CoreFoundation                      0x0000000106ee52fd __CFRunLoopDoSources0 + 269
    17  CoreFoundation                      0x0000000106ee4934 __CFRunLoopRun + 868
    18  CoreFoundation                      0x0000000106ee4366 CFRunLoopRunSpecific + 470
    19  GraphicsServices                    0x00000001096cca3e GSEventRunModal + 161
    20  UIKit                               0x0000000107560900 UIApplicationMain + 1282
    21  myApp                             0x00000001064da2c7 main + 135
    22  libdyld.dylib                       0x0000000109ab3145 start + 1
    23  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

出现异常的原因是,在为某些字符设置字体时,
nsmutableAttributeString
为空。确保字符串的字符范围在
{4,5}
将解决此问题:

let string = NSMutableAttributedString(string:"First\nSecond Line")
string.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(13), range: NSMakeRange(4, 5))
self.startBtn.titleLabel?.attributedText = string

注意:范围
{4,5}
中的字符是
“t\nSec”
。如果您希望第二行使用较小的字体,则范围应为
{6,11}

出现异常的原因是,在为其某些字符设置字体时,
nsmutableAttributeString
为空。确保字符串的字符范围在
{4,5}
将解决此问题:

let string = NSMutableAttributedString(string:"First\nSecond Line")
string.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(13), range: NSMakeRange(4, 5))
self.startBtn.titleLabel?.attributedText = string
注意:范围
{4,5}
中的字符是
“t\nSec”
。如果您希望第二行使用较小的字体,则范围应为
{6,11}