代码:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self addView]; } #pragma -mark -functions //初始化界面 -(void)addView { //文字 UILabel *dataLabel = [[UILabel alloc] init]; dataLabel.backgroundColor = [UIColor orangeColor]; dataLabel.text = @"我是文字文字文字文字文字文字文字文字文字文字文字111111111111111111111111111111111112222222222222333333333"; dataLabel.textColor = [UIColor redColor]; dataLabel.font = [UIFont fontWithName:@"Arial" size:14]; dataLabel.numberOfLines = 0; [self.view addSubview:dataLabel]; //富文本设置文字行间距 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init]; paragraphStyle.lineSpacing = 4; NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:14], NSParagraphStyleAttributeName:paragraphStyle}; dataLabel.attributedText = [[NSAttributedString alloc]initWithString:dataLabel.text attributes:attributes]; //获取设置文本间距以后的高度 CGRect fram = [dataLabel.attributedText boundingRectWithSize:CGSizeMake(210, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil]; dataLabel.frame=CGRectMake(50, 100, 210, fram.size.height); }
时间: 2024-10-11 06:06:15