Skip to content

Instantly share code, notes, and snippets.

@dunkelstern
Created June 3, 2015 13:09
Show Gist options
  • Select an option

  • Save dunkelstern/84c7b2de0534f11b4fe1 to your computer and use it in GitHub Desktop.

Select an option

Save dunkelstern/84c7b2de0534f11b4fe1 to your computer and use it in GitHub Desktop.
Fix UILabel not auto-resizing it's height when using autolayout in landscape mode with a multi line label
@import UIKit;
#import "JRSwizzle.h" // <- from cocoapods: https://cocoapods.org/pods/JRSwizzle
// Stackoverflow question: http://stackoverflow.com/questions/17491376/ios-autolayout-multi-line-uilabel
// Answer see: http://stackoverflow.com/a/23696158/518532
@interface UILabel (AutoLayoutFix)
- (void)DST_setBounds:(CGRect)bounds;
@end
@implementation UILabel (AutoLayoutFix)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
NSError *error;
if (![class jr_swizzleMethod:@selector(setBounds:) withMethod:@selector(DST_setBounds:) error:&error]) {
NSLog(@"Swizzling %@ setBounds: failed, %@", class, error);
}
});
}
- (void)DST_setBounds:(CGRect)bounds {
[self DST_setBounds:bounds]; // swizzled
if (bounds.size.width != self.preferredMaxLayoutWidth) {
self.preferredMaxLayoutWidth = self.bounds.size.width;
}
}
@end
@dunkelstern

Copy link
Copy Markdown
Author

Just include into Xcode project, no call needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment