Colas
Colas

Reputation: 3573

In a UILabel, is it possible to force a line NOT to break at slash?

Example: My text is @"This is a great UI/UX feature".

A better example: @"5/17 of this quantity".

Is it possible to force the line NOT to break at the / (the slash)?

I'm looking for a general way of doing, since I have others sentences with slashes.

Upvotes: 3

Views: 1569

Answers (3)

Anton
Anton

Reputation: 1

Yes, it is possible - just replace "/" or any other symbol, next to which you want to prevent line break, with non-breaking space of zero length - it is \u{2060}, e.g.:

anyTitle.replacingOccurrences(of: "/", with: "\u{2060}/\u{2060}")

"\u{2060}" prevents linebreak before and after any of symbols. In iOS 17 it works fine.

I learned it from here In a UILabel, is it possible to force a line NOT to break in a certain place

Upvotes: 0

Rory McKinnel
Rory McKinnel

Reputation: 8014

An option would be to use a Unicode character rather than normal slash.

There are non breaking versions in unicode for space and hyphen. If you were ok to change to using - rather then /, this using @"UI\u2011UX" will show UI-UX and not break the line at the hyhen.

There does not seem to be an equivalent non break for slash, however you could try another flavor of slash. e.g. UI\u2044UX will not line break but show UI/UX using the FRACTION SLASH. See http://www.fileformat.info/info/unicode/char/2044/index.htm.

I tried this just now on a UILabel and using the unicode characters will prevent line break. You just need to decide which slash or alternative you want to use.

Upvotes: 5

Ashok Londhe
Ashok Londhe

Reputation: 1491

Try this...

self.label.text=[self.label.text stringByReplacingOccurrencesOfString:@"/" withString:@" "];

Upvotes: -2

Related Questions