1. 换行符\n
UILabel *tips = [[UILabel alloc]initWithFrame:CGRectMake(50, 200, 300 - 20, 60)];
[tips setTextColor:[UIColor grayColor]];
[tips setText:@"支付密码必须为6位数字组合。\n您可依次进入 '功能列表' -> '安全中心' 修改支付密码。\n好的"];
[tips setFont:[UIFont boldSystemFontOfSize:12]];
tips.textAlignment = NSTextAlignmentCenter;
tips.numberOfLines = 0; // 关键一句
[self.view addSubview:tips];
2. 根据字体内容计算frame
UILabel *starLab=[[UILabel alloc]init];
starLab.text =@"打到小日本狗,中华人名共和国万岁!!!";
starLab.textAlignment =NSTextAlignmentLeft;
starLab.numberOfLines = 0;
starLab.font = [UIFont systemFontOfSize:20];
CGSize titleSize = [starLab.text boundingRectWithSize: CGSizeMake(100, MAXFLOAT) options: NSStringDrawingUsesLineFragmentOrigin attributes: @{NSFontAttributeName: [UIFont systemFontOfSize:20]} context: nil].size;
[starLab setFrame:CGRectMake(50, 200, titleSize.width, titleSize.height)];
starLab.textColor =[UIColor blackColor];
[self.view addSubview:starLab];
网友评论