Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

根据字数自适应高度的UILabel | 东隅已逝/桑榆非晚 #26

Open
h1z3y3 opened this issue Apr 23, 2021 · 0 comments
Open

根据字数自适应高度的UILabel | 东隅已逝/桑榆非晚 #26

h1z3y3 opened this issue Apr 23, 2021 · 0 comments

Comments

@h1z3y3
Copy link
Owner

h1z3y3 commented Apr 23, 2021

https://h1z3y3.me/posts/uilabel-with-height-fixed/

  • (UILabel *) getLabelHeightFixedWithText: (NSString *) text { UIFont *font = [UIFont boldSystemFontOfSize: 12.0f]; int width = 225, height = 10000; NSMutableDictionary *attrs = [[NSMutableDictionary alloc] init]; [attrs setObject: font forKey: NSFontAttributeName]; CGRect size = [text boundingRectWithSize:CGSizeMake(width, height) options: NSStringDrawingUsesLineFragmentOrigin attributes: attrs context: nil]; UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, size.size.width, size.size.height)]; label.numberOfLines = 0;//一定要设置行数为0 label.font = font; label.lineBreakMode = NSLineBreakByWordWrapping; label.text = (text ? text : @""); label.backgroundColor = [UIColor clearColor]; label.textColor = [UIColor blackColor]; return label; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant