在UILabel中,是否可以强行不要在某个地方打破一条线 [英] In a UILabel, is it possible to force a line NOT to break in a certain place

查看:177
本文介绍了在UILabel中,是否可以强行不要在某个地方打破一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应该是两行长的UILabel。文本已本地化为法语和英语。

I have a UILabel that is supposed to be two lines long. The text is localized into French and English.

我正在设置标签的attributedText属性。该文本由三个附加的字符串构成,例如textFragment1,textFragment2和textFragment3。中间字符串实际上是使用NSTextAttachment创建的图像。

I'm setting the attributedText property of the label. The text is constructed from three appended strings, say textFragment1, textFragment2 and textFragment3. The middle string is actually an image created with an NSTextAttachment.

我想确保textFragment2和textFragment3在同一行。第一个字符串可能会或可能不会换行,具体取决于它的长度。问题是我的法语文本目前相当短,所以该行在textFragment2之后换行。

I want to make sure that textFragment2 and textFragment3 are on the same line. The first string may or may not wrap depending on how long it is. The problem is that my French text is currently fairly short, so the line is wrapping after textFragment2.

我通过在本地化的法语中添加换行符来暂时修复此问题textFragment1的文本。我不喜欢这个解决方案。我想知道是否有办法处理textFragment2和textFragment3,以便它们总是在同一行上。

I have fixed this temporarily by adding a line break symbol in the localized French text for textFragment1. I don't really love this solution though. I was wondering if there is a way to treat textFragment2 and textFragment3 so that they will always be together on the same line.

推荐答案

您可以使用不间断空格( \ u00a0 )来连接textFragment2和textFragment3。这个角色看起来就像一个普通的空间 - 即。它导致相同数量的空白 - 但不会在它的任何一侧发生断线。

You could use a non-breaking space (\u00a0) to join textFragment2 and textFragment3. This character looks just like a normal space—i.e. it results in the same amount of whitespace—but line breaking will not take place on either side of it.

你也可以使用零宽度空间( \\\⁠ )。使用此字符不会产生任何空格,但仍会阻止任何一方的断行。如果您不希望textFragment2和textFragment3之间有任何空格,但您仍希望防止在那里进行换行,那么这就是您想要的。 (如果你的中间有一个带连字符的单词,但是你想要阻止连字符后的行被破坏,这也很有用。)

You could also use a zero-width space (\u2060). Using this character will not result in any whitespace, but it will still prevent line breaking on either side. This is what you want if you don’t want any space between textFragment2 and textFragment3 but you still want to prevent line breaking there. (It’s also useful if you have a word with a hyphen in the middle of it but you want to prevent the line from being broken after the hyphen.)

你可以阅读更多关于维基百科上的这些角色。

You can read more about these kinds of characters on Wikipedia.

这篇关于在UILabel中,是否可以强行不要在某个地方打破一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆