0

My UILabel has a long text (text is different for different situations and languages, I use AttributedString), it ends with amount. Smth like:

This amount will be taken from your account: 12.00 $

Label has numberOfLines = 0, so sometimes text looks like

This amount will be taken from your account: 12.00 
$

And it's very important not to set line break between 12.00 and $, because it looks very bad. Are there any special symbol like   in html, so I can put it between 12.00 and $ ? So the UILabel never put like break between 12 and $?

So what I want is:

This amount will be taken from your account: 
12.00 $

or

This amount will be taken from your account: 12.00 $

or

This amount will be taken from 
your account: 12.00 $

but it should never be like

This amount will be taken from your account: 12.00 
$
Paul T.
  • 4,938
  • 7
  • 45
  • 93
  • see this once http://stackoverflow.com/questions/3527494/how-to-calculate-uilabel-width-based-on-text-length – Anbu.Karthik Jan 31 '17 at 10:35
  • I don't think this is actually a duplicate because the generic solution to replace space with a non-breaking space is not optimal in this case. – Sulthan Jan 31 '17 at 11:08

4 Answers4

4

Are there any special symbol like   in html, so I can put it between 12.00 and $ ?

For this case, you can use the nobreak space character \u00a0

Example:

let text = "This amount will be taken from your account: 12.00\u{00a0}$"

enter image description here

Andriy Savran
  • 535
  • 3
  • 19
0

You can add the non-breaking unicode space character, however, what you are doing is wrong.

You should be using NSNumberFormatter with style set to .currency and currencyCode set to USD.

That would give you:

  1. non breaking space automatically
  2. correct formatting for all languages (that is, $12.00 in English, $(12.00) for negative amounts in English, 12.00 US$ in European locales).
Sulthan
  • 128,090
  • 22
  • 218
  • 270
0

Note that I flag to a duplicated question.

As mentioned in the duplicated question, you need to use the "No-Break Space" unicode character "\u00a0".

What I want to suggest that you might want to use NumberFormatter for handling the currency character, similar to:

let formatter = NumberFormatter()
formatter.numberStyle = .currency
if let amount = formatter.string(from: 12.0 as NSNumber) {
    print(amount) // $12.00
}
Community
  • 1
  • 1
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
-1

So make sure you haven't set a height constraint on your label, and do this.

var value = 12.00
yourLabel.numberOfLines = 0
yourLabel.text = "This amount will be taken from your account: \n \(value) $"
Clinton D'Souza
  • 301
  • 2
  • 8
  • Im not sure who down voted and answer and why? At least leave a reason. – Clinton D'Souza Jan 31 '17 at 10:57
  • Hello, I think your answer has been down-voted because it's not giving the expected result for the OP. This will *always* let the value and "$" to be on a new line, which is not the case here. To elaborate, what if "This amount will be taken from your account:" was only "taken amount"? – Ahmad F Feb 02 '17 at 07:40