UITextView框架并不关心其内容的属性

时间:2016-12-04 14:09:59

标签: swift attributes uitextview frame

我有一个带有belongsText的UITextView(包含font& lineSpacing以使线条靠近)。 我使用下面的代码来确定textView的框架。

问题是frame.height太大了,似乎并不关心lineSpacing。它看起来像没有特定lineSpacing的textView的框架。

有没有人有想法解决这个失败?我错过了什么吗?

//Attributes of textView         
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = -1.25

let textViewText = Data().text   

let attributedText = NSMutableAttributedString(string: textViewText, attributes: [NSFontAttributeName: UIFont(name: "AvenirNext-Medium", size: 14)!])   
attributedText.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSRange(location: 0, length: attributedText.string.characters.count))

textView.attributedText = attributedText

...

//Determining the frame of textView in other method
let textViewText = Data().text    

let paragraphStyle = NSMutableParagraphStyle()            
paragraphStyle.lineSpacing = -1.25

let rect = NSString(string: textViewText).boundingRect(with: CGSize(width: view.bounds.size.width, height: view.bounds.size.height), 
                    options: NSStringDrawingOptions.usesFontLeading.union(NSStringDrawingOptions.usesLineFragmentOrigin), 
                    attributes: [NSFontAttributeName: UIFont(name: "AvenirNext-Medium", size: 14)!, NSParagraphStyleAttributeName: paragraphStyle],
                    context: nil)

0 个答案:

没有答案