---
title: "swift -> UILable  计算高度， 行间距"
author: "mft8899"
date: "2018-11-13T15:37:25Z"
tags: []
categories: ["移动开发"]
slug: "swift-uilable"
draft: false
lastmod: "2018-11-13T15:37:25Z"
id: 2433758
url: "https://mft.iteye.com/blog/2433758"
---

UILabel 计算 高度，行间距

 

```
let str:String = "斯蒂芬斯蒂芬斯蒂芬斯蒂芬";

var attrButes:[NSAttributedStringKey : Any]! = nil;
let paraph = NSMutableParagraphStyle()
paraph.lineSpacing = 20
attrButes = [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 13.5),NSAttributedStringKey.paragraphStyle:paraph]

let size:CGRect = str.boundingRect(with: CGSize(width: 200, height: CGFloat.greatestFiniteMagnitude), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: attrButes, context: nil)

content.attributedText = NSAttributedString(string: str, attributes: attrButes)
content.numberOfLines = 0
content.sizeToFit()
```