Skip to content

Instantly share code, notes, and snippets.

@yl1003
Created April 9, 2014 13:26
Show Gist options
  • Save yl1003/10270319 to your computer and use it in GitHub Desktop.
Save yl1003/10270319 to your computer and use it in GitHub Desktop.
UILabel根据内容调节高度
/*根据uilabel的内容决定uilabel的高度--iOS7之前*/
CGSize maximumSize = CGSizeMake(scrollView.width - 10, 999);
NSString *dateString = _passport_useZone.text;
UIFont *dateFont = [UIFont fontWithName:@"Helvetica" size:14];
CGSize dateStringSize = [dateString sizeWithFont:dateFont constrainedToSize:maximumSize lineBreakMode:_passport_useZone.lineBreakMode];
CGRect dateFrame = CGRectMake(5, _passport_money.bottom, scrollView.width - 10, dateStringSize.height);
_passport_useZone.frame = dateFrame;
/*iOS7之后*
CGSize maximumSize = CGSizeMake(300, 9999);
NSString *dateString = @"The date today is January 1st, 1999";
UIFont *dateFont = [UIFont fontWithName:@"Helvetica" size:14];
NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:dateFont,NSFontAttributeName,nil];
CGSize dateStringSize = [dateString boundingRectWithSize:maximumSize options:NSStringDrawingUsesFontLeading attributes:tdic context:nil].size;
CGRect dateFrame = CGRectMake(10, 10, 300, dateStringSize.height);
ticketsValidTime.frame = dateFrame;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment