How to increase max height of UITextView beyond 10000 in iOS? - ios

I have a huge text to display in iOS UITextView which makes the height around 40000 but in XCode it says it cannot support size larger than 10000 by 10000.
Any solutions?
Thanks.

The short answer is: you're doing it wrong.
A UITextView already allows scrolling through text. You don't make the view large enough to display the text, you make it large enough to fit on screen, and even in the macOS world, 10Kx10K is larger than any possible screen dimensions.

so many text that size over 10000 ,I advice you to use UiWebview or UILable replace UITextField. I hope I can help you.
-(UIWebView*)XueweiwebView {
if (!_XueweiwebView) {
_XueweiwebView = [[UIWebView alloc]init];
_XueweiwebView.frame = self.tableView.frame;
_XueweiwebView.delegate = self;
_XueweiwebView.scrollView.bounces = NO;
NSString* str = #"<p>here is many text you can plae 4000 charater or more</p>";
[_XueweiwebView loadHTMLString:str baseURL:nil];
[self.view addSubview:_XueweiwebView];
}
return _XueweiwebView;
}
NSString* str = #"<p> place your text 40000 </p>";

Related

How to adjust UILabel font size to fit the fixed Rectangle (for iOS7 +)?

I know this question has been asked so many times and I have seen most of the solutions like
How to adjust font size of label to fit the rectangle?
How to calculate actual font point size in iOS 7 (not the bounding rectangle)?
NSString sizeWithFont: alternative in iOS7
What I want is that I want to adjust the font size in a given rect with WORD WRAPPING technique. The posts I mentioned above somehow adjust the font size but none of them is using word wrapping technique. Let say, I have a label
UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 60, 25)]; \\60px width
tempLabel.text = #"Internationalisation";
tempLabel.numberOfLines = 2;
Now it should wrap the text in one line (within 60 px) with minimum possible font. But it keeps the text in 2 lines with a bit bigger font and the text breaks up like "Internationali" in first line and "sation" in second. How can we apply word wrapping in it.
tempLabel.adjustsFontSizeToFitWidth = YES;
does not seem to work for two or more lines and
CGSize size = [self sizeWithFont:font
minFontSize:minFontSize
actualFontSize:&actualFontSize
forWidth:maxWidth
lineBreakMode:self.lineBreakMode];
is deprecated in iOS7+
So how can I resolve my Issue. Please do help. This is so important for me.

Estimate character count in uitextview of non scrollable fixed size iOS

I have a scenario , where I want to calculate , How many characters can be placed within Non scrollable UITextView.
I have tried the following ,
Get the height of the Text that needs to be placed within UITextView
by the following code ,
CGRect rect = [labelStr boundingRectWithSize:CGSizeMake(label.frame.size.width, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
labelHeight = rect.size.height;
and also computed the height of my UITextView.
But still with these two height values , I cant figure out the way to find out the character count within my UITextView.
Any Ideas will be greatly appreciated....
Thanks in advance.
Though not very glamorous, you could just type characters to fill the UITextView in the simulator and add a button that would count them for you, a simple method like this perhaps:
- (NSUInteger)numberOfCharactersInString:(NSString *)textToCheck // uitextview.text
{
NSUInteger *numOfChars = [textToCheck length];
return numOfChars;
}
Then you'll know how many characters can fill the UITextView. But, this wouldn't be a very useful method if you're using preferred fonts that change when the user changes the Text Size in Settings.

Localized UILabel with background

I have an app in two languages (English and German) and app contains labels with background. In English everything is ok but in German UILabels are to short and text which is in it is shorter with 3 dots. Like:
Can I add some auto-resize property which resize label to correct size? Last one thing is that I have a lot of labels interface-only. I want to save some time and figure it out in interface. Is it possible or the only way is set sizeToFit to labels? Thank you for advices.
So what you want to do is, set the sizeToFit in the interface.
It is quite easily possible.
Set the AutoShrink Property in Attributes inspector, and it will be equivalent to size to fit.
Further you can even Specify the minimum font size and font scale.
Kakshil's answer will work for the case that you're not wanting to resize the label to actually fit the contained text. It's going to reduce the point size of the text in an attempt to fit the available space. For truly dynamic text that may be the best option.
On the other hand, if you're wanting to actually resize the label to fit the text then you can call [label sizeToFit] after you change the label text. Alternatively, you can use sizeThatFits: to use a modified bounds, where you don't want the label to grow too big or too small.
I customised my label, add localization and set frame with inset. How's simple. But one disadvantage is that I have to set tag as key. My solution is:
- (void)layoutSubviews {
NSString *key = [NSString stringWithFormat:#"%d", self.tag];
self.text = NSLocalizedString(key, nil);
CGSize textSize = [self.text sizeWithAttributes:#{NSFontAttributeName:self.font}];
CGFloat strikeWidth = textSize.width;
[self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, strikeWidth + 20, self.frame.size.height)];
[super layoutSubviews];
}
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {0, 5, 0, 5};
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}

Is there any similar solution to UIWebView?

I use a UIWebView in my app to present content, but now I can't do it anymore because I also need to use a UIScrollView and it creates conflicts related to scrolling.
Also, UIWebView is very slow.
So my question is : is there any another way to load string which contains HTML tags (strong, p, div, and etc...)?
UPDATE (improved explanation)
I have UIVIewController containing an image at the top, and under this image is a title and under the title is content (HTML string from web). The problem is that when the text is too big, the webview is scrolling, but not the whole page. I want the whole page to scroll, not just the webview.
You won't get anything faster or better than the native UIWebView. Probably you should overthink your UI/UX. What exactly do you want to achieve with a webview in a scrollview??
Perhaps attributed strings are enough for you. Look it up.
The OHAttributedLabel even parses HTML for you.
Solved.
-(void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *output = [webView stringByEvaluatingJavaScriptFromString:#"document.body.scrollHeight;"];
int webViewHeight = [output intValue];
//NSLog(#"height: %d", webViewHeight);
if(webViewHeight > 220){
CGRect frame = articleContentWebView.frame;
frame.size.height = webViewHeight;
articleContentWebView.frame = frame;
int scrollViewheight = 416 + (webViewHeight - 220);
[articleScrollView setContentSize:CGSizeMake(320, scrollViewheight)];
}
}
In delegate method webViewDidFinishLoad I calculate height od UIWebView content and then set height of UIWebVIew and height of UIScrollView.

Determine Font Size After Autoshrink

I have 2 UILabels for a scorekeeping app (Home and Away). The user can tap on Home or Away and change the label to be whatever they like. This is all working fine. When a user enters a name larger than the label size it will shrink the font to fit. This makes the 2 labels no longer match in font size, and doesn't look right. My question: How can you set the font size of one label to the size of another label that has been "autoshrunk"?
I also realize I will need to write code to determine which font size is smaller between the two labels and set them both to that number. I don't think I will have a problem with that, as long as I get an answer to the above question. It seems like it should be simple, but it has so far eluded me.
So I hope you know the frame size of both the labels.
Use – sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: to find the minimum font size that it would use.
If you have any doubt, please let me know.
For me this worked fine:
- (CGFloat)actualTitleFontSize
{
NSStringDrawingContext *labelContext = [NSStringDrawingContext new];
labelContext.minimumScaleFactor = 0.2;
NSAttributedString *attributedString =
[[NSAttributedString alloc] initWithString:label.text attributes:#{NSFontAttributeName : label.font}];
[attributedString boundingRectWithSize:label.frame.size
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
context:labelContext];
CGFloat actualFontSize = label.font.pointSize * labelContext.actualScaleFactor;
return actualFontSize;
}

Resources