ios - In UITextView, how to get the point that next input will begin another line -
i'm using uitextview in ios, , moment/point next input text begin new line.
how can this?
-----editted-------
----after long time research, found below ok, seems little complicated.
//limit text within text box nsstring *originalstr = textview.text; textview.text = [textview.text stringbyappendingstring:text]; cgfloat lineheight = textview.font.lineheight; int currentlines = textview.contentsize.height / lineheight; int maxlines = textview.frame.size.height/lineheight; if (currentlines > maxlines) { nsstring * firsthalfstring = [originalstr substringtoindex:range.location]; nsstring * secondhalfstring = [originalstr substringfromindex: range.location]; textview.text = [nsstring stringwithformat: @"%@%@%@", firsthalfstring, text, secondhalfstring]; int timeout = 0; while (true) { timeout =50; textview.text = [textview.text substringtoindex:(textview.text.length -1)]; int newcurrentlines = textview.contentsize.height / lineheight; if ((newcurrentlines == maxlines) || (timeout > 50)) break; } textview.selectedrange = range; return false; } else { textview.text = originalstr; textview.selectedrange = range; return true; }
in delegate method of uitextview, code count number of lines each time text field changed :
-(void)textviewdidchange:(uitextview *)textview { uifont *font = [uifont boldsystemfontofsize:11.0]; cgsize size = [string sizewithfont:font constrainedtosize:myuitextview.frame.size linebreakmode:uilinebreakmodewordwrap]; // default mode float numberoflines = size.height / font.lineheight; }
Comments
Post a Comment