At the time the Ranger went away mid sizes were not selling well. Don’t forget the Colorado was retired too.
Yearly sales in the US:
http://www.goodcarbadcar.net/2011/01/toyota-tacoma-sales-figures/
http://www.goodcarbadcar.net/2011/01/ford-ranger-sales-figures/
Year---Ranger--------Tacoma
2002--226,094-------151,960
2003--209,117-------154,154
2004--156,322-------152,933
2005--120,958-------168,831
2006--92,420--------178,351
2007--72,711--------173,238
2008--65,872--------144,655
2009--55,600--------111,824
2010--55,364--------106,198
2011--70,832--------110,705
2012--19,366--------141,365
2013--0-------------159,485
2014--0-------------155,041
2015--0-------------179,562
2016--0-------------191,631
2017--0-------------198,124
I'm not sure it's accurate to say that midsize truck sales were struggling. Ranger sales definitely were, but they were trying to sell a truck that had it's origins all the way back in 93. Toyota has consistently sold over 100k Tacos in the US annually, and they're approaching 200k. But they've redesigned the truck multiple times during the Ranger's nearly 20 years on the market. GM and Nissan did the same. Even Chrysler released an entirely new Dakota during that time. We all know that Ford made updates to the Ranger between 2012 and 93, but when a 2012 shares body panels with a 93, it's a dinosaur.
By 2012, the Ranger was pure profit for Ford. The development and tooling had long been paid for, so even if the margins weren't as good as they are on loaded F-150s they still should've been profitable. But, they'd taken the platform as far as it could go. Customers weren't interested in buying a truck that shared parts with a 19 year old truck but cost the same as it's much newer competition. More stringent crash test standards and fuel economy had caught up with the Ranger's 20 year old design and it couldn't go on any longer.
Maybe there were too many players in the small truck class. Maybe the crash in 08-09 meant that they didn't have the cash to fund a replacement (but they still developed multiple iterations of the "global" Ranger so I'm not sure that's the case). Maybe they were hoping that Ranger buyers would just switch to F-150s that had higher profit margins. I think the increase in Tacoma sales after the last Ranger was discontinued might show that instead of Ranger buyers upgrading to F-150s as Ford had hoped, many were switching to Tacomas to stay in "small" trucks.