#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) UILabel *fontLabel;
@property (nonatomic, strong) UITextField *textField;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 50, 320, 40)];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTextField) name:UITextFieldTextDidChangeNotification object:nil];
_textField.backgroundColor = [UIColor purpleColor];
_textField.keyboardType = UIKeyboardTypeEmailAddress;
_textField.returnKeyType = UIReturnKeyDone;
_textField.delegate = self;
[self.view addSubview:_textField];
_fontLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,140,320,40)];
_fontLabel.backgroundColor = [UIColor blueColor];
NSString *fontString = @"明月几时有,把酒问酒吧";
CGSize labelSize = CGSizeMake(320,2000);
CGSize fontStringSize = [fontString boundingRectWithSize:labelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13]} context:nil].size;
_fontLabel.frame = CGRectMake(10, 260, fontStringSize.width, 40);
_fontLabel.text = fontString;
// [_fontLabel sizeToFit];
_fontLabel.textAlignment = NSTextAlignmentRight;
[self.view addSubview:_fontLabel];
UIButton *click = [[UIButton alloc]initWithFrame:CGRectMake(50, 100, 140, 40)];
[click setTitle:@"点击我吧" forState:UIControlStateNormal];
click.backgroundColor = [UIColor redColor];
[click addTarget:self action:@selector(touchMe) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:click];
}
- (void)touchMe {
[_fontLabel sizeToFit];
}
- (void)changeTextField {
_fontLabel.text = self.textField.text;
// [_fontLabel sizeToFit];
}