Created
August 1, 2014 06:31
-
-
Save rizumita/e32d839c8d55c75c952c to your computer and use it in GitHub Desktop.
UILabelで幅固定で高さを計算する
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
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 50.0, 500.0)]; | |
view.translatesAutoresizingMaskIntoConstraints = NO; | |
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 50.0, 44.0)]; | |
label.translatesAutoresizingMaskIntoConstraints = NO; | |
label.lineBreakMode = NSLineBreakByWordWrapping; | |
[label setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical]; | |
[label setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical]; | |
label.text = @"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; | |
label.numberOfLines = 0; | |
NSLog(@"%@", NSStringFromCGRect(label.frame)); | |
[view addSubview:label]; | |
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[label]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]]; | |
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[label]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]]; | |
[label addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[label(200@300)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]]; | |
[view layoutIfNeeded]; | |
NSLog(@"%@", NSStringFromCGRect(label.frame)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment