【问题标题】:UITextView editingrectforboundsUITextView 编辑rectforbounds
【发布时间】:2015-01-25 15:23:34
【问题描述】:

UITextView 有类似editingrectforbounds 的属性吗?! 我需要限制文字CGsize

【问题讨论】:

    标签: swift uitextview cgsize


    【解决方案1】:

    我相信你想要的是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 }
    • 是的,这对任何人都不起作用。您需要更新您的问题,而不是将代码发布到评论中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    • 2013-03-28
    相关资源
    最近更新 更多