Detect linebreak in UITextView

可紊 提交于 2020-01-02 14:15:40

问题


Is there a way to detect line breaks occurred by the user or when the textview automatically changes line. I am going to change the height of the textview depending on the number of lines.

Any help appreciated.


回答1:


You can use the method proposed in this answer to find the occurrences of @"\n", the newline character. You can see if the string change using the textViewDidChange: protocol method (apple doc here).

If you want the heigh, directly, look at this answer. I think it can help.




回答2:


You can get the number of rows which is actually what i wanted to do.

numLines = textView.contentSize.height/textView.font.leading;

put this code into textViewDidChange:



来源:https://stackoverflow.com/questions/9252404/detect-linebreak-in-uitextview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!