Delete 3 dots on Truncate Head using UILabel - ios

i need to delete the 3 dots in the head of label, how can i do this?
i already tried others types of line breaks but "Truncate Head" is the only that truncate the label in the beginning.
Here is my actual label with 3 dots '...'
I need to do like this:
Any ideas ?

I don't think there is a good way to do this without faking it. You can fake it by giving the label a trailing constraint but not a leading one. and make the text right justified. This has the effect of anchoring the right side and the text will run off the screen to the left. If you put another view in front of it with the same color as the background, this will mask out the extra text.

This isn't supported out of the box, so you'll need to make a custom label class to accomplish this. See the answer on a similar question "How to change truncate characters in UILabel?"

Instead of setting label.text value, set it as attributed text. Like below.
let attrString = NSAttributedString(string: "YOUR Text")
label.attributedText = attrString
After this, your font settings might be disturbed, which you need to set again programmatically for your label.

Related

UILabel word wrapping at the number sign instead of before the sign

I have a UI Label with text that is wrapping too late. Here's a screenshot:
Is there a way to fix this? Why is it wrapping after the + sign?
The hack is to insert a newline right before + sign, but that won't look good on different screen sizes.
Here's what I have set on the UILabel:
textLabel.numberOfLines = 0
Thanks #matt for the comment which helped me solve this. I also should mention that I'm setting attributedText on this UILabel, which is why the lineBreakMode probably didn't work.
I fixed this by setting the text like so:
"... +\u{200D}$50"
Basically I added a zero width joiner as #matt suggested \u{200D}

How to align UIButton to the left with UILabel on the right. Like instagram's comments

I am trying to have the username on the right and then a UIlabel with some text to the left of the button.
Here is an example of what I am after:
I am just not sure how I can get the constraints to do that, especially the part where the UILabel text continues on the next line below the button...?!
If anyone has any ideas, any help is appreciated!
Regards.
Please see the sample I created on my UIButton inside UITextView idea:
https://github.com/smozgur/UIButton-inside-UITextView I am still playing with it but it results fine so far.
I think the best approach would be to do it all with one label. You could set the text styling using an attributed string (code below). Then create a UIButton that is laid on top of the label (the size of the UIButton can be adjusted based on length of username).
This is the approach I used when creating a disclaimer label on a signup screen as shown below
Here's the code to create the attributed string:
let disclaimerAttributedString = NSMutableAttributedString(string: disclaimerLabel.text!, attributes: [NSKernAttributeName: -0.4])
disclaimerAttributedString.addAttributes([NSForegroundColorAttributeName: UIColor.whiteColor(), NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue], range: NSMakeRange(38, 12))
disclaimerAttributedString.addAttributes([NSForegroundColorAttributeName: UIColor.whiteColor(), NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue], range: NSMakeRange(55, 14))
disclaimerLabel.attributedText = disclaimerAttributedString
My situation is slightly different because I am applying it to static text. For you, you can either create the button in code based on the length of the username or you may just be able to approximate it by pinning the button to the top left of the UILabel.
You could figure out the length of the attributed string that the username would occupy by using the String extension in this SO answer

How to leave a space between lines in UILabel?

I am using UILabel into one of my application and its having multiple lines into it. Now I have to put some more space between two lines in UILabel. I tried to search out the solution, but not able to find it. How can I put more spacing between two lines?
In the UILabel's Attribute Inspector, change the text from Plain to Attribute, then change number of lines and line spacing, its not the UILabel thing but the string itself
You can set it in the UILabel's Attribute Inspector.
Change Text type from Plain to Attribute, then change number of lines and line spacing.
Hope it will help.
You may find useful link for spacing between two lines in UILabel.
There are also third party libraries to do so.
Few examples are like
MSLabel
MTLabel
You can also add spacing directly in your fonts. To get more info,check this SO Post

Line Spacing for UILabel with a single line of text

Is it expected that a multi-line UILabel with a custom lineSpacing attribute include that line spacing even when the label's text fits on one line?
Here is my label:
let label = UILabel()
label.numberOfLines = 4
var paragraph = NSMutableParagraphStyle()
paragraph.lineSpacing = 5
paragraph.lineBreakMode = .ByTruncatingTail
label.attributedText = NSAttributedString(string: "Some short text", attributes: [NSParagraphStyleAttributeName: paragraph])
And here is how it is laid out. Note the additional spacing below the text.
For comparison:
What's strange is the lack of consistency. When the label extends to a second line, the bottom line no longer includes this additional spacing:
Is there a way to remove this line spacing when there is a single line of text? Or some other way to enforce some consistency so I can at least account for it?
Update
The baseline calculation also seems broken. When attempting to align a view (here, the red box) with the label's baseline, multi-line labels are partially covered.
Since you said you were using a custom font, my best guess based on prior experience is that the root cause of that issue that you seeing lies somewhere inside of the custom font itself. Whenever I am given a custom font by a client, 90% of the time, something is "wrong" with the actual font metrics (as interpreted by Apple's internal font rendering subsystem, even though it might render correctly somewhere else).
The good news is that this is fixable, but it requires rebuilding the font with new metrics, which is usually a trial/error affair. You might also need to check to see if the license you have for the font will allow such a thing (if it even matters).
That being said, these are some resources to questions that I keep around for this exact scenario whenever I start a new project:
Here's a similar question to yours with the assumption that this is a custom font issue: "Custom UIFont baseline shifted". This question deals with this issue in a UIButton "UIButton custom font vertical alignment", but both of these questions end up at the answer to this question "Custom installed font not displayed correctly in UILabel".
I have a personal testbed app for custom fonts now that I use whenever I am first given a custom font. This allows me to test the font in isolation for each rebuild iteration to make sure it's perfectly rendering. Make sure to test your changes in various font sizes and even in additional languages (yes, lots of permutations). I have had issues specifically with Thai and Chinese when using custom fonts as their ascenders extend very close to the edge of the bounding box for a UILabel. The testbed that I've created for myself includes the font rendered in basic UILabels in various sizes and various languages in various sizes (since like I said, I've had a bad experience in the past with custom fonts in certain languages that rendered fine in Roman characters).
If someone has a better solution to this, I'd love to hear it as I run into this issue with custom fonts almost every time. This is my workflow for nipping the issue in the bud before we start compensating for the font's rendering issues during layout or using individual attributed string adjustments. I'm not font expert, I'm just a guy who likes fonts to render like the built-in fonts (especially when using auto layout).
You can calculate the number of lines and set lineSpacing to 0 if there's only one line.
But there might be a better solution.
paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping
can avoid this issue when text is multiline.
explicitly setting the font with with fontName [UIFont fontWithName:#"PingFangSC-Regular" size:14] , instead of using [UIFont systemFontOfSize:14] can avoid the issue when text is single line.
Hope this is helpful for you!
This is def. a issue for UILabel. It happens for custom as well as the system fonts.
If you can use a UITextView, go with that. The UITextView has no problems with single or multi line text line-spacing and behaves correctly (single line = no line spacing).
This way you can also avoid creating a custom line count func/ext.
Yes, the lineSpacing is applied regardless of how many lines are in the label. If you're using autolayout, you can work around this by constraining your label's baseline to its parent or sibling views (as appropriate), instead of the label's top edge aligned to the parent or sibling's top. (This assumes, however, that your label's background color is the same as the color of its parent view; otherwise, you'll see the extra line spacing appear in the background color.
Another thing you can do (and this is probably preferable, now that I think about it) is to set a paragraphSpacing attribute of 0 as well. That should negate the lineSpacing for the last line in the label, regardless of how many lines you have.

Customize iOS UITextView text

In my iOS application i have an UITextView in which i insert some text downloaded from the web. This text is not editable/selectable by the user and it's a sort of preview of the whole text downloaded.
So, i want to show only the first two line of the text and i want to have always a vertical centered alignment. The text can also have only one line and, if there are more than two lines or there is a very long line i want to put these ".." at the end of the visible text.
I want something like this:
I hope i explained myself.
If it is only a preview of the entire text, you can use a UILabel instead. The UILabel will have a fixed size and it will automatically truncate the text at the end and add the "..." you want. Don't forget to specify the numberOfLines property of UILabel to be 2 and the textAlignment property to NSTextAlignmentCenter if you want it centered.

Resources