16

I'm getting runtime failures whenever I try to set underline or strikethrough attributes on the attributedText of a UITextView. Other properties, like font and background color have no problems. Here's a code snippet. All I have is a test project with a single UITextView. I'm on iOS 11.

class ViewController: UIViewController {

    @IBOutlet weak var myTextView: UITextView!

    var myMutableString = NSMutableAttributedString(string: "")

    override func viewDidLoad() {
        super.viewDidLoad()

        let string = "The cow jumped over the moon" as NSString
        myMutableString = NSMutableAttributedString(string: string as String)

        let underlineAttribute = [NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle]

        myMutableString.addAttributes(underlineAttribute, range: string.range(of: "cow"))

        myTextView.attributedText = myMutableString
    }
}

This results in the text disappearing and some errors printed:

2017-11-05 19:43:34.708830-0800 UITextView_Test[11771:907014]
-[_SwiftValue _getValue:forType:]: unrecognized selector sent to instance 0x60c00004eb50 2017-11-05 19:43:34.709351-0800 UITextView_Test[11771:907014] <NSATSTypesetter: 0x60800016be80>: Exception -[_SwiftValue _getValue:forType:]: unrecognized selector sent to instance 0x60c00004eb50 raised during typesetting layout manager <NSLayoutManager: 0x6080001fe400>
    1 containers, text backing has 28 characters
    Currently holding 28 glyphs.
    Glyph tree contents:  28 characters, 28 glyphs, 1 nodes, 64 node bytes, 64 storage bytes, 128 total bytes, 4.57 bytes per character,
4.57 bytes per glyph
    Layout tree contents:  28 characters, 28 glyphs, 0 laid glyphs, 0 laid line fragments, 1 nodes, 64 node bytes, 0 storage bytes, 64 total bytes, 2.29 bytes per character, 2.29 bytes per glyph, 0.00 laid glyphs per laid line fragment, 0.00 bytes per laid line fragment , glyph range {0 28}. Ignoring... 2017-11-05 19:43:34.709827-0800 UITextView_Test[11771:907014] -[_SwiftValue _getValue:forType:]: unrecognized selector sent to instance 0x60c00004eb50 2017-11-05 19:43:34.710014-0800 UITextView_Test[11771:907014] <NSATSTypesetter: 0x60800016be80>: Exception -[_SwiftValue _getValue:forType:]: unrecognized selector sent to instance 0x60c00004eb50 raised during typesetting layout manager <NSLayoutManager: 0x6080001fe400>
    1 containers, text backing has 28 characters
    Currently holding 28 glyphs.
    Glyph tree contents:  28 characters, 28 glyphs, 1 nodes, 64 node bytes, 64 storage bytes, 128 total bytes, 4.57 bytes per character,
4.57 bytes per glyph
    Layout tree contents:  28 characters, 28 glyphs, 0 laid glyphs, 0 laid line fragments, 1 nodes, 64 node bytes, 0 storage bytes, 64 total bytes, 2.29 bytes per character, 2.29 bytes per glyph, 0.00 laid glyphs per laid line fragment, 0.00 bytes per laid line fragment , glyph range {0 28}. Ignoring...
Adam Bryant
  • 545
  • 3
  • 13

1 Answers1

57

Your underline style is invalid. Change this line:

let underlineAttribute = 
    [NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle]

to this:

let underlineAttribute = 
    [NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue]

Result:

enter image description here

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 2
    Boom, that was it! Thank you. Why is rawValue needed here? I'll admit I'm fairly new to Swift but that seems overly-verbose and ugly. I thought swift was supposed to be cleaner and simpler. – Adam Bryant Nov 06 '17 at 04:02
  • From https://developer.apple.com/documentation/foundation/nsattributedstringkey/1524865-underlinestyle – "The value of this attribute [underlineStyle] is an NSNumber object containing an integer". I don't see why the NSNumber isn't valid as the value in the key-value pair. – Adam Bryant Nov 06 '17 at 04:06
  • You gave the answer yourself. An enum is not an integer. Its raw value is the integer. – matt Nov 06 '17 at 04:30
  • Swift enums: too clever by half – Adam Bryant Nov 06 '17 at 05:09
  • 4
    I'd say that the fault lies not with Swift but with Cocoa, which is written in Objective-C and knows nothing of Swift enums. Therefore this dictionary value is, as you yourself have said, an integer — not a Swift enum. Therefore, to talk to Cocoa, we have to "cross the bridge" by taking the enum's raw value. – matt Nov 06 '17 at 15:02