2017-11-06 15 views
2

UITextViewのattributedTextにアンダーラインまたは取り消し線の属性を設定しようとすると、ランタイムエラーが発生します。フォントや背景色などの他のプロパティは問題ありません。ここにコードスニペットがあります。私が持っているのは、単一のUITextViewを持つテストプロジェクトです。私は、iOS 11.UITextView - テキストの下線または取り消し線属性を設定できませんか?

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

これは、テキストの消失につながる上だと多少の誤差がプリントさ:

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

答えて

6

あなたの下線スタイルが無効です。これに

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

:この行を変更し

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

結果:それがあった

enter image description here

+0

ブーム!ありがとうございました。 rawValueはなぜここに必要ですか?私はSwiftにとってかなり新しいことを認めますが、それはあまりにも冗長で醜いようです。私は素早くきれいになると思った。 –

+0

https://developer.apple.com/documentation/foundation/nsattributedstringkey/1524865-underlinestyle - "この属性[underlineStyle]の値は、整数を含むNSNumberオブジェクトです"。 NSNumberがキーと値のペアの値として有効でない理由がわかりません。 –

+0

あなたは自分で答えを出しました。列挙型は整数ではありません。その生の値は整数です。 – matt

関連する問題