scrolling CoreText in UIScrollView - iphone

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.

Related

set the width of a UIText view based on the content size property

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

UIScrollView dynamically resize UILabel

i have got a UILabel which is not static.
I want to resize my scroll view so that it fits the label.
Here is my idea now:
self.scrollView.contentSize = CGSizeMake(320.0, 92+self.contentLabel.frame.size.height);
92 stands for pixels from where my label start. (there is a heading too)
But it doesn't work, it seems to be connected with Interface Builder also.
Thanks guys.
You need to set the frame as well. The contentSize of the scrollview is just what is within, not the frame of the view itself. If the contentSize is greater than the frame it will result in scrolling.
CGSize buttonSize = CGSizeMake(320.0, 92+self.contentLabel.frame.size.height);
self.scrollView.contentSize = buttonSize;
self.scrollView.frame = CGSizeMake(0, 0, 320.0, buttonSize.width, buttonSize.height);

resizing text in UITextView

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

UIlabel linebreak with scrollview not correctly display if too long (iphone retina simulator)

i have UIlabel using below code, i don't know maybe its too long or not. but when it is scrolled, the uilabel get this result. How can i solve the problem? are there any limitation using uilabel?
CGSize maximumLabelSize = CGSizeMake(100,9999);
CGSize expectedLabelSize = [summaryBlogParsed sizeWithFont: contentSummaryLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode: contentSummaryLabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = contentSummaryLabel.frame;
newFrame.size.height = expectedLabelSize.height;
contentSummaryLabel.frame = newFrame;
contentSummaryLabel.numberOfLines = 0;
contentSummaryLabel.text = summaryBlogParsed;
[contentSummaryLabel sizeToFit];
contentScrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width*2 + CGRectGetMaxY(contentSummaryLabel.frame));
Edit:
This problem occur when i use Iphone Retina simulator.
It looks like labels overlaying on top of each other. You can try setting their backgroundColour to [UIColor clearColor] and their opaque property to NO to check that it is indeed the case. If it is then you will have to check your frame setting code.
If the whole thing is one label then you probably don't want a single label with so many lines, but you can try calling setNeedsDisplay to force redrawing.

What's the best way to set the x,y coordinate of UILabel but let it auto width/height on the iPhone?

I know exactly where I want to position my UILabel in a UIView, but I don't necessarily know what height/width I want it to have at compile time, so setting a width/height in initWithFrame is useless. I want to be able to just init its x,y point, and then use something like [myLabel sizeToFit] so it can automagically set the width/height based on my break mode and content.
The solution I'm looking for is how to set the UILabel's point of origin while setting it's width/height to a dynamic number based on the text set to the label.
Size the label first, then set its origin or center:
[myLabel sizeToFit];
CGRect frame = myLabel.frame;
frame.origin = CGPointMake(x, y);
myLabel.frame = frame;
or
[myLabel sizeToFit];
myLabel.center = CGPointMake(x, y);
If you already have the frame with dynamic size but want to change the origin:
CGRect myFrame = myLabel.frame;
myFrame.origin = aCGPoint;
myLabel.frame = myFrame;
If you need to calculate the size:
CGSize suggestedSize = [myString sizeWithFont:myLabel.font constrainedToSize:CGSizeMake(FLT_MAX, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap];
Of course, you can change the FLT_MAX to constrain to a specific width or heigh.
EDIT: I didn't realize you wanted to programatically calculate the size. Added a better explanation above.