UITextView - 无法在文本上设置下划线或删除线属性?

Ada*_*ant 13 uitextview uikit appkit ios textkit

每当我尝试在UITextView的attributedText上设置下划线或删除线属性时,我都会遇到运行时故障.其他属性,如字体和背景颜色没有问题.这是一段代码片段.我所拥有的只是一个带有单个UITextView的测试项目.我在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
    }
}
Run Code Online (Sandbox Code Playgroud)

这导致文本消失并打印出一些错误:

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...
Run Code Online (Sandbox Code Playgroud)

mat*_*att 43

您的下划线样式无效.改变这一行:

let underlineAttribute = 
    [NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle]
Run Code Online (Sandbox Code Playgroud)

对此:

let underlineAttribute = 
    [NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue]
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

  • 我要说的是,错误不在于Swift,而在于Cocoa,它是用Objective-C编写的,对Swift枚举一无所知.因此,正如您自己所说,这个字典值是一个整数 - 而不是Swift枚举.因此,与Cocoa交谈,我们必须通过获取enum的原始值来"过桥". (3认同)
  • 砰的一声,就是这样!谢谢.为什么需要rawValue?我承认我对Swift相当新,但这看起来过于冗长和丑陋.我认为斯威夫特应该更干净,更简单. (2认同)