PHOENIX — In nearly 20 years as sheriff of Maricopa County, Joe Arpaio has honed his publicity skills, using headline-grabbing, often outrageous maneuvers to build a reputation as the nation’s toughest law officer and make himself a hero to many Americans.


He created the nation’s first female chain gang; issued pink socks and underwear to the men held in the county’s jails; and housed inmates in refurbished Korean War tents under the blazing desert sun, then cut the salt and pepper out of their two meals a day. He also sent a posse to Hawaii to check on President Obama’s birth certificate, earning staunch supporters and vociferous opponents along the way.


But now his penchant for public relations coups, each seemingly intended to outdo the last, threatens to become one of his greatest liabilities.


Sheriff Arpaio and his office are on trial in a federal class-action lawsuit here, accused of singling out Latinos, regardless of citizenship or immigration status, for stops, questioning and detention during large-scale policing operations. The Justice Department has sued him on the same grounds, alleging discriminatory practices that extend from the streets to the jails.


On the stand last week, he had to confront past statements to the news media: Is it indeed “an honor” to be compared to the Ku Klux Klan, as he once told the TV anchor Lou Dobbs? Is the appearance of having “just came from another country” reason enough to target a person for arrest, as he said to the talk show host Glenn Beck?


“Sheriff,” Stanley Young, one of the plaintiffs’ lawyers, asked him, “which is the truth — what you say here in court, or what you say to audiences who want to hear you talk?”


It is an uncomfortable question for someone facing the potential of censure in the lawsuit, filed on behalf of every Latino pulled over by his deputies since 2007, not to mention a type of question that any candidate would dread in an election year. But Sheriff Arpaio, who, at 80, is running for his sixth term in office, did not seem to care. (His answer: “What I say in court under oath, that’s the truth.”)


A day after his testimony, looking down on the courthouse from his office on the 18th floor of one of Phoenix’s taller buildings, he wiggled his fingers, as if shooing a pesky fly, and said: “The more they go after me, the more I do my job. If they think I’m going to hide now, they’re wrong.”


Sheriff Arpaio has wisps of gray in his thinning brown hair, though his looks do not betray his age. He says he works 14 hours a day, and spending time with him makes it not hard to believe — though much of that work appears to be devoted to bolstering the image that keeps getting him re-elected. He rarely denies interview requests, and he can talk for hours; aides had to barge in repeatedly one recent afternoon to remind him of another commitment, a going-away party for a member of his staff.


His news releases often carry headlines written in bold red letters. “Eight more illegal aliens detained. Among them a 3-year-old,” a recent one read. He uses a Smith Corona typewriter to keep records of every interview he has given; a stack of paper thicker than an encyclopedia fills a deep drawer in his desk. (There were 13 news organizations, from as far away as Russia and Japan, listed for June 25, when the United States Supreme Court issued its ruling on Arizona’s immigration law.)


“You know what I average on TV a month? Here, local?” Sheriff Arpaio said defiantly, punching his desk for emphasis. “Two hundred appearances.”


He handed a visitor his résumé, which runs to five pages and lists under “awards and citations” the fact that he has been “featured and profiled thousands of times by worldwide news media.” He also offered a transcript of some of the most recent messages left for him at his office.


July 18, Parker, Ariz.: “Thank you for all you do.”


July 19, Nevada: “Wish you could be cloned.”


July 20, Ohio: “Sending 100-percent support.”


(Also among the messages were “You have an al-Qaida mentality.”)


Beneath his hard shell, though, there have been occasional signs of insecurity. When he first took office in 1993, many people here recall, he used to go around asking strangers, “Do you know who I am?”


And though he needs no introduction these days, he sounded regretful in pointing out that people still know little about him beyond his years as sheriff. So this summer, there will be television ads noting his service as an agent for the Drug Enforcement Administration and its predecessor, the Bureau of Narcotics and Dangerous Drugs, in places like Turkey and Mexico, along with Arizona.


Some of Sheriff Arpaio’s opponents here say the news media has a complicit relationship with him, frequently promoting the tough-guy and regular-guy images he has worked hard to project. On his birthday in June, he raced reporters on go-carts (“to show I could do it,” he said) while his deputies raided an auto parts store, arresting four workers on charges of using fraudulent Social Security numbers on their job applications. Both events made the evening news.