首页 \ 问答 \ 内联换行符?(Inline line breaks?)

内联换行符?(Inline line breaks?)

这是很多需要阅读的内容,但如果你全部阅读,这应该是有道理的。

我正在研究一个需要用户在iOS上突出显示文本的应用程序。 文本被分成段落,换行符和段落符号,并且我决定只使用<br />标签作为换行符,并为每个段落启动一个新的p元素。 移动Safari的问题在于,如果您选择的元素不是内嵌的,则无法单独选择字母。 相反,试图突出显示文本会突出显示该段的整个块,因为它显示的是块。

为了解决这个问题,我决定用一个内联元素替换所有换行符( \n ),如下所示:

.space{
    height:0px;
    display:inline-block;
    width:100%;
}

所以HTML看起来像:

Line one
<span class="space"></span>
Line two

并输出为:

第一行

第二行

我想,“干得好,你明白了!”


跳到今天,我发现这不是我想要的行为。 由于用户突出显示的文本来自处理为纯文本的PDF文件,因此会出现如下情况:

Hello there, this is a paragraph and\n 
it is continuing onto the next line and this line will continue and be wrapped by the set width of the parent

我将如何处理

Hello there, this is a paragraph and
<span class="space"></span> 
it is continuing onto the next line and this line will continue and be wrapped by the set width of the parent

这会输出为

你好,这是一段和

它将继续到下一行,并且此行将继续并由父级的设置宽度包装

这显然不好! 现在有一个全新的“段落中断”,应该有一个“换行”。 所以,我想我会做两个不同的内联“休息”类,一个用于单个空间,另一个用于双空间。

双重空间元素将充当上面那个,而单个空间元素只是将内容移动到下一行。 所以,这让我想起了我的实际问题:

如何使用内联定位元素将文本移动到下一行?

另外,我不能将文本包裹在另一个元素中,例如span ,所以我只能使用CSS来制作内联换行元素和段落元素。


有几件事我试图让它起作用。 与其将单个空间元素的width设置为100%就像我使用双空格元素那样),我可以改为计算容器的宽度和文本占用的宽度,然后减去两个元素的宽度剩余空间。 这将使我能够将内容推向新的界限。

你可以在这里测试这个方法: http : //jsfiddle.net/charlescarver/ksvkk/12/

这种方法的问题是,虽然我可以确定宽度,但我无法确定它是否适用于多行文本节点。 另外,如果容器大小发生变化,这不是很灵活。

我有一个可能的想法,但无法实现,就是让单个空间元素具有100% width ,并且将文本推入,以便将新行压入下一行。 就像我说的,我无法让它工作。


This is a lot to read, but if you read it all, it should make sense.

I am working on an app that requires users to highlight text on iOS. The text is separated into paragraphs, with line breaks and paragraph breaks, and I decided I would simply use <br /> tags for the line breaks, and start a new p element for each paragraph. The problem with Mobile Safari is that you can't individually select letters when the element you are selecting from is not inline. Instead, trying to highlight text would highlight the entire chunk of the paragraph, as it was displayed block..

To combat this, I decided I would replace all new-line characters (\n) with an inline element like so:

.space{
    height:0px;
    display:inline-block;
    width:100%;
}

So the HTML would look like:

Line one
<span class="space"></span>
Line two

And would output as:

Line One

Line Two

I thought, "Good job, you figured it out!"


Skip to today, where I found out that this is not the behavior I want. Since the text that the user is highlighting comes from a PDF file which is processed into plain text, there would be occurrences like this:

Hello there, this is a paragraph and\n 
it is continuing onto the next line and this line will continue and be wrapped by the set width of the parent

Which I would process as

Hello there, this is a paragraph and
<span class="space"></span> 
it is continuing onto the next line and this line will continue and be wrapped by the set width of the parent

Which would output as

Hello there, this is a paragraph and

it is continuing onto the next line and this line will continue and be wrapped by the set width of the parent

That's obviously not good! Now there's a whole new "paragraph-break" where there should be a "line-break". So, I figured I would make two different inline "break" classes, one for a single space, and one for a double space.

The double space element would act as the one above is, while the single space element would simply move the content onto the next line. So, this brings me to my actual question:

How can I move text onto the next line using an inline-positioned element?

Also, I cannot wrap the text in another element, such as span, so I can only use CSS to make an inline line break element and paragraph element.


There are a couple of things that I have tried to get this to work. Instead of setting the width of the single space element to 100% like I do with the double space element, I could instead calculate the width of the container and the width that the text takes up, and subtract the two, getting the width of the remaining space. This would allow me to push the content to the new line.

You can test this method here: http://jsfiddle.net/charlescarver/ksvkk/12/

Problem with this method is that while I could determine the width, I couldn't determine it for multi-line text nodes. Also, this isn't flexible if the container size changes.

A possible idea that I had but couldn't get to work was to have the single space element have a 100% width, and have the text push it so that it would push the newline down to the next line. Like I said, I couldn't get that to work.


原文:https://stackoverflow.com/questions/16754375
更新时间:2023-06-21 19:06

最满意答案

我们可以用一个简单的例子来理解它,我们在声明下面写下,

const int * p;

它说* p是不变的p。 p可以存储指向常量整数的任何地址。

同样,const int ** p;

它指向指向常量int的指针。 你可以改变p和* p而不是** p。 * P可以容纳任何地址指向你的情况下的&n的整数,你可以用指向常量整数的地址来改变* P。

希望它解释你更多:: :)


It helps to understand it with a simple example. When you write the declaration below:

const int *p;

It says *p is constant not p. p can store any address pointing to constant integer.

Similarly const int **p; says pointer to pointer to a constant int. You can change p and *p but not **p. *p can hold any address pointing to constant integer like &n in your case and you can change *P with any addresses pointing to constant integer.

相关问答

更多
  • 如果你想要一个字符列表(一个字),你可以使用char *word 如果你想要一个单词列表(一个句子),你可以使用char **sentence 如果你想要一个句子列表(一个独白),你可以使用char ***monologue 如果你想要一个单独的列表(传记),你可以使用char ****biography 如果您想要传单(生物图书馆),您可以使用char *****biolibrary 如果你想要一个生物库列表(一个哈尔),你可以使用char ******lol ... ... 是的,我知道这些可能不是最好 ...
  • const是一个工具,您应该使用它来追求非常重要的C ++概念: 通过让编译器强制执行你的意思,在编译时找到错误,而不是运行时。 即使它不改变功能,当你做的事情你不是要做的时候,添加const产生一个编译错误。 想像下列打字错误: void foo(int* ptr) { ptr = 0;// oops, I meant *ptr = 0 } 如果使用int* const ,则会产生编译器错误,因为您将值更改为ptr 。 通过语法添加限制是一件好事。 只是不要把它太远 - 你给出的例子是一个大多数 ...
  • 在大多数情况下,它不再有什么区别。 通常,编译器将能够推断变量实际上是只读的,并且在优化期间将其视为常量。 无论哪种方式,你通常使用const处理像const char *string这样的事情,以避免意外修改参数。 由于所有参数都是按值传递的,所以这不是一个整数不可能发生的事情。 只有指针指针本身就是值。 实际上, const char *string也不完全是常量。 只有指针指向的char是,但指针作为变量不是。 它只会在将其指定为const char * const string时变成。 现在, st ...
  • 我认为你混合了两个不同的概念。 如果你有两个指针p和q的任何类型,那么p == q 总是比较存储在指针中的地址而不是指向的对象。 结果,声明 if (p == q) 测试p和q是否字面上指向同一个对象。 至于为什么要返回相同的对象 - 许多编译器作为优化,将所有相同值的字符串文字汇集到一个字符串文字中,并设置所有指针以共享它。 此优化可节省二进制文件中的空间。 这也是写入字符串文字的未定义行为的原因 - 如果您可以写入字符串文字,那么您可能会意外地污染初始化为该字面值的其他字符串。 独立地,流库是专门编 ...
  • 正如你将会看到的那样,你存储的指针对访问问题没有帮助,但是当你复制一个对象时,它们会引用错误实例中的数据,除非你负责复制。 顶层const防止为类实例分配。 即指针是有问题的,并没有任何优势。 相反,请这样做: int data() const { return n_; } void set_data( int const value ) { n_ = value; } 或者你也可以像在标准库中那样做,并且只将data命名为setter,但命令形式在调用代码中更具可读性。 这种方法的一个 ...
  • 我们可以用一个简单的例子来理解它,我们在声明下面写下, const int * p; 它说* p是不变的p。 p可以存储指向常量整数的任何地址。 同样,const int ** p; 它指向指向常量int的指针。 你可以改变p和* p而不是** p。 * P可以容纳任何地址指向你的情况下的&n的整数,你可以用指向常量整数的地址来改变* P。 希望它解释你更多:: :) It helps to understand it with a simple example. When you write the de ...
  • 从C11标准(草案N1570) : 6.7.3类型限定符 句法 类型限定符: const restrict volatile _Atomic [...] 语义: 与限定类型关联的属性仅对作为左值的表达式有意义。 [...] 例1 声明了一个对象 extern const volatile int real_time_clock; 可以由硬件修改,但不能分配,递增或递减。 简单来说: const并不意味着值永远不会改变。 这只意味着你不能改变它1 。 对于被调用者来说 , const是一种限制,而不是一种承 ...
  • const有两个地方可以去: T* p1; // non-const pointer, non-const object T const* p2; // non-const pointer, const object T* const p3; // const pointer, non-const object T const* const p4; // const pointer, const object 只需从右 ...
  • 之前 const char * const g_someString = "Hello"; 你需要将它声明为extern (例如通过包含头部),因为const命名空间级别变量默认具有内部链接。 也就是说,您可以在标题中定义字符串。 单独的编译为您提供了修改字符串而不会导致重建大量文件的能力,但除此之外,恕我直言,这是过早的优化。 要使标题中的字符串定义inline函数正式安全,如果需要,您需要字符串(或至少指针)具有extern链接。 一种方法是在One Definition Rule中利用模板的特殊豁免 ...
  • 第二个例子中的const适用于int ,即你有一个指向const int的非const指针。 由于C和C ++中的类型是从右向左读取的,因此将const始终放在右边实际上是最简单的。 它也是唯一可以始终如一地放置的地方: int i(0); int * p0(&i); // non-const pointer to non-const int int const* p1(&i); // non-const pointer to const int int * co ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)