A federal judge on Monday dismissed a defamation lawsuit filed by the pornographic film actress Stephanie Clifford against President Trump, ruling that the president had not defamed her on Twitter last spring and ordering her to pay his legal fees.

The tweet in question was posted by the president on April 18, one day after Ms. Clifford, known professionally as Stormy Daniels, posted a sketch of a man who, she alleged, threatened her in 2011 as she was first considering speaking out about the affair she said she had with Mr. Trump. Mr. Trump called the sketch “a total con job,” depicting “a nonexistent man.”