// ViewController.m
// 计算文字宽高
//
// Created by Admin on 2019/7/13.
// Copyright © 2019年 JZY. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//定义字体
NSDictionary *attrs = @{NSFontAttributeName :[UIFont systemFontOfSize:14]};
//定义这个控件文字最大的宽度和高度,这里定义了可以无限宽和高
CGSize maxsizes=CGSizeMake(200, 200);
UILabel *texts;
texts.text=@"阿瑟费好的咖啡好";
//textzise即为当前控件文字的所占宽高
CGSize textzise= [texts.text boundingRectWithSize:maxsizes options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
NSLog(@"%f--%f",textzise.width,textzise.height);
// Do any additional setup after loading the view, typically from a nib.
CGFloat a = [self getLabelHeightWithText:@"阿瑟费好的咖啡好" width:200 font:14];
NSLog(@"getLabelHeightWithText: %f",a);
}
- (CGFloat)getLabelHeightWithText:(NSString *)text width:(CGFloat)width font: (CGFloat)font{
CGRect rect = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil];
return rect.size.height;
}
@end
网友评论