Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent UILabel from character wrapping symbols

Tags:

ios

uikit

uilabel

I have a multi-line label that has the following text:

Lots of text here · $$$$

Since the text at the beginning is freeform, sometimes the wrapping ends up looking like this:

Lots of text here · $$$
$

How do I prevent this from happening? I want it to look like this:

Lots of text here ·
$$$$

I've tried every lineBreakMode to little avail. Word wrap doesn't work because it doesn't treat $$$$ as a word.

like image 521
Justin Avatar asked Mar 12 '15 00:03

Justin


3 Answers

It seems that you might benefit from subclassing UILabel, which would treat a string differently for the NSLineBreakByWordWrapping line break mode, which treats your phonetic words like words. You will effectively be expanding the definition by which your custom linebreakmode considers a word.

You would have to roll your own line-breaking algorithm. The approach to determining the location of your line-breaks would be similar to the following:

  • Loop through the string, to get each character, until one of two conditions is met: a) you have reached the width of the view, or b) you have reached a space, and the next word (delimited by a space) doesn't fit on the same line.

  • If you have reached condition a, you have two options--you could either adopt a policy that never splits words into multiple lines, or your could only apply the non-split rule to your phonetic words. Either way, you will need to insert a line break at the beginning of the phonetic word, when there is no more room on a given line.

  • You may want to use two separate strings, to keep the source string separate from the display string that contains your formatting.

Let me know if that helps!

like image 108
Sheamus Avatar answered Nov 05 '22 17:11

Sheamus


This might be very late but atleast it could help someone. The way I have done it is as follows:

UIFont *fontUsed = [UIFont systemFontOfSize:17];
NSDictionary *dictFont = [NSDictionary dictionaryWithObject:fontUsed forKey:NSFontAttributeName];

NSString *strTextToShow = @"text that has to be displayed but without $$$$";
CGRect rectForSimpleText = [strTextToShow boundingRectWithSize:CGSizeMake(154, 258) options:NSStringDrawingUsesLineFragmentOrigin attributes:dictFont context:nil];

NSString *strTextAdded = [NSString stringWithFormat:@"%@ $$$$", strTextToShow];
CGFloat oldHeight = rectForSimpleText.size.height;

CGRect rectForAppendedText = [strTextAdded boundingRectWithSize:CGSizeMake(154, 258) options:NSStringDrawingUsesLineFragmentOrigin attributes:dictFont context:nil];
CGFloat newHeight = rectForAppendedText.size.height;

if (oldHeight < newHeight) {
    strTextAdded = [strTextAdded stringByReplacingOccurrencesOfString:@"$$$$" withString:@"\n$$$$"];
}

[lblLongText setText:strTextAdded];

lblLongText here is the IBOutlet of UILabel and CGSizeMake(154, 258) is the size of UILabel I have used. Let me know if there is any other way you have found.

like image 2
channi Avatar answered Nov 05 '22 18:11

channi


Try inserting a line break in your input text.

Lots of text here ·\n $$$

It should print the $$$ in the next line.

like image 1
MChid Avatar answered Nov 05 '22 19:11

MChid