简体   繁体   English

将用户输入限制为固定的宽度和高度内容

[英]Restrict user input to fixed width and height contenteditable div

After a lot of research, I haven't found a post with exactly the same requirements so I thought write a new post. 经过大量研究,我没有找到具有完全相同要求的帖子,因此我想写一篇新帖子。

I'm trying to create a fixed area (eg 200px by 300px) where the user can enter text input. 我正在尝试创建一个固定区域(例如200px x 300px),用户可以在其中输入文本。 He should be able to enter any character (including line breaks). 他应该能够输入任何字符(包括换行符)。

However, he should not be able to 'write outside the box' (ie there shouldn't be overflow scroll or hidden for the 200x300 area). 但是,他应该不能“在框外书写”(即200x300区域不应有溢出滚动或隐藏)。

Once user reaches the 'bottom' of the area, they can't enter any more line breaks. 一旦用户到达该区域的“底部”,他们将无法再输入换行符。

And once they reach the 'bottom right' corner of the 200x300 area, they shouldn't be able to enter any more characters at all. 并且一旦到达200x300区域的“右下角”,他们就根本不能再输入任何字符。

Is this possible in css, angular, js, jquery, etc? 这可能在CSS,Angular,js,jquery等中吗?

Limit the length of characters with base in font and div's size, but you must change the font size and family or line height because every browser can have different styles. 以字体和div的大小为基础限制字符的长度,但是您必须更改字体大小和字体或行高,因为每种浏览器可以使用不同的样式。

To limit the length of characters in the div is need to ignore the HTML tags in the content, like interpreting. 为了限制div中字符的长度,需要像解释一样忽略内容中的HTML标签。

Firstly calculate how many characters fits there. 首先计算在那里适合容纳多少个字符。

You can restrict the number of characters per line with the cols="" attribute and set the displayed the number of editable lines with the rows="" attribute. 您可以使用cols =“”属性限制每行的字符数,并使用rows =“”属性设置显示的可编辑行数。 However limiting the number of rows could only be one with the maxlength attribute which would control the number of characters you can have, which you'd have to estimate. 但是,限制行数只能是具有maxlength属性的行,该属性将控制您可以估计的字符数。 There are some hacks to limit the number of rows with event listeners , but they seem to have fairly major bugs. 有一些技巧可以限制事件监听器的行数 ,但是它们似乎有相当大的错误。

It is possible, you just need to do following: 有可能,您只需要执行以下操作:

  • Use event handlers to control character input process. 使用事件处理程序来控制字符输入过程。 It is required to be able to stop processing further keystrokes when limit is reached. 当达到限制时,必须能够停止处理其他按键。 Use keypress and keydown , first handles character processing, second - control keys processing. 使用keypresskeydown ,第一个处理字符处理,第二个-控制键处理。
  • Each time user presses a key, use a separate buffer to produce final result, compute its bounding rectangle, and if it is bigger than limit, prevent event handling. 每次用户按下一个键时,都使用一个单独的缓冲区产生最终结果,计算其边界矩形,如果该矩形大于限制,则防止事件处理。
  • Height of text body could be calculated by multiplying number of lines by line height (interpret font-size and line-height CSS properties). 可以通过将行数乘以行高来计算文本主体的高度(解释font-sizeline-height CSS属性)。
  • Width of text body could be computed rather easy with help of HTML5 canvas measureText method. 借助HTML5 canvas measureText方法,可以很容易地计算文本主体的宽度。
  • If you don't have canvas, you can use offscreen span (or any other inline ) element - just fill innerHTML with text block and use its offsetWidth attribute. 如果没有画布,则可以使用屏幕外span (或任何其他inline )元素-只需用文本块填充innerHTML并使用其offsetWidth属性即可。 Actually, if you replace line break characters with <br> , you may use span approach to get both dimensions in one go - just make sure it has same style as editable container. 实际上,如果将换行符替换为<br> ,则可以使用span方法一次性获得两个尺寸-只需确保其样式与可编辑容器相同即可。

ContentEditable containers, as i remember, store text body in HTML format already (in other words - with <br> s instead of line break characters). 我记得,ContentEditable容器已经以HTML格式存储了文本主体(换句话说,用<br>而不是换行符)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM