简体   繁体   English

UILabel Word Wrap / Character Wrap

[英]UILabel Word Wrap / Character Wrap

I have a UILabel with the lineBreakMode set to UILineBreakModeWordWrap . 我有一个UILabel, lineBreakMode设置为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. 在这种情况下,它不会包裹文本的长板,而是在它到达UILabel框架的右端时切断它。 How can I tell the UILabel that it should wrap on a character boundary in this situation only (essentially equivalent to the UILineBreakModeCharacterWrap setting, but only for those long slabs of spaceless text). 我怎么能告诉的UILabel,它应该包裹在这种情况下 (实质上相当于一个字符边界上UILineBreakModeCharacterWrap设置,但仅限于spaceless文本的那些漫长的砖)。

Thanks in advance! 提前致谢!

For anyone else having this same issue, I ended-up solving it by using a UITextView instead of a UILabel . 对于其他有同样问题的人,我最终通过使用UITextView而不是UILabel来解决它。 This was the best solution for two reasons: 这是最好的解决方案,原因有两个:

  1. It doesn't require any custom behaviour to determine whether the text contains spaces and change the line break mode of the UILabel to/from word/character wrap. 它不需要任何自定义行为来确定文本是否包含空格并将UILabel换行模式更改为/从单词/字符换行。

  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 . 如果没有写一些逻辑来在一个超长的文本中插入一个空格(在正确的位置)来强制“假的自动换行”,我根本无法看到任何方法来处理文字和字符的换行,具体取决于具体情况,在一个UILabel The UITextView handles this situation automatically, breaking on word boundaries or character boundaries as necessary. UITextView自动处理这种情况,根据需要打破字边界或字符边界。

For specifics on how I am doing this, I have a one line UITextView with editing and scrolling disabled. 有关我如何执行此操作的详细信息,我有一行UITextView禁用编辑和滚动。 I also set the .contentInset to remove the padding making it look (to the unsuspecting eye) just like a UILabel . 我还设置了.contentInset来移除填充,使其看起来(对于毫无防备的眼睛)就像UILabel I am then using the sizeWithFont:constrainedToSize:lineBreakMode: method to determine the frame of the rendered text, and adjusting the frame of the UITextView accordingly so that it exactly fits the text. 然后我使用sizeWithFont:constrainedToSize:lineBreakMode:方法来确定渲染文本的框架,并相应地调整UITextView的框架,使其完全适合文本。

Hope that this helps! 希望这有帮助!

One way to do this is to set the Lines property in IB. 一种方法是在IB中设置Lines属性。

在此输入图像描述

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时,文本将换行,直到占用了许多行。

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? 如果一个单词太长,你可以在显示它之前插入空格,以帮助标签知道在哪里换行?

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;
        }

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

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