UITextView reporting incorrect height - objective-c

I have a UITextView with a width of 240. Monitoring the text width shows that it is wrapping the text when the text width >= 224.
In this situation, where the UITextView wraps the text onto the next line, but the length of the text is not greater than 240, the text height is being reported as if it was a single line of text, rather than the height of two lines (since the UITextView wrapped it).
Any ideas why this is happening and how i can resolve it?

UITextView is a subclass of UIScrollView and there are some default insets applied—8 pixels on both the left and right side. I have never managed to find a way to get rid of them.

Related

Objective C multiline UILabel is not working

Here I have attached the scenario of my issue.
Current View
Here Top Title is a bit long string. But my view is shown partially.
Here I added the storyboard properties that I used for my UILabel.
Can someone suggest a solution to make this UiLabel as multilined one ?
You can't have a multi-line label with a fixed font size, if that label is constrained to a certain height.
Either the height needs to scale to allow more lines at that fixed font size, or the font size needs to scale down to fit all the text in.
If neither are possible, then the text will be truncated.
If your label is constrained to a certain height, then you can change the Autoshrink setting from Fixed font size to Minimum font size and specify the smallest size that you will allow the font to shrink to in order to accommodate longer strings.
Try to change the relation of constraint height: Greater than or equal

iOS 7 Autolayout does not correctly resize UILabel when font size is large

I used the solution here (UILabel sizeToFit doesn't work with autolayout ios6) to get the UILabel to fit the text.
However, when I increased my font size, the height did not fit any more.
I am not familiar with typography, but I guess the padding within the bounding box of the text always exists, and it is just not obvious when font size is small.
In my project, the text in the UILabel switches between two fonts, and the difference of padding makes it so hard to align it with other controls on the same view.
So my question is, how can I get rid of the padding-top and padding-bottom in a UILabel with large font-size?
Simply, you can't.
But you can decrease the Height accordingly as font increases using following formula,
Int heightToDecrease = FontSize - 12; //Change the value to fit your font family.
lblText.frame = CGRectMake(lblText.frame.origion.x, lblText.frame.origion.y, lblText.frame.size.width, lblText.frame.size.height - heightToDecrease);
I know this is not the perfect solution but it will help you for sure.

iOS7 spellcheck bug in UITextView

I have editable UITextView that expands as I start writing into multiple rows. At some point the height of UITextView reaches predefined maximum height and becomes scrollable. While UITextView was not scrollable everything looked nice.
When text view becomes scrollable, weird thing starts happening with spellcheck underlining red dots. Details on attached image.
Can anybody help me with this? I'm sure someone has encountered it so far.
It is clearly a bug that was introduced in iOS 7 (still there for 7.0.6).
As soon as you make UITextView scrollable, spellchecker underlines are getting mashed. If you set
textView.scrollEnabled = NO;
then it's fixed.
Solution is to put your text view into scroll view and resize scroll view's content size when text grows.
Here are some screenshots. I've also made views coloured so that you can see the bounds of all text view's subviews. Interesting that with .scrollEnabled set to YES text is inserted in some other UIView which height is being calculated wrong.
.scrollEnabled == YES
.scrollEnabled == NO

UITextView Text cut off when using setContentInset

I'm trying to shift the text in my UITextView right by 20 pixels to create a text indent using the code below
[textview setContentInset:UIEdgeInsetsMake(0, 20, 0,-20)];
But this is cutting off the right hand side of my text as shown in the screenshot. Can any help me stop this happening please
I think your edge insets are a bit off. Edge insets work towards the middle of the rectangle. For the right inset you should try 20 rather than -20.
[textview setContentInset:UIEdgeInsetsMake(0, 20, 0, 20)];
I couldn't find a solution to your problem even by subclassing UITextView and blocking setContentSize which seemed to be the problem.
[textview setContentInset:UIEdgeInsetsMake(0, 20, 0, rightInset)];
No matter what is the value of the rightInset the text is always clipped. Something is always redrawing text so that it fits the frame width of the text view.
But I am wondering why do you need to set the inset? Can't you just set the background of the UITextView object to clear color and position it properly on your background view to get the illusion of indentation?

How to Know the maximum font size allowed for a given font name?

Is there any way to know the maximum size a font allowed.
I am doing
textInputTextView.font = [UIFont fontWithName:currentFontName size:doubleFontSize];
frame.size = textInputTextView.contentSize;
For different fonts I am getting wrong contentSize when doubleFontSize is more than 70. For some fonts I get wrong contentSize when doubleFontSize variable is only 40.
I am guessing textView.font = doubleSizeFont is too big for those particular fonts. All are ok when font size is small between 10 to 30.
Is there any way to know the maximum size allowed by a particular font?
Details:
I need an image from UITextView. I am using UIGraphicsGetImageFromCurrentImageContext image is producing ok. But I realized image quality is low. So I tried to make the TextView frame and font double.
When I Write in big font "I am a good boy" if good is the right most word. I get "I am a boy" the right most word good disappears I get some blank space on right side of the image. How tragic :)
You Can Calculate height/Width of the content by using the following code.Then resize your frame. Here my Width is fixed (290) and Calculating the height.
CGSize strSize= [DataToBeDisplayed sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:CGSizeMake(290.0,99999.0)];
frame.size.height = strSize.height
I believe the issue that you are having is because you are using an UITextView. Text views inherits from the UIScrollView so it would not decrease the size of your font, it would only adapt its contents size and make your text scrollable.
Use an UITextField instead which also inherits from the UIView (thus can be used as an image) but allows for greater control. Fix its size to the maximum you would like it to be on screen and then working with both the [mylabel adjustsFontSizeToFitWidth:Y/N] and the [mylabel setminimumfontsize:somefontsize] methods, constrain it if too much text is entered. This will yield you a crisp big font and dynamically fit it upon input
~/End of Line