内容自适应UILabel
xcode 6.1
File-New-Project..
iOs-Application-Simple View Application
代码:
- (void)viewDidLoad { [super viewDidLoad]; UILabel *labelLoginname = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)]; //[labelLoginname setNumberOfLines:0]; NSString *s = @"账号:"; UIFont *font = [UIFont fontWithName:@"Arial" size:15]; CGSize size = CGSizeMake(0,0); NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:15]}; CGSize labelsize = [s boundingRectWithSize:size options: NSStringDrawingUsesFontLeading attributes:attribute context:nil].size; labelLoginname.frame = CGRectMake(80.0, 80.0, labelsize.width, labelsize.height); labelLoginname.backgroundColor = [UIColor whiteColor]; labelLoginname.textColor = [UIColor blackColor]; labelLoginname.text = s; labelLoginname.font = font; [self.view addSubview:labelLoginname];
}
道法自然