0

This is the code :

let sub1 = subsessions[0]
let titleText = NSMutableAttributedString(string: String(format: "%@\n%@", session.Title, sub1.Subtitle))
titleText.addAttribute(NSFontAttributeName, value: UIFont(name: "AppleGaramond-Italic", size: 23), range: NSMakeRange(0, session.Title.characters.count))
titleText.addAttribute(NSFontAttributeName, value: UIFont(name: "AppleGaramond-BoldItalic", size: 23), range: NSMakeRange(session.Title.characters.count + 1, sub1.Subtitle.characters.count))
titleText.addAttribute(NSForegroundColorAttributeName, value: UIColor.white, range: NSMakeRange(0, titleText.length))
label_title.attributedText = titleText 

I never get an exception in this code but when I continue while debugging, I get the crash. When I commented lines with NSFontAttributeName, the exception never came. So definitely it's an issue with these two lines.
I am running on Swift 3.

Stack backtrace :

enter image description here

* thread #1: tid = 0x20b0e, 0x25237c5c libsystem_kernel.dylib`__pthread_kill + 8, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
    frame #0: 0x25237c5c libsystem_kernel.dylib`__pthread_kill + 8
    frame #1: 0x252ddb46 libsystem_pthread.dylib`pthread_kill + 62
    frame #2: 0x251cc0c4 libsystem_c.dylib`abort + 108
    frame #3: 0x24d427dc libc++abi.dylib`abort_message + 108
    frame #4: 0x24d5b6a0 libc++abi.dylib`default_terminate_handler() + 268
    frame #5: 0x24d67098 libobjc.A.dylib`_objc_terminate() + 192
    frame #6: 0x24d58e16 libc++abi.dylib`std::__terminate(void (*)()) + 78
    frame #7: 0x24d588f6 libc++abi.dylib`__cxa_rethrow + 102
    frame #8: 0x24d66f46 libobjc.A.dylib`objc_exception_rethrow + 42
    frame #9: 0x254b824e CoreFoundation`CFRunLoopRunSpecific + 650
    frame #10: 0x254b7fbc CoreFoundation`CFRunLoopRunInMode + 108
    frame #11: 0x26ad4af8 GraphicsServices`GSEventRunModal + 160
    frame #12: 0x29bf2434 UIKit`UIApplicationMain + 144
  * frame #13: 0x000b8298 PPOnlineClient`main + 172 at AppDelegate.swift:13
    frame #14: 0x25164872 libdyld.dylib`start + 2
Nitish
  • 13,845
  • 28
  • 135
  • 263
  • Can you add the stack backtrace? – Martin R Nov 04 '16 at 09:20
  • @MartinR : Added as image. – Nitish Nov 04 '16 at 09:40
  • Are you sure that the fonts are available on your system? What happens if you unwrap forcefully `UIFont(name: "AppleGaramond-Italic", size: 23)!` ? – Martin R Nov 04 '16 at 10:02
  • @MartinR : Yes I have the font. I get this exception on forcefully unwrapping : fatal error: unexpectedly found nil while unwrapping an Optional value – Nitish Nov 04 '16 at 10:20
  • But that error clearly indicates that the font could not be loaded. Perhaps the name is not correct, or it is not correctly added as a resource to the app. You should fix that issue first. – Martin R Nov 04 '16 at 10:31
  • @MartinR : I am using it in other screen without NSMutableAttributedString, and it works fine. Moreover I am using in in storyboard as well. – Nitish Nov 04 '16 at 10:40
  • Well, if `UIFont(...)` returns `nil` then that is your first problem. – Martin R Nov 04 '16 at 11:02
  • @Nitish try to print font family and find your font name. May be case that font name is wrong. – Chirag Shah May 16 '17 at 05:51

0 Answers0