Break line on max width text UILabel swift 2 - ios

I'm currently trying to make an app that takes the usernames of some people and print them out in a custom cell, inside a label.
But if a user has the name "John Goodwilkinson" It's a large name,
so i'm trying to figure out how to make a
/n
Between the 2 words.
Does anyone has any idea?
I've googled but couldnt find anything at all.
Below you can see the screenshots with my code and the suggestions of the fellows :)
Also i have tried to put the code in the view controller that i call the custom cell like this
cell.frndName.numberOfLines = 0
cell.frndName.lineBreakMode = .ByWordWrapping
But it didn't work either

You can do that by setting the number of lines to the label to be 2 or 0 (if you wnat to be any number) and line breaks to be word wrap.
Link this
Update
after checking you UI. It ended up that removing the height/ width constraints and pinning the label from the bottom/ right the other controllers solved the problem.

You have to increase height of your UILabel and apply constant only for its width.
second thing you can add #“\n” by NSString *newUsername = [oldUsername stringByReplacingOccurrencesOfString:#" " withString:#“\n”];
it will replace all spaces between two words by #“\n” and next word will show in next line.

Related

UILabel line breaks too early on iOS 11

I have an iOS 11 app with a table with cells.
In this cells, I have a UILabel.
The UILabel seems to "decide" to break the line in a way that keeps the bottom line with at least 2 words (no matter what the BreakLineMode is!).
I know about using -
Adding non-breaking space (U+00A0) - TOO EXPENSIVE
and
Using NSAllowsDefaultLineBreakStrategy NO on NSUserDefaults - NOT ALLOWED BY APPLE
What can I do to fix this problem?
In this image, you can see that the first word in the second line, could appear on the first line but got down to the second for some reason...
EDIT:
This is NOT a width issue, this is an issue with iOS 11 NEW UILabel logic(!) that doesn't allow orphans words (one word) on a new line, Only two word because they think it looks better. It doesn't. But there must be a way to make a UILabel act on iOS 11 like it did on iOS 10 and under.
I have found an hack -
Adding a lot of spaces (like 10 - 20) to the end of the string will make the UILabel "think" that the line is long enough so it will leave one word on the bottom line. (instead of 2 so the first line will not break early)
I don't like hacks but this is all I've got at the moment, if anybody has a different answer i will gladly accept it instead of this one
You can add this three line of code -
labelName.adjustsFontSizeToFitWidth = true
labelName.minimumScaleFactor = 0.3
labelName.numberOfLines = 0 // or you can set it 1 or 2
In my case I was able to solve this by using a textview.
UITextView allows orphans, sizes according to intrinsic size if scrolling is disabled and works like label if interaction is disabled, which is also disabled in label so it will forward all the touches.

Find specific characters in UIlabel?

I am having a slight issue at the moment regarding finding specific characters in a UILabel. I tried the simple
if variable == "the string you want it to equal" {
//Do the thing you want
}
The issue is that my UIlabel updates based on the time. This normally would be no issue but I also have a smaller Label next to it as seconds.
This works fine until the main time label hits the double digits of 10, 11, 12. This causes the Label to expand and cuts off the smaller seconds label. I know I could place the smaller label somewhere where the main label doesn't cut it but the design wouldn't be as great.
I tried also to make variables as BOOLEANS
let time10 = "10"
let time11 = "11"
let time12 = "12"
Now this would require me to make many other variables like "10:01, 10:02" which would be highly inefficient.
Is it possible for a feature in swift to look up a specific amount of characters in a string (the first 2 in this case" and let me write the thing I want to do if the condition is met?
Try playing with .minimumScaleFactor and auto layout priorities.
The first one will let one label scales down font size to avoid ....
With auto layout priorities you can make a label shrinks in spite of another.
It should be easier if you are using Interface Builder.
Hope this helps
Try using label.sizeToFit every time you update the label's value. Hopefully it should fix the issue.
To answer your original question, you can use the count function, for example you can do count(string), as explained in this answer.

Word Wrapping on UILabel iOS

Hi friends i have one requirement that word wrapping on label. Using myLabel.lineBreakMode=NSLineBreakByCharWrapping;
I am able to show the text in label like below
Designation : I am working as iOS
Developer
But i want to show
Designation : I am working as iOS
Developer
Is this possible to achieve above requirement within one label?
Thanks
I tried it and got it solved. I have attached the screenshot. You just need to do the following:
set lines property of label to 2.
set height and width of the label through xib and its done.
In Xcode, you can design your UILabel to have 2 lines. Then, you just need to add a new line ("\n") after the word "iOS", and center-align everything? If your caption is always the same you can pre-enter it in Xcode. To add a new line, just press alt+Enter.
Would that be acceptable?
No, it's not possible with UILabel, Unless workaround to this. I think, you know how to done manually. But You can't wrap the content like above. Simple answer is you can use two label.
Not possible. UILabel just renders text according to properties that you defined. If you want to do it ideally then use two UILabels. & Place them according to your need.

Superscript "Registered mark" in UIPickerView

I need to show a registered trademark symbol in a superscript (exponent position) in respect to the name of the label. need to show it in a uipickerView and a label. I havent found the option of how to show the unicode \u00AE in the exponent position,
1) Should I use 2 labels in different positions in the uipickerView?
2) Or, Show an image in the picker view with the intended name?
Thanks a lot!
For me this works:
#"Sometext \u00AE other text".
Can you check?
The unicode symbol is very slightly superscripted but I guess this isn't enough for you. You can't mix font sizes and styles within a label so there isn't an easy answer. Two labels probably won't work because you will be having to adjust the position of the R sign.
The simplest answer is probably to subclass UILabel and, in the drawRect method, call the super implementation and then manually draw the R string where you want it. You can get the size of the existing text using the NSString drawing methods so you'll know where to put the symbol.
I ended up using an image in the picker view to show the superscript as needed,
inspired by the catalog app from apple,
if you are a noob like me and would like to check the project, please go to
project in Github

How to write multiple lines in a label

I have a XIB file of type View XIB. In it I have a View controller, and in the View controller I have a label.
I have long text that I want to break, but it doesn't - it gets truncated.
I tried this solution - I did it in the attribute window - but it doesn't help.
You could use a TextView and disable the Editable and User Interaction Enabled options in IB.
use this
myLabel.lineBreakMode = UILineBreakModeWordWrap;
if you are worried about backwards compatibility use this because the comand i mentioned before was introduced in iOS 6.0
myLabel.numberOfLines = 0;
I have been running into a similar problem. I started out with a label, but wanted multi-lines and a UITextView worked great for this, at first. Because I am using a third party library (MBProgressHUD) to handle stuff like progress messages and what not, I had thread problems when trying to update a UITextView and show the progress message at the same time.
So I went back to UILabel which actually didn't have thread problems. I found a property to allow a specific number of lines of my choosing, and had to create the label big enough to display those lines. The downfall to this approach is that I don't get the context menus like Copy, Paste, etc. but more importantly I'm not running into thread problems. I can always embed this into a UIScrollView in the future if I so choose.
You could check out my category for UILabel on GitHub. https://gist.github.com/1005520
This lets you resize the height of the label to accommodate the content.
From the Attiribute inspector, choose LineBreaks->Word Wrap option when you have selected the lablel. Also increase the number of lines Label->Lines.
Try Following steps to solve issue :
Drag out a UITextView.
Double-click it to edit text, place cursor where you want the
paragraph break.
Option-Enter a couple of times to create a blank line & paragraph.

Resources