Textarea scrollHeight returning incorrect value - textarea

I have an example where I want to show the height of the text area based on the content.
I see a gap at the end, where the textarea height is more than the content.
http://jsfiddle.net/Nd6B3/55/
function autoheight(a) {
$(a).height($(a).prop('scrollHeight'));
}

Related

Get the content height of a UITextView

I use this open source library called ReadMoreTextView to display a bunch of text. The library enables me to toggle between displaying an excerpt of the text and showing the full length content (the button is a normal UIButton added by me, not the library).
This works as expected.
My problem arises when I have to display a smaller amount of text. If I add some content that doesn't exceed the height of the UITextView, I want to hide the more/less toggle button. So I thought of taking the full content height and if only it's larger than the text view's height, show the toggle button.
In the above example, I aded a long couple of paragraphs that exceeds the text view bounds. The text view's height comes up as 128. But the content height also returns 128. There's a library specific method called boundingRectForCharacterRange which is supposed to return the content height also returns a wrong value (100).
print("TEXTVIEW HEIGHT: \(textView.bounds.height)") // 128
print("CONTENT HEIGHT: \(textView.contentSize.height)") // 128
let rect = textView.layoutManager.boundingRectForCharacterRange(range: NSRange(location: 0, length: textView.text.count), inTextContainer: textView.textContainer)
print("TEXT HEIGHT: \(rect.height)") // 100
I opened an issue at the library's Github page but the owner asked to ask it here.
Why does the content height return a wrong value?
Here is the project I'm using in the above example by the way.
You can simply use following method to get the content size:
let contentSize = self.textView.sizeThatFits(self.textView.bounds.size)
Then update the textview frame accordingly:
self.textView.frame = CGRect(width: contentSize.width, height: contentSize.height)

How to convert height to WKWebView's coordinate

I changed WKWebView's contentInset so i can add a headerView.but web can't be clicked within insets.smileEvday adviced me to add a empty div in HTML string, at the beginning of the body.if contentInset.top is equal to 100,height of the empty div should be 100px when using UIWebView.But I failed to calculate the height of the div when using WKWebView.if contentInset.top is equal to 100, how to calculate the height of the empty div?

How to get content height of UITextView with word wrapping

I am trying to get the content height of an UITextView for calculating the bottom position of it (y position + height).
I can get the y position just fine with
travelDescriptionTextView.frame.origin.y
and need to determinate the height. All over the internet it says to get the content height with:
let contentSize = self.travelDescriptionTextView.sizeThatFits(self.travelDescriptionTextView.bounds.size)
let height = contentSize.height
but this technique doesn't work when height is resized (extended) by word wrapping, that is, if a sentence is wider than the width of the text box and the textbox creates a new line automatically. The above technique for getting content height only get the height right if there is no word wrapping, or else the height is excluding the extra word wrapping lines causing the content height to be shorter than the actual content height.
So how do I get the content height of a UITextView containing word wrapping height resizes?
Try like as follows,
let textViewComputedHeight = textView.contentSize.height - textView.contentInset.top - textView.contentInset.bottom
Thanks to RDC for the link. The working solution is:
self.travelDescriptionTextView.sizeToFit()
self.travelDescriptionTextView.layoutIfNeeded()
let contentSize = self.travelDescriptionTextView.sizeThatFits(self.travelDescriptionTextView.bounds.size)
let textViewHeight = contentSize.height

How can I check out-of-sight characters in UITextField?

I have a UITextField that shrinks and expands when user input text. If the textfield's width reach the screen width, I want it to be right-aligned so user can see the last input characters. In other circumstance, I want it to be left-aligned.
Because the textfield's maximum width is not exactly the same with screen, I need to find a way to check if it has characters out of visible area.
Is there anyway to achieve this?
You could try checking the width of the string that is in the field and comparing it to the width of the field itself. It would look something like this -
CGSize textSize = [self.field.text sizeWithAttributes:#{NSFontAttributeName:self.field.font}];
// If the text is larger than the field
if (textSize.width > self.field.bounds.size.width) {
// There is text that is not visible in the field.
}
This is fairly rough, but should get you close.

dynamically displaying table cells height with boundRectWIthSize

Right now, I'm using the following code to display my table cell. It is displaying text properly. However, there are some texts which are too long, so I want to display the first 5 lines of the text and then if the user expands the cell, it will display the whole text. I'm stuck because I am not so familiar with the new method in ios 7, boundRectWithSize.
CGSize size = CGSizeMake(self.reviewComments.width,999);
CGSize textRect =[self.reviewComments.text boundingRectWithSize: size options: NSStringDrawingUsesLineFragmentOrigin
attributes: #{NSFontAttributeName:self.reviewComments.font} context: nil].size ;
float height = textRect.height;
self.reviewComments.height = height;
I tried:
if (height > 150) {
height = 150;
}
But this way just cuts off the text, even after when I expand it.
UPDATE/EDIT:
I want my cell so that it only displays maybe the first 5 lines of the text if it exceeds 5 lines. The entire text will appear if the cell is expanded.
Try this:
float height = ceilf(textRect.height);

Resources