No, there’s no rule that favors spelling out the negative verb form “didn’t” in certain compositions. The choice on whether or not to spell it out simply depends on the tone intended by the writer or speaker—the uncontracted “did not” for a formal tone or mood and the contracted “didn’t” for an informal or casual one.
In fiction, conversations obviously will favor using the contracted “didn’t” as this reflects the natural way people speak; indeed, a conversation will sound stilted or unauthentic if the characters spoke using the uncontracted “did not.” About the only time a character may be expected to use the uncontracted “did not” is when he or she is emphasizing or vehemently stating a point, as in “I certainly did not invite that creep to the party!” Also, in fiction, foreigners grappling with English will often be portrayed speaking with uncontracted “did nots.”
Academic writing is normally expected to avoid using “didn’t” as a matter of form. In journalistic reporting, it is obligatory to use the contracted “didn’t” in quoted statements if, in fact, the speaker uttered it that way; a reporter isn’t at liberty to change it to the uncontracted “did not” in the news story.