0

I have a UITextView in which I am showing the result of a calculation. Unfortunately, if there is a negative sign in front of a number, and the number is near the edge, UITextView is wrapping the number by splitting it at the negative sign like this:

Current behavior:

Answer = -
123456789

Instead I would like it to show:

Answer =
-123456789

In the current behavior, there is a risk that the user does not see the minus sign and misinterprets the answer.

To solve this, I could force a line break in front of the answer, but sometimes the entire string will fit on one line, in which case I would prefer to leave it all on one line if the answer is short enough. Is there an easy way to prevent UITextView from wrapping in between the minus sign, or do I need to resign myself to forcing a line break?

Sometimes the first part of the answer has a few words in it, and could itself also require a line break, so in other words, it could look like this if I just forced a line break:

The result of the calculation
is
-2

but in that case I would rather keep the "-2" next to the word "is", so that it would read as follows:

The result of the calculation
is -2

user955853
  • 379
  • 2
  • 13

4 Answers4

0

How about using two labels? You can place the one with the number "one line down" only if its width is above a certain value...

EDIT: You definitely need two labels. After determining the text that goes on each, apply 'sizeToFit' on both and lay them out fluidly, like HTML in-line elements: If both fit in a line side by side, good; otherwise one below the other.

The line break logic you want is not included in UILabel. You must implement it yourself.

(Perhaps UITextView behaves slightly differently?)

Nicolas Miari
  • 15,044
  • 6
  • 72
  • 173
  • Sometimes the first part of the answer has a few words in it, and could itself also require a line break, so in other words, it could look like this: The result of the calculation
    is
    -2

    but I would in that case rather keep the -2 next to the word "is"
    – user955853 Jun 16 '12 at 15:54
  • Oh, I see, the 'answer' part is not fixed... Then you should put together all the possible cases on paper and make a decision. – Nicolas Miari Jun 16 '12 at 16:45
  • Still, having the numeric value on a separate label will ensure the line is never broken after the sign (because it's the first two chars) – Nicolas Miari Jun 16 '12 at 16:46
0

I guess you could parse for numbers with negative signs in front of them, and then insert a newline (\n) in front of that number. The UITextView will accept newlines; I have used them.

If you want to keep the number on the same line if it is small enough, you will need to come up with a more elegant approach of programmatically deciding if the number has enough characters to stay on one line, or if it has moved to two.

dgund
  • 3,395
  • 3
  • 37
  • 64
0

For presenting number results the best way is to use NSNumberFormatter and for the number to have the - sign always next to it.

user1447414
  • 906
  • 1
  • 7
  • 24
0

Thanks everyone for your answers! This gave me some ideas, and was able to solve the problem. I hope this helps someone else with a similar problem. First, to clarify the problem statement again, the result is in the following form:

NSString * result = [NSString stringWithFormat:@"%@ = %@", answerString, answerNumber];

By calculating the # of lines in the UITextView content view (How do I size a UITextView to its content?) I was able to solve the problem with the following approach:

First, compare the number of lines in the UITextView both with and without the answer. If the number of lines is different, then this means that UITextView has decided to break the result onto a new line, in which case I should re-format the result to add the line break manually before the number, in order to ensure that the negative sign (which is part of the number) is the first character on the new line:

- (int) numberOfLines: (NSString *) result {

    UITextView *myTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 255, 0)];

    myTextView.text = result;

    CGRect frame = myTextView.frame;
    frame.size.height = myTextView.contentSize.height;
    myTextView.frame = frame;

    int numLines = myTextView.contentSize.height / myTextView.font.lineHeight;

    return numLines;
}

- (NSString *) formatResult: (NSString *) answerString answerNumber: (NSString *) answerNumber {

    NSString * resultWithoutAnswer = [NSString stringWithFormat:@"%@ = ", answerString];
    NSString * resultWithAnswer = [NSString stringWithFormat:@"%@ = %@", answerString, answerNumber];
    NSString * result = resultWithAnswer;

    if ([self numberOfLines:resultWithoutAnswer] != [self numberOfLines:resultWithAnswer]) {
        // If these are different, then UITextView has added a line break before the answer. To prevent UITextView from potentially splitting the number across the negative sign, manually add a line break to ensure that the negative sign shows on the same line as the number.
        result = [NSString stringWithFormat:@"%@ = \n%@", answerString, answerNumber];
    }
    return result;
}
Community
  • 1
  • 1
user955853
  • 379
  • 2
  • 13