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

16
我在尝试给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
    }
}

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

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...
1个回答

57

您的下划线样式无效。请更改此行:

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

到这个:

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

结果:

在此输入图片描述


2
太好了,就这样!谢谢。为什么这里需要rawValue?我承认我在Swift方面相对较新,但那似乎过于冗长和丑陋。我原本以为Swift应该更加简洁明了。 - Adam Bryant
从 https://developer.apple.com/documentation/foundation/nsattributedstringkey/1524865-underlinestyle - “此属性[underlineStyle]的值是包含整数的NSNumber对象。” 我不知道为什么NSNumber不能作为键值对中的值。 - Adam Bryant
你已经给出了答案。枚举不是整数,它的原始值是整数。 - matt
Swift 枚举:聪明过头 - Adam Bryant
4
我认为问题不在于Swift本身,而是因为Cocoa是用Objective-C编写的,不了解Swift的枚举类型。因此,正如你自己所说,这个字典值实际上是一个整数,而不是Swift的枚举类型。因此,为了与Cocoa交互,我们需要通过使用枚举的原始值来“跨越桥梁”。 - matt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接