【问题标题】:UITextView – can't set underline or strikethrough attributes on text?UITextView – 不能在文本上设置下划线或删除线属性?
【发布时间】:2017-11-06 03:48:21
【问题描述】:

每当我尝试在 UITextView 的属性文本上设置下划线或删除线属性时,都会遇到运行时故障。其他属性,如字体和背景颜色没有问题。这是一个代码sn-p。我所拥有的只是一个带有单个 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...

【问题讨论】:

    标签: ios uikit uitextview appkit textkit


    【解决方案1】:

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

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

    到这里:

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

    结果:

    【讨论】:

    • 轰隆隆,就是这样!谢谢你。为什么这里需要 rawValue?我承认我对 Swift 还很陌生,但这似乎过于冗长和丑陋。我认为 swift 应该更干净、更简单。
    • 来自developer.apple.com/documentation/foundation/…——“这个属性[underlineStyle]的值是一个包含整数的NSNumber对象”。我不明白为什么 NSNumber 作为键值对中的值无效。
    • 你自己给出了答案。枚举不是整数。它的原始值是整数。
    • Swift 枚举:太聪明了一半
    • 我想说,问题不在于 Swift,而在于 Cocoa,它是用 Objective-C 编写的,对 Swift 枚举一无所知。因此,正如您自己所说,这个字典值是一个整数——而不是 Swift 枚举。因此,要与 Cocoa 对话,我们必须通过获取枚举的原始值来“过桥”。
    猜你喜欢
    • 2014-03-22
    • 2012-10-18
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 2012-09-13
    • 1970-01-01
    • 2014-02-24
    相关资源
    最近更新 更多