How can I resize textview when I input long text? - ios

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.

Related

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.

Changing the height of a tableview cell dynamically

I'm implementing a tableview that look like facebook newsfeed.
I've done setting the height of tableviewcells dynamically based on the content inside
But now i got a problem that is i want to show ....more after just showing 3 lines of content just like in facebook and after clicking the more button the height of the tableviewcell has to be increased by showing the full content of the tableview cell.
See the screenshots below
instead of showing all the content i want to show just 3 lines and after that ....more has to be there and if the more button is clicked, the height of the tableviewcell has to change dynamically
You could truncate your UILabel or change the size of your UITextView and place an adjacent UIButton with the title "more". So when the user clicks on more you would change the height of the cell and the frame of your textView/label and the more button should be hidden.
You could show a UILabel with test using truncating it( Your Text is... ) along with a UIButton place just below that.
So when user would click on the button you could change the property of the label( no. of lines, word wrap, etc) to fit in all the text along with changing the height of cell.
You can calculate text hight using :
-(CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
Returns the size of the string if it were rendered and constrained to the specified size.
Parameters:
1) font : The font to use for computing the string size.
2) size : The maximum acceptable size for the string. This value is used to calculate where line breaks and wrapping would occur.
3) Return Value : The width and height of the resulting string’s bounding box.
According to this you can update cell height.

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.

how to get uitextview text in particular area

i need to get text from uitextview. The text i need is in particular area after scrolling the textview, not all the text in uitextview.
i'll describe what i need in some images.
first, i have this textview
then, i want to get some text, like in this image :
but i dont know how to do that, can somebody help me ?
thank you
NSRange range = NSMakeRange(desired_position, 0);
[myTextview setSelectedRange:range];
Interesting and unusual question.
[Rewrite for UITextView, originally written for UITextField]
A UITextView is basically a text input control wrapped in a UIScrollView ( using inheritance). A UIScrollView works by moving it's contents around using the co-ordinates of it's view around relative to it's frame.
Look at the contentOffset of the UITextView to get the amount of vertical scrolling that has occurred ( the text has scrolled up, so the y value will be negative.)
Once you have the amount of scrolling, you need to get the font for the UITextView. Use NSString sizeWithFont to get the height of a line. Divide the y-coordinate by the line height and that will give you how many newlines to skip.
The end of the visible text is a little different. Depending on your controller structure, the UITextView may have been resized, or not. If you're using a UITableViewController, for example, it will typically resize the tableView automatically. I'm going to assume that you have resized the UITextView, in which case the end 'y' is the start 'y' + the textView.frame.size.height. If you haven't resized the UITextView to fit the space above the keyboard, then you change the design to do so, or subtract the size of the on screen keyboard.
Hope this helps

Three20: TTTableControlItem caption's width

I have a simple login form on a TTTableView, in which I use TTTableControlItem with UITextFields.
I have two fields, Email & Password. Since the UILabel's width determined by the text, the two textfields aren't aligned vertically to the same line and it looks bad.
How do I control the width of the caption inside?
Is there another way to achieve this without subclassing/finding the labels/controls and move them after render?
Thanks.
You'll have to subclass TTTableControlCell with your own custom cell and override the function
- (void)layoutSubviews;
to layout the contents the way you want to.
Also don't forget to map TTTableControlItem to your new custom cell subclass in your data source (in the
- (Class)tableView:(UITableView*)tableView cellClassForObject:(id) object;
method)
I had the same problem and I chose to just set the text field to align the text on the right. I didn't like the amount of padding so I modified TTTableControlCell to cut back the width of the text field.

Resources