-1

I have a bunch of text views, and I now want to turn autoresizing on in all of them. I don't want to highlight each one individually, and I only want the changes to apply to all text views in the app. So I don't want to modify the framework. I will be sure to pick the best answer.

EDIT: I guess I should have mentioned earlier that I am using storyboards.

  • 5
    "I will be sure to pick the best answer." - me too. [What have you tried?](http://whathaveyoutried.com) –  Dec 11 '12 at 18:49
  • I'm afraid I haven't tried anything yet. I didn't even know if it could be done, seeing as the text views and controllers are not linked to a custom class. –  Dec 11 '12 at 23:41
  • Have you tried using the appearance proxy? It may have some long-term risks but it should work. – Aaron Dec 12 '12 at 14:19

2 Answers2

0

In iOS 5.0+, you should be able to use the appearance proxy for UITextView like so:

[[UITextView appearance] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

This would make all UITextView's respond with a flexible width and height. Here's a great tutorial on how to use the proxy for other UI controls:

http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5

EDITED:

This code should work and I've tested this in both the simulator and on an iPhone directly, even though the autoresizingMask property in UIView.h is not flagged with UI_APPEARANCE_SELECTOR. This post seems to indicate that its possible for a property to not have this flag, yet still obey messages from the appearance proxy. All that said, @rmaddy is right. It might be a risk to rely on this long-term.

Community
  • 1
  • 1
Aaron
  • 7,055
  • 2
  • 38
  • 53
  • You can only use the `appearance` proxy on properties that are flagged with `UI_APPEARANCE_SELECTOR`. The `UIView autoresizingMask` is not such a property. Did you actually try the code you suggested or is this a guess? – rmaddy Dec 11 '12 at 18:57
  • I have tried the code and it works in the simulator (famous last words, I know) even though I don't see the `UI_APPEARANCE_SELECTOR` flagged on the `autoResizingMask` property in UIView.h. Poking around a bit, [this answer](http://stackoverflow.com/questions/8257556/ios-5-curious-about-uiappearance) seems to indicate that even if `UI_APPEARANCE_SELECTOR` isn't flagged for a given property, it still *might* work. – Aaron Dec 11 '12 at 20:49
  • Interesting. I added that line to my own project and it didn't crash like I suspected it would. But I have no idea if it actually applies the resizing mask to every text view or not. BTW - you have an extra pair of square brackets in the code you posted. – rmaddy Dec 11 '12 at 20:55
  • Indeed there are extra brackets. Just removed them. I also just tested this on an iPhone 5 running iOS 6.0.1 and had no problems. The UITextViews demonstrated the flexed width and height. The code works, but the docs make it seem like it shouldn't. – Aaron Dec 11 '12 at 21:08
  • 1
    I'd be very weary about relying on this. It may work now but it is undocumented behavior and it could stop working in iOS 6.1 or some other future update. – rmaddy Dec 11 '12 at 21:12
  • I'll try this. I think Apple's engineers are unlikely to break this code. I'll be sure to file a report to bring this behavior to their attention. EDIT: Where do I put this code? –  Dec 13 '12 at 01:47
  • I'm not 100% sure of the best practice here, but it makes sense to put them in AppDelegate's `didFinishLaunchingWithOptions` method because they're a sort of "global" setting. [Be sure to read docs, too](http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAppearance_Protocol/Reference/Reference.html). – Aaron Dec 13 '12 at 15:38
  • Congrats, you won one up vote. Though I'm still not exactly sure the method is working, as the text views look unchanged ATM. What I'm actually trying to do is make the text look good in landscape mode. –  Dec 13 '12 at 17:28
  • @moonman239 what version of iOS are you running against? Have you tried styling a UITextView in IB with the properties how you want them, and then another from code without the properties to see if there is a difference in appearance / behavior? – Aaron Dec 13 '12 at 17:44
0

One option is to subclass UITextView and create a CustomTextView. In its init methods or layoutSubviews method, set the autoresizing mask and then use that CustomTextView in your project. One advantage with usage of init method for setting this is that, you can later override this autoresize mask.

iDev
  • 23,310
  • 7
  • 60
  • 85
  • What if you are adding the view in the xib? The init doesn't get called right? – yuf Dec 11 '12 at 20:13
  • Not sure about that since I dont use xibs. There should be some method which will get called in that case. This should be added there. Something like `layoutSubviews` or so should get called. Or check if `loadNibNamed` or something like that is getting called. This is the best way to do this in a single place. – iDev Dec 11 '12 at 20:23