【发布时间】:2015-01-25 15:23:34
【问题描述】:
UITextView 有类似editingrectforbounds 的属性吗?!
我需要限制文字CGsize
【问题讨论】:
标签: swift uitextview cgsize
UITextView 有类似editingrectforbounds 的属性吗?!
我需要限制文字CGsize
【问题讨论】:
标签: swift uitextview cgsize
我相信你想要的是NSTextContainer。我推荐阅读the Apple Guide for Text Layout,它详细解释了如何进行各种类型的文本布局以及所涉及的 Cocoa API。还有一个苹果指南Text Handling in iOS
var txtView = UITextView(frame:CGRect(), textContainer:NSTextContainer())
或者如果它是一个现有的 UITextView,你可以设置一个新的文本容器。
txtView.textContainer = NSTextContainer(NSMakeSize(width, height))
【讨论】:
UITextedField 的大小,UITextView 也需要同样的代码:func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { let oldString = textField.text as NSString let newString = oldString.stringByReplacingCharactersInRange(range, withString: string) as NSString let stringSize = newString.sizeWithAttributes([NSFontAttributeName:textField.font]) return stringSize.width < textField.editingRectForBounds(textField.bounds).size.width }