Customize iOS UITextView text - ios

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.

Related

Delete 3 dots on Truncate Head using UILabel

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.

How to make UILabel text appear with ellipsis at the end when text is longer than label's width

My issue is with a UILabel.
The text it holds is much longer than the actual width of the UILabel. So I would like it to appear with "..." (an ellipsis) at the end to denote that there's more text.
I played around with horizontal content hugging priority (made it less than 251) but it doesn't seem to make a difference.
Right now it just chops the text when the width fills up.
It is fairly easy:
Create a UILabel called "aLabel" for example.
Create an IBOutlet.
Do:
aLabel.adjustsFontSizeToFitWidth = false
aLabel.lineBreakMode = .byTruncatingTail
If you are adding UIlabel through a Storyboard, following steps can be useful:
Select the label which you want to display '...' at the end.
Go to attributes inspector.
Select 'Line Breaks' and choose an option Truncate Tail
Check it by giving a text of more content size than label size.
Objective C:
yourUILabel.adjustsFontSizeToFitWidth = false;
yourUILabel.lineBreakMode = NSLineBreakByTruncatingTail;

UITextview clipping with three dots?

I have a UITextView showing some dynamic content that can vary in size.
The textview has no scrolling allowed and its size is independent from the content.
Given its auto-layout constraints, the text view has a different horizontal size on iphone5 and iPhone6plus for example.
What I would like is my text to be clipped when necessary with 3 dots at the end like that “…”. (there is a "More" UIButton launching safari below the UITextView)
I’m not sure if there is a UITextView property or if i should consider some code that checks how many characters the textview can display in the current circumstances and modify my string to be shown accordingly (cutting and appending #“…”).
Thank you.
Try setting the UITextView line break mode. It works just like UILabel and should use "..." at the end.
self.textView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;
See https://developer.apple.com/library/ios/documentation/UIKit/Reference/NSTextContainer_Class_TextKit/index.html#//apple_ref/occ/instp/NSTextContainer/lineBreakMode
If you don't need to edit the content of the text view from the user and you are only displaying content, then use UILabel.
above white background it the text view and the text with 4 lines is the UILabel, i assume you are trying to implement something like this.
Only change the line break mode to "Truncate Tail" and set the no of lines to value you desire.

How can I resize textview when I input long text?

I have a textfield in a tableview 's last cell. The textfield is one line at beginning, when I input some text more than one line, I need the textfield change its height for me can view all text. How can I do this?
edit 1:
I have try text view, it can input multiple line text, but how can I know when it from one line become two line? because I need resize cell height also.
edit 1 's answer
UITextView Auto Height
You can't do something like this with a UITextField because it's intended to be only a field and you can't change its height. Otherwise take a look to UITextView which can be set as you want, its frame etc... You only have to set it as editable so you can write text in it.

customize leading and kerning in a text view in iPhone application

I am using a UITextView in a iPhone application.I am using custom font.I want to set 32px line spacing in the text view also want to adjust kerning and leading between characters in text view.
Anyone knows about it?
Can the core text framework is helpful? If so, please explain how. How can Incustomize the line spacing in text view in iPhone application?
If you want use Core Text, it has the feature to specify the leading-space of a paragraph, the first line head indent of a paragraph, the line-spacing, underline...
check the docs here

Resources