听说有个constraint的方法,但是不知道具体怎么操作,应该写在那个方法里(个人觉得应该写在textViewChanged:里边),不知道有哪位大人写过类似的程序,求指导!
要根据输入的文字动态改变宽和高,比如输入一个字母,宽增加一个位置,按回车,高增加一行,要求宽高都是动态改变的。

解决方案 »

  1.   

    需要根据字符串先计算出 包含 文本的 rect。
    参考下一下几个方法:// Wrapping to fit horizontal and vertical size. Text will be wrapped and truncated using the NSLineBreakMode. If the height is less than a line of text, it may return
    // a vertical size that is bigger than the one passed in.
    // If you size your text using the constrainedToSize: methods below, you should draw the text using the drawInRect: methods using the same line break mode for consistency
    - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size; // Uses NSLineBreakModeWordWrap
    - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode; /