iOS 设置label的行间距教程

 时间:2024-10-12 07:43:48

1、创建一个普通的label用来对比效果 UILabel *label = [[UILabel alloc]init]; label.frame=CGRectMake(10, 50, 70, 60); label.text=@"修改前\n行间距"; label.numberOfLines=2; label.backgroundColor=[UIColor grayColor]; label.textColor=[UIColor blackColor]; [self.view addSubview:label];

iOS 设置label的行间距教程

2、 //方式一 UILabel *label2 = [[UILabel alloc]init]; label2.frame=CGRectMake(100, 50, 70, 60); label2.text=@"修改后\n行间距"; label2.numberOfLines=2; label2.backgroundColor=[UIColor grayColor]; label2.textColor=[UIColor blackColor]; [label2 setValue:@(25) forKey:@"lineSpacing"]; [self.view addSubview:label2];

iOS 设置label的行间距教程

3、 //方式二 UILabel *label3 = [[UILabel alloc]init]; label3.frame=CGRectMake(200, 50, 70, 60); label3.text=@"修改后\n行间距"; label3.numberOfLines=2; label3.backgroundColor=[UIColor grayColor]; label3.textColor=[UIColor blackColor]; [self.view addSubview:label3]; //通过修改文本属性 NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc] initWithString:label3.text]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; [paragraphStyle setLineSpacing:10];//设置距离 [attriString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [label3.text length])]; label3.attributedText = attriString;

iOS 设置label的行间距教程

4、方式一和方式二均可以调整行间距,但通过修改文本属性调整行间距效果更好些

iOS 设置label的行间距教程
  • 华为视频历史记录中怎么过滤短视频?
  • 部落战争5本最佳布局部落冲突5本神阵
  • 金币云商如何注册
  • 如何处理破碎的玻璃水银温度计
  • 福昕PDF编辑器怎么放大书签文字
  • 热门搜索
    有眼不识泰山是什么意思 一触即发的意思 sakura是什么意思 小米手环支持什么手机 尽力局是什么意思 惊心动魄的意思是什么 嘉是什么意思 糟糠之妻是什么意思 毕业祝福语 托福考试带什么