UILabel设置行间距

OC:

- (void)viewDidLoad

{

[super viewDidLoad];//Do any additional setup after loading the view.//UILabelUILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(50,50, 300,300)];

label.backgroundColor=[UIColor orangeColor];

label.text=@"巴亚是一座古老的罗马城市,是精英、富人和名人的聚集地。它的吸引力部分来自于它的热温泉,一系列的火山口为这里提供了固定的热水源。8世纪时萨拉森人洗劫了这座城市,从此以后再也没能恢复繁荣,并且最终在16世纪被人们遗弃。埃及皇后的宫殿:古老亚历山大港的部分建筑位于海底,而且这座拥有2000年历史的古城数十年里一直都是考古学家挖掘的目标。自从一场地震让这些建筑滑进海底之后,考古学家们就在世代保护着这里。";

label.numberOfLines=0;

[self.view addSubview:label];//设置行间距NSMutableParagraphStyle *paragraphStyle =[[NSMutableParagraphStyle alloc]init];

paragraphStyle.lineSpacing=20;

NSDictionary*attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:14], NSParagraphStyleAttributeName:paragraphStyle};

label.attributedText=[[NSAttributedString alloc]initWithString:label.text attributes:attributes];

}



swift:

let tipsLabel = UILabel(fontSize: 12, textColor: UIColor.init(hexColorString: "#e93a38"), text: "资质信息修改后账号需重新审核将无法正常登陆,预计3个工作日内审核完成,请谨慎提交。")

tipsLabel.numberOfLines = 2

//设置行间距

let paragraphStyle = NSMutableParagraphStyle()

paragraphStyle.lineSpacing = 7

let attributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 12),NSAttributedStringKey.paragraphStyle:paragraphStyle,NSAttributedStringKey.foregroundColor:UIColor.init(hexColorString: "#e93a38")]

tipsLabel.attributedText = NSAttributedString(string: tipsLabel.text!, attributes: attributes)

你可能感兴趣的:(UILabel设置行间距)