IOS 11 UILabel automatic line break doesn't work when text is too long [duplicate] - swift

This question already has answers here:
UILabel Text Not Wrapping
(8 answers)
Closed 5 years ago.
All automatic line break of UILabel was failed when I updated the XCode to 9.1. Any body same as me? I have set the attribute like below:
cell.testLabel.text = "long text here"
cell.testLabel.lineBreakMode = .byWordWrapping
cell.testLabel.numberOfLines = 0
Anybody can help me? Thanks!!!

Since iOS11, there appears to be an intentional change in how words are wrapped within an UILabel. Word wrapping mechanism was adjusted so as to make your multiline text wrapped with enhanced proportion. If you have a long text consisting of short words standing consecutively, it is likely that your text will be wrapped avoiding orphaned words, i.e. more than one word is about to be wrapped onto the next line. If the finishing word is long enough, then there might be no need to wrap extra words, because proportion may appear justified.
Example:
1.
Both words are wrapped, even though there's a room for one 'test' on the first line.
2.
Only the longest word is wrapped, because enough space is already occupied on the second line with it.
As of now, unfortunately, you are unlikely to influence it, since no additional properties were introduced to manage this behavior.
Workaround
There's a hack which can help in certain circumstances. You can replace whitespace with a non-breaking space (U+00A0). This way two consecutive words are treated as one which may cheat wrapping mechanism.

Related

Is it correct to use Word Joiner (U+2060) in the same word?

In Bangla, Hosonto (U+09CD) is used to create a ligature, which joins adjacent letters. For example ক্ক is created using ক + ্ + ক. But sometimes we need a non-joining Hosonto (ক্‌ক). To make it possible, traditionally we use a Zero-width non-joiner (‌‌‌‌‌U+200C‌).
The problem with ‌‌‌‌‌ZWNJ is that, when the line is too long and line wrapping occurs, the word is broken into two lines. To keep the word as a whole, I need a character, something like “Zero-width non-breaking non-joiner”. But I don’t see such character in Unicode. So I think, Word Joiner (U+2060) is the best option.
To me, Word Joiner sounds like “joins two words”. But in my case, I need to join two parts of a single word. So, the question is, is it correct to use Word Joiner here?
U+200C ZERO WIDTH NON-JOINER has no effect on line breaking. Its absence or presence does not change where line wrapping can occur. If inserting a ZWNJ within a word causes that word to be broken across lines, then whatever application you are using to view your text does not implement the standard correctly.
ZWNJ is the only correct character for your purposes. More than that, using U+2060 WORD JOINER could in fact lead to inconsistent results. Much like ZWNJ does not affect line breaks, WJ is not supposed to affect joining behaviour (it is defined as “transparent” in that regard). While the standard doesn’t explicitly mention cases like this to the best of my knowledge, one could reasonably argue that inserting a WJ between the two letters in your example should not change the way they are displayed.

How can I inject custom line breaks on a symbol layer label?

Is there a way to override the default "text-max-width" label wrapping and inject my own custom line breaks on a symbol feature label? Unfortunately for my current uses, "text-max-width" is strictly defined by em units and not actual character limits.
I'm attempting to break at specific characters and was hoping that setting "text-max-width" to 0 would give me control over line breaks. Currently if I inject "\n" within my string while "text-max-width" = 0, those custom breaks will be ignored and the label will instead be drawn as a single line.
Is there currently a way to work around this in mapbox-gl-js; or would this type of functionality be a new feature?
Just for clarification: \n in text-field is supported since v0.24.0. text-max-width defaults to 10em. Setting it to 0 disables all wrapping, including \n, in v0.33.1.
This may feel wrong, but setting text-max-width to Infinity should work.
The code responsible for text wrapping is in src/symbol/shaping.js, starting withshapeText(..., maxSize, ...). A quick look didn't reveal any problems with using Infinity, but I also found no other way.

How to display a lot of read only text in GWT?

I have a requirement to display a somewhat large amount of text, read only to the user. It can be up to a maximum of 500 characters, which isn't excessive, but it's still a lot. Since it's read only I was thinking of a label a versus text area box, if it can handle that much. Is there a better way to do this than I'm not aware of?
Thanks,
James
Label works fine. Just remember that the default css for white-space collapses whitespace rather aggressively. If your text includes line breaks you may want to switch to pre or pre-wrap.
The most straightforward (if not necessarily most correct) way to do that is:
Label myLabel = new Label();
myLabel.getElement().getStyle().setProperty("whiteSpace", "pre");
Note the Camel Case on the CSS attribute.
Either a Label or a TextBox will definitely be able to handle 500 characters.
Think of all the blog posts, Wikipedia articles, Stack Overflow questions, longer than that that have been written. They were all composed in a text box and displayed in a div. You'll be fine.
500 chars is no big deal so it will be ok. Label is ultimately calling element.innerHTML = text which is a browser-native Javacript function that can handle any amount of text.

How to make the 1st line in certain amount of text in a Label bold?

I have about 400 character length string with a heading called Details. In this 'Details' is to be bold and of fontsize 19. While all the remaining text starts in the next line and should be of fontSize 18 like the contents. How can I do all this by using a UILabel?
Plz help me...
You can't do it with a stock UILabel as of SDK 3.2. You can create an NSAttributedString that specifies particular styles (like bold) for particular parts of the string, but there isn't a simple way to actually render it. This is unlike In Max OS X, which has NSAttributedString(AppKitAdditions), which allows you to draw an attributed string in a single line of code. If you want to render it, you can, but you'll have to delve into one of the lower level APIs (such as Core Text).
You're probably much better off using two UILabels. Make the first one bold, and make the second one cover multiple lines.
Since UILabel does not support attributed strings, you have two options:
Subclass UILabel and override -drawRect:
Create 2 UILabels, one of which will display the heading, the other showing the rest of your text. You can choose to wrap the two labels into your own view if necessary.
You could use a UIWebView with an HTMLString of <strong>First part</strong> second part

How to word wrap text? [duplicate]

This question already has answers here:
Best word wrap algorithm? [closed]
(10 answers)
Closed 2 years ago.
In Cocoa, there is a method (lineBreakBeforeIndex:withinRange:) on NSAttributedString to find appropriate line breaks in a piece of text.
Since NSAttributedString does not exist on the iPhone, does anyone have any suggestions for similar functionality?
Edit: Realized that I'm really looking for a word-wrap algorithm. For example, I want the text to wrap at 80 characters; where do I place line breaks so that words are not split over two lines?
Edit: An example will probably help. Say I have the following lines:
Here is line 1 that probably won't even be 80 characters long.
Here is another line with different text that is longer but not by much.
I would like to turn that into something like:
Here is line 1 that probably
won't even be 80 characters
long.
Here is another line with
different text that is
longer but not by much.
Edit: I'm looking to place this text in a UITextView so that additional edits can be made (very similar to a quoted reply with emails)
NSString *newString = [oldString stringByReplacingOccurrencesOfString:#"\n" withString:#"<br />"];
That will find all your line breaks and replace it with a "" tag. Just edit it for your own needs. :)