UILabel Word Wrap / Character Wrap

2020-05-27 11:04发布

I have a UILabel with the lineBreakMode set 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 UILineBreakModeCharacterWrap setting, but only for those long slabs of spaceless text).

Thanks in advance!

4条回答
beautiful°
2楼-- · 2020-05-27 11:24

For anyone else having this same issue, I ended-up solving it by using a UITextView instead of a 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.

  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 UITextView handles this situation automatically, breaking on word boundaries or character boundaries as necessary.

For specifics on how I am doing this, I have a one line UITextView with editing and scrolling disabled. I also set the .contentInset to 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 UITextView accordingly so that it exactly fits the text.

Hope that this helps!

查看更多
神经病院院长
3楼-- · 2020-05-27 11:36

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;
        }
查看更多
够拽才男人
4楼-- · 2020-05-27 11:37

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

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.

查看更多
Ridiculous、
5楼-- · 2020-05-27 11:48

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?

查看更多
登录 后发表回答