How do I resize the text in my UITextView so that it doesn't cut off the text? I have a UITextView set at a specific height and I want the text in that UITextView to fit in that height. In a UITextField you have a adjustSizeToWidth, but not in UITextView
CGRect frame = textView.frame;
frame.size.height = textView.contentSize.height;
textView.frame = frame;
Source: https://stackoverflow.com/a/2487402/253008
EDIT:
You can use
-(CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
to check the width of the text in the UITextView and then when it reaches your set width you can start lowering the font size
Related
TextView.contentSize.width does not work to set the UITextView's .frame.size.width.
[TextView setFrame:CGRectMake(TextView.frame.origin.x, TextView.frame.origin.y, TextView.contentSize.width, TextView.contentSize.height)];
Setting the UITextView's frame height to the contentSize.height property works to make the view's frame scale to the proper size for the current vertical size of the content. For some reason, the width of the view's frame does not respond in the same way. It just remains the same size regardless of the amount of text input.
When I log the contentsize of the UITextView dynamically, as I am typing in text to the view, the height property changes (at each line break), while the width does not. Makes me wonder what the width property is doing, what's it for.
Try this way..
UIFont *font = [UIFont fontWithName:#"Enriqueta" size:15];
NSString *text = #"Your text";
CGSize frameSize = [text sizeWithFont:font];
CGRect originalFrame = textViewA1.frame;
textViewA1.frame = CGRectMake(CGRectGetMinX(originalFrame), CGRectGetMinY(originalFrame), frameSize.width, frameSize.height);
As depending upon width of UITextView it should be like this:
UIFont *myFont = [UIFont boldSystemFontOfSize:15.0]; //your font specification here
NSString *strText = yourTextView.text;
CGSize strsize = [strText sizeWithFont:myFont
forWidth:yourTextView.frame.size.width
lineBreakMode:UILineBreakModeWordWrap]; //get string size
yourTextView.frame = CGRectMake(yourTextView.frame.origin.x,yourTextView.frame.origin.y,strsize.width,strsize.height+10); //change accordingly
I am experimenting with CoreText, one of the problems i have is that content isn't scrollable and idk how to make it scrollable... with labels this code works:
//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(300,9999);
CGSize expectedLabelSize = [labelText sizeWithFont:label.font
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeTailTruncation];
//adjust the label the the new height.
CGRect newFrame = label.frame;
newFrame.size.height = expectedLabelSize.height;
label.frame = newFrame;
my test project: http://dl.dropbox.com/u/47384598/AA_CoreText.zip
but what should i do with CoreText? Any help highly appreciated!
You should probably render your CoreText frames into a UIView. Then, add this UIView to your UIScrollView as a subview and set the contentSize of the UIScrollView such that the contentSize is large enough to fit your UIView holding the CoreText.
Note that if the contentSize of the UIScrollView is less than the size of the view on the screen, it will not need to scroll.
scrollView = [[UIScrollView alloc] initWithFrame:
[[UIScreen mainScreen] applicationFrame]];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(25, 100, 200, 250)];
textView .text =#"long text ";
I will be getting some data of unknown length. to be added to the UITextView . Sometimes the height of the content of the text might exceed the height of the UITextView which is 250 (shown above).
How can i increase the height of the UITextView based on the text i receive ? I would appreciate a sample code?
note: according to my code i am defining the width and height of the UITextView before adding the text to it.
You can use NSString's sizeWithFont:constrainedToSize:lineBreakMode: to get a CGSize struct telling you how big the text will be.
Usage:
CGSize size = [mystring sizeWithFont:[UIfont systemFontOfSize:15]
constrainedToSize:CGSizeMake(200, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
[myTextView setFrame:CGRectMake(25,100,200,size.height)];
I have created a custom view cell with a UILabel in it, I have set :
cell.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.titleLabel.numberOfLines = 0;
and it's not going in multiple lines, why is this?
in the layoutSubviews I have:
- (void)layoutSubviews {
[super layoutSubviews];
CGRect frame = titleLabel.frame;
frame.origin.y = 5;
titleLabel.frame = frame;
}
and that's about all the settings I have, however for a long text put in the label in the cell it just won't word wrap:
What is the height of the label's frame? It needs to be tall enough to hold more than one line of text. Check out Adjust UILabel height depending on the text
I have a UILabel and if i adjust the size of the text i can make it look loke a UITextView however the left margin is different, on the UIlabel the text is right up against the left border where the UITextView has a slight margin. How do i adjust the UILabel so that when these controls are placed above one another, they look consistent?
Simply change the label's frame:
CGRect frame = label.frame;
CGRect newFrame = CGRectMake(frame.origin.x + MARGIN, frame.origin.y, frame.size.width - MARGIN, frame.size.height);
label.frame = newFrame;
Of course replace MARGIN with whatever you want your margin to be.
Or you could subclass UILabel and override textRectForBounds:limitedToNumberOfLines: like so:
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
CGRect newBounds = CGRectMake(bounds.origin.x + MARGIN, bounds.origin.y, bounds.size.width - MARGIN, bounds.size.height);
return [super textRectForBounds:newBounds limitedToNumberOfLines:numberOfLines];
}
Hope this helps!