Created
June 3, 2015 13:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just include into Xcode project, no call needed.