Dynamically indent UITextView's first line - ios

I have a UITextView in a prototype UITableView cell. I'd like to dynamically indent the first line of the UITextView to make room for a button that will have a variable width depending on the cell row. The button will vary in width because it's title will contain usernames in an activity feed.
Think of Instagram or any app that has a feed with clickable usernames on the left and text directly following.
I've been able to get close by counting the number of characters in the button title and left padding the text of my UITextView with a series of #" " blank spaces. This is akin to what other SO posts have suggested.
This is inaccurate for me because the width of blank spaces don't precisely match that of the button title because different characters have different widths.
Does anyone know if it is possible to indent a UITextView by a specific float value? I've been able to convert the width of the button title to a float and I believe that this would solve my issue.

Related

Text spilling over out of UILabel container in TableView cell using .lineBreakMode = .byTruncatingMiddle

I've got a UILabel in my cell and it gets filled from text from my model that is being shortened with
text.lineBreakMode = .byTruncatingMiddle
If the text is longer than what will fit in my UILabel, the text continues to show until it's off the side of my cell.
As you can see the K is being cut off by the right end of the cell and there are like 5 more characters that follow in the string.
What I would like to happen is if the string was "123456789101112" for the string to be shortened enough to fit within my UILabel and not spill out of the right side of the label and look something like "1234...1112".
Edit: Tried adding some constraints, still having the same issue.
What I'm trying to say with those constraints are to have a constant width and to pin it to the sides so there's no chance for overflow , but even with those it's having the same issue.
I have a collection view and added the same constraints to that cell and it worked perfectly. Should there be something different needing to be done just because it's a tableview cell?
In all probability the label itself is spilling off the right of the screen. You need to give it constraints so that the whole label stays on the screen no matter what size the screen is.
In this example, the label contains your "123456789101112" string:

Place Text in Top part of UIlabel

I have a UILabel that covers most of the view. When I place text in the label, the text is center in the middle of the label. I have tried everything by playing around with the options in the attributes inspector, however the text wont start in the top area of the label, instead it appears in the middle of the label.
Is there any way to places the text in the top part of the label??
(If someones is wondering, yes I want the UILabel to cover most of the view because some texts are longer then others, and some are shorter, and I would prefer the text to be placed on the top part and not the middle of the UIlLabel.)
UILabel has an intrinsicContentSize matching the text contents. If you don't constrain the height, then it will automatically adjust. So you don't need to make it cover the entire view.
Instead, tag it onto the top with a fixed distance. Specify the bottom distance with greater-than-or-equal. This way the label can grow until it reaches the lower limit and then the text will begin to be truncated.

iOS what element is most suitable for large texts eg text view

I wonder what ui element I should use for large sets of text. Eg anything between 200 - 1000 characters.
I already place the text inside a scrollview so it doesn't have to ve scrollable or editable etc, I just want to display text.
So what should I use between label / text view / text field?
Thanks in advance.
Text views are good for texts with varying lengths, but they have a scroll view of their own. You may consider using a normal UILabel and setting the "Lines" property to 0. I know that sounds strange, but setting it to 0 tells the label that it is a multi-line label. Then you can use auto layout to establish the width of the UILabel. It will grow down based on the amount of text in it. Text fields would be inappropriate for displaying texts in most cases; they are better for user input.

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.

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

Resources