M
M
ManWithBear2014-11-21 22:01:50
Objective-C
ManWithBear, 2014-11-21 22:01:50

Why does UILabel spit on formatting?

Need to align multiple lines in an application.
The code itself:

subText = [NSString stringWithFormat:@"%3d%% - %@",subPercent,subfactor];
NSLog(@"%@",subText);
[label setText:subText];

As a result, everything is perfectly aligned in the logs, and in the application you can observe the following:
57e87194da024595970fca04d173524c.png
You can notice that the problem occurs with a different number of digits in the number.
What could be causing this and how to fix it?
UPD.
The labels are created in the storyboard, the dimensions and alignment are the same.
All this under iOS 7+

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lostuser, 2014-11-22
@ManWithBear

Looks like the problem is in "%3d%%".
When the number is two-digit, a space is added. When unambiguous, two spaces.
Try to output a number with %d
UPD. The mistake was that it was designed for monospace fonts (in which the width of all characters, including spaces, are equal).
In your case, you can use two UILabels, on the left for percentages, on the right for the rest. Or choose a monospaced font.

A
Anton Martsen, 2014-11-21
@martsen

First, additional questions:
1) Under what version of the OS is development underway? 7+?
2) Are you creating the UILabel from code or on the Storyboard?
I propose to start by looking at the UILabel property textAligment .
It is set by parameters from enum'a - NSTextAlignment
You need to either put it on the Storyboard, or write it in the code (for example, on viewDidLoad).
In the editor it looks like this:
85195d9a47a54e49915e96102c7be16e.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question