ios UILabel 自动换行/字符换行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8355175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:43:11  来源:igfitidea点击:

UILabel Word Wrap / Character Wrap

objective-cioscocoa-touchuilabel

提问by Skoota

I have a UILabel with the lineBreakModeset to UILineBreakModeWordWrap. This works fine, except when I have a long slab of text with no spaces. In this case it does not wrap the long slab of text but instead just cuts it off once it reaches the right-hand end of the UILabel frame. How can I tell the UILabel that it should wrap on a character boundary in this situation only(essentially equivalent to the UILineBreakModeCharacterWrapsetting, but only for those long slabs of spaceless text).

我有一个lineBreakMode设置为UILineBreakModeWordWrap. 这很好用,除非我有一大段没有空格的文本。在这种情况下,它不会包裹长文本块,而是在到达 UILabel 框架的右侧时将其切断。我怎么能告诉的UILabel,它应该在这种情况下一个字符边界上的包裹(实质上相当于UILineBreakModeCharacterWrap设置,但仅限于spaceless文本的那些漫长的砖)。

Thanks in advance!

提前致谢!

回答by Skoota

For anyone else having this same issue, I ended-up solving it by using a UITextViewinstead of a UILabel. This was the best solution for two reasons:

对于遇到同样问题的其他人,我最终通过使用 aUITextView而不是UILabel. 这是最好的解决方案,原因有二:

  1. It doesn't require any custom behaviour to determine whether the text contains spaces and change the line break mode of the UILabelto/from word/character wrap.

  2. More importantly, there is an edge case whereby you may have normal words (that you want to wrap on word boundaries) plus extra long text (which you need to wrap on character boundaries). Short of writing some kind of logic to insert a space into that extra long text (at the correct position) to force a "fake word wrap" I can't see any way to handle wrapping on words and characters, depending on the situation, within the one UILabel. The UITextViewhandles this situation automatically, breaking on word boundaries or character boundaries as necessary.

  1. 它不需要任何自定义行为来确定文本是否包含空格并更改UILabelto/from 单词/字符换行的换行符模式。

  2. 更重要的是,存在一种边缘情况,即您可能有普通单词(您想在单词边界上换行)加上超长文本(您需要在字符边界上换行)。没有编写某种逻辑来在超长文本中插入一个空格(在正确的位置)以强制“假换行”,我看不出有任何方法可以根据情况处理单词和字符的换行,内一UILabel。该UITextView自动处理这种情况,上破字边界或字符边界是必要的。

For specifics on how I am doing this, I have a one line UITextViewwith editing and scrolling disabled. I also set the .contentInsetto remove the padding making it look (to the unsuspecting eye) just like a UILabel. I am then using the sizeWithFont:constrainedToSize:lineBreakMode:method to determine the frame of the rendered text, and adjusting the frame of the UITextViewaccordingly so that it exactly fits the text.

有关我如何执行此操作的详细信息,我有一行UITextView禁用了编辑和滚动。我还设置了.contentInset删除填充,使其看起来(在毫无戒心的眼中)就像UILabel. 然后我使用该sizeWithFont:constrainedToSize:lineBreakMode:方法来确定渲染文本的框架,并相应地调整 的框架UITextView,使其完全适合文本。

Hope that this helps!

希望这会有所帮助!

回答by Srikar Appalaraju

One way to do this is to set the Lines property in IB.

一种方法是在 IB 中设置 Lines 属性。

enter image description here

在此处输入图片说明

or from code do this -

或从代码做这个 -

textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 3;

So when you set the number of lines as 3 the text wraps till that many lines are occupied.

因此,当您将行数设置为 3 时,文本会自动换行,直到占用了许多行。

回答by sElanthiraiyan

dean is right. If you want it you have to do it manually. The below will code will wordwrap a label even though it doesn't spaces. This may help

院长是对的。如果你想要它,你必须手动完成。即使没有空格,下面的代码也会对标签进行自动换行。这可能有帮助

         NSString *someText = yourLabel.text;

    //Check if the text contains spaces    
//The method in the below if condition is user defined and you have to define one. lol
        if(![self textContainsSpaces:someText])
        {
        //Do the word wrap manually
            CGSize constraintSize;

            constraintSize.width = 165;

            constraintSize.height = 165;

            CGSize stringSize =[someText sizeWithFont: [UIFont boldSystemFontOfSize: 17] constrainedToSize: constraintSize lineBreakMode: UILineBreakModeWordWrap];

            CGRect rect = CGRectMake(yourLabel.frame.origin.x, yourLabel.frame.origin.y, 165, (stringSize.height+10));

            yourLabel.frame = rect;
        }

回答by deanWombourne

You have to do it yourself, there's no option for 'use word wrapping except when I don't want you to' :)

你必须自己做,没有选项可以使用自动换行,除非我不希望你使用自动换行' :)

If a word was too long you could insert spaces into it before you display it to help the label know where to wrap?

如果一个单词太长,您可以在显示之前插入空格以帮助标签知道在哪里换行?