NSMutableAttributedString和UITextView单元测试

时间:2019-05-12 13:41:23

标签: swift unit-testing

我正在为NSMutableAttributedString和UITextView添加一些单元测试,不确定如何解决这种情况。

该应用程序具有麦克风图标,该人可以在其中讲话,并且文本视图将记录该人的讲话。转录将突出显示转录,直到该人或语音朗读停止记录,并还原为先前的ui颜色设置。

func setSpeechToTextTranscript(transcript:String)

此函数将获取这样的字符串

SpeechToText didHypothesizeTranscription: Hello
SpeechToText didHypothesizeTranscription: Hello
SpeechToText didHypothesizeTranscription: Hello
SpeechToText didHypothesizeTranscription: Hello how
SpeechToText didHypothesizeTranscription: Hello how can I
SpeechToText didHypothesizeTranscription: Hello how can I help
SpeechToText didHypothesizeTranscription: Hello how can I help you
SpeechToText didHypothesizeTranscription: Hello how can I help you

它将有两个字符串,一个用于先前的文本和转录

enter image description here

两个字符串都有不同的NSMutableAttributedString属性

        let unfocusedTextToSpeechAttributes = createAttribute(font: self.font!, foregroundColor: getTextColorByBrightness())
        let unfocusedTextRange = NSMakeRange(0, textCache.count)

        let focusedTextToSpeechAttributes = createAttribute(font: self.font!, foregroundColor: self.getHighlightByBrightness(backgroundColor: self.backgroundColor!))
        let focusedTextRange = NSMakeRange(textCache.count + 1, transcript.count)

我需要在textview中测试属性,如何获取属性以验证其是否正确?

我尝试执行此功能以获取预期的4个属性(未聚焦的和聚焦的属性具有其中的两个字体和文本颜色),但UITextView的属性具有2-

func getTextAttributes() -> [NSAttributedString.Key : Any] {
    var range = NSRange(location: 0, length: text.count)

    let attributes = self.attributedText.attributes(at: 0, effectiveRange: &range)

    print("attributes count : " + String(attributes.count))

    for attribute in attributes {
        let key = attribute.key.rawValue
        let value = attribute.value

        if(key == "NSColor") {

        }
        else if(key == "NSFont") {

        }

        print(key)
    }

    return attributes
}


attributes count : 2
NSColor
NSFont

我可以按照一些指示将属性存储在某些字典中进行验证,我不确定是否要利用/研究TextKit最佳实践-WWDC 2018或I中提到的“存储”(NSTextStorage或NSTextContainer)做错了。

0 个答案:

没有答案