Changing UITextView height depending of it text - ios

i have app that have table with many cells, when user tap on it, then detail view controller appear. Detail View Controller have an image and UITextView, that is not editable and it purpose only for showing text (just like label). Text it contain come from an xml file, sometimes it have several rows, sometimes there is a lot of text.
What i want is - to make UITextView height change depending of amount of text it have. For example, for several rows it might be 50 pixels height, in other case 300 pixels. How could i measure correct height and set it for UITextView? Again i want to point that it purpose only for showing text, not edit in any way. My second task is to measure final UITextView height and store it in some variable.
And there is what i've tried:
CGRect textViewRect = self.myTextView.frame;
textViewRect.size.height = self.myTextView.contentSize.height;
self.myTextView.frame = textViewRect;
But apparently its not working. Any idea how to achieve that?
Any advice would be appreciated, thanks!

Use this method of UIView:
- (CGSize)sizeThatFits:(CGSize)size
Typically you want your view limited in width, but any height. So you pass in a size with width as much as you want, and height for example 10000.0, so it gets as high as it needs. This returns how big the view needs to be according to its current content.

Try this
UITextView *textview1;
[textview1.text sizeWithFont:text.font constrainedToSize:textview1.frame.size];

Related

Find height of UILabel when number of lines increases/decreases dynamically

I am trying to find the height/space occupied by an UILabel on the screen programmatically whose number of lines increases or decreases based on different devices and string internalization. So I tried to get the height of the UILable by the following method.
uiLable.bounds.size.height/ uilable.frames.size.height
This method was implemented in both viewDidAppear and viewDidLoad methods. In both methods, the height returned is same on all devices and when the number of lines increases/decreases. Please find the screenshot of the UI that I have. I have to align the switch button to the centre of both text (Remember for all meetings and These can be changed in settings).
Any help would be appreciated. Thanks in Advance
After updating the text in your label you can force the layout system to update its UI and then get the correct bounds of your label.
// set your label text
label.text = "multiline text here"
// Now force layout system to update its UI instantly
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
// Now you will get the correct bounds
let bounds = label.bounds
I am trying to find the height/space occupied by an UILabel on the screen programmatically whose number of lines increases or decreases based on different devices and string internalization.
Do not try to do that. You have no reason to want to know it.
Please find the screenshot of the UI that I have. I have to align the switch button to the centre of both text
Then align it with auto layout. That is exactly what it is for. Let the runtime take care of the problem for you. You cannot possibly internationalize successfully without using auto layout to take care of this sort of thing.
You can place both the label in a single view and set autolayout as verticalCenter of view is equal to vertical center of toggle button.

Calculation Frame of a UILabel Subclass

The bottom line is that I'm trying to reproduce the UI that iMessage has.
For the Label:
The special padding of that type of text made me create a custom UILabel. Here's the code under drawTextInRect:
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, UIEdgeInsetsMake(0, 15.0, 5.0, 15.0))];
No mystery for now.
The problem comes when my cell (that contain that label has to calculate height).
The Label gets a rounding effect on the label like so:
cell.message.layer.cornerRadius = 18;
[cell.message sizeToFit];
Apparently I can't get the proper height and width of that label. I'm using sizeToFit and then I mesure the possible sizes with "sizeWithFont:" (deprecated in iOS 7) and "boundingRectWithSize:".
The only way the text can show properly is adding manually an undetermined amount of size to height and width once the calculations are made.
The best I can get then is a screen that may look good but still has some problems and not draws properly the texts.
The link has a screen of some of the screens not showing properly.
The only answer I've been able to see looking at code from other people is that they at some point make their own calculations based on letter size.
Anyone with this problem check : https://github.com/jessesquires/MessagesTableViewController
It was the only source I could find, at the end for some cases the boundingRectWithSize is not good enough.

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

Reposition images within view based on size of text - iOS

I have been recently getting into iOS development, and I'm trying to build something that looks (very roughly) like this: http://falkendev.com/downloads/ios-sample.png
Basically, it's a page that shows simple text -- large header text that may span multiple lines, a separator line, and then smaller descriptive text that may be a variable length. This text does not need to be editable. I'm working using interface builder, but I imagine that what I want done may need to be done programmatically?
Two questions:
-- How do I go about creating these text fields so that they adjust their height based on the content? I'm assuming I would be using a standard "text" field for each, make them not editable, and then programmatically change their height? And then based on the height of the various text fields, I would need to adjust the positioning of the text fields and the divider line between them?
-- How do I go about making the page scrollable? It's possible that the descriptive text will be long and would extend off the edge of the screen. I would want the whole page to be scrollable, not just the descriptive text section. I'm assuming I would place all my elements within a scroll view... but currently when I do that and view it, the view just gets centered (cutting off both the top and the bottom) and I can't scroll it at all.
Thanks for any help!
set the scrollview content size to greater than its actual size to
make it scrollable like this :
scrollView.contentSize = CGSizeMake(YourWidth ,YourHEight ); // Here you can change either of height and width to make it more scrollable in that direction.
You can use UITextView object to have a scrollable text field...
which can scroll to show additional text..just set its editing
property to NO.
Otherwise to dynamically update label height yourself...use
NSString sizeWithFont method

How to layout UITextview beneath other textview?

(iOS)
I have a view that contains multiple textviews under static headings (see image below). The text of the textviews will change dependent on the previous view. I am having trouble getting Heading 2 and its textview to layout beneath heading 1 and its text view (this is all contained within a scrollview). Any ideas on a good way to ensure that these textviews layout appropriately? I want the textviews to be able to wrap height, moving all views "below" it further down the view. (Note: I have tried, via google-fu, a number of different methods involving things along the lines of the following without much success:
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;
_scrollView.contentSize = CGSizeMake(frame.size.width, 10+CGRectGetMaxY(frame));
)
Goal:
Bad Layout using above code:
Not much to go on, but it looks like you may not have "moved" the second label and text view down to compensate for the now larger upper text view. Try changing the frame of the lower label and text view so that the Y value places them down far enough to clear the upper text view.

Resources