Prevent UILabel from character wrapping characters

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

Lots of text here. $$$$

Since the text at the beginning is free form, sometimes the wrapping looks like this:

Lots of text here · $$$
$

How can I prevent this? I want it to look like this:

Lots of text here ·
$$$$

I tried every lineBreakMode to help a bit. Word wrap does not work because it does not treat $$$$ as a word.

+4
source share
3 answers

It can be very late, but at least it can help someone. The way I did this 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 IBOutlet of UILabel CGSizeMake(154, 258) - UILabel, . , , .

+2

, UILabel, - NSLineBreakByWordWrapping, , . , linebreakmode .

. :

  • , , : ) , ) , ( ) .

  • a, - , , . , .

  • , , .

, !

+2

.

·\n $$$

$$$ .

+1

All Articles