I am trying to add a label to a scroll view, then set the contentSize of the scrollview to match the desired size of the label. The problem is I seem to have to make the contentSize bigger than the size of the label:
theScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,320,480)];
bodyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320.0, 50.0)];
bodyLabel.textColor = [UIColor whiteColor];
bodyLabel.backgroundColor = [UIColor clearColor];
bodyLabel.textAlignment = UITextAlignmentLeft;
bodyLabel.text = event.description; <- basically some long ass text here
bodyLabel.font=[UIFont systemFontOfSize:16.0];
bodyLabel.numberOfLines = 0;
bodyLabel.lineBreakMode = UILineBreakModeWordWrap;
here I think Im getting the necessary label size to contain the text
CGSize size = [event.description sizeWithFont:bodyLabel.font constrainedToSize:CGSizeMake(bodyLabel.frame.size.width, 9500) lineBreakMode:bodyLabel.lineBreakMode];
frame = bodyLabel.frame; // to get the width
frame.size.height = size.height;
bodyLabel.frame = frame;
here I set the contentSize to the label's Size, but it isn't big enough, I need to
set it to bodyLabel.frame.size.height + 80 for it to be covered in the scrolling.
theScrollview.contentSize = CGSizeMake(bodyLabel.frame.size.width, bodyLabel.frame.size.height);
[theScrollview addSubview:bodyLabel];
Thanks for any thoughts in advance!
[theScrollview addSubView: bodyLabel];
Related
Im trying to put an image behind a uilabel and need both to be about the same height (the uiimageview a little bigger to surround uilabel) and I have given both the same height in cgrectmake but yet the uiimageview remains smaller than the uilabel even though they are set to the same height.Please help! Code:
UILabel *labelEntry = [[UILabel alloc]init];
labelEntry.numberOfLines = 0;
labelEntry.text = entry;
CGSize expectedLabelSize = [entry sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:CGSizeMake(300.f, FLT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
CGRect lblFrame = CGRectMake(13, 30, 320, expectedLabelSize.height);
labelEntry.frame = lblFrame;
labelEntry.lineBreakMode = NSLineBreakByWordWrapping;
labelEntry.font = [UIFont systemFontOfSize: 17.0];
labelEntry.textAlignment = NSTextAlignmentCenter;
labelEntry.backgroundColor = [UIColor clearColor];
UIImageView *backgroundImage = [[UIImageView alloc]initWithFrame:CGRectMake(13, 30, 320, expectedLabelSize.height)];
backgroundImage.image = [UIImage imageNamed:#"postl.png"];
[scrollView addSubview:backgroundImage];
From your image(postl.png) what i can notice is, there is shadow effect applied to the image. Try once by setting background color for the imageview & you can see the shadow of applied image. Later you can adjust the frames accordingly
What is the size of post1.png? If it is not the same size as your CGRect you will need to stretch it. That would include changing your UIImageView's contentMode and possibly creating a stretchable image.
I am new to iphone development . Can anyone please tell me how to add a vertical scrollview inside a horizontal scroll view. I went through many samples but couldn't get a clear picture about it . I wan my view to be scrolled in both vertical and horizontal directions. Any help is appreciated.
Here is my code for scrolling:
EDIT: Here is my code for scrolling
self.firstScroll.pagingEnabled=YES;
self.firstScroll.clipsToBounds=YES;
int numberOfViews=3;
for(int i=0;i<numberOfViews;i++){
CGFloat xOrigin=self.view.frame.size.width*i;
UIView *awesomeView=[[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
awesomeView.backgroundColor=[UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[self.firstScroll addSubview:awesomeView];
}
self.firstScroll.contentSize=CGSizeMake(self.view.frame.size.width*numberOfViews, self.view.frame.size.height);
[self.view addSubview:firstScroll];
self.nextVerticalScroll.pagingEnabled=YES;
for(int i=0;i<numberOfViews ;i++){
CGFloat yOrigin=self.view.frame.size.height * i;
UIView *verticalView=[[UIView alloc]initWithFrame:CGRectMake(0, yOrigin, self.view.frame.size.width, self.view.frame.size.height)];
verticalView.backgroundColor=[UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[self.nextVerticalScroll addSubview:verticalView];
}
self.nextVerticalScroll.contentSize=CGSizeMake(self.view.frame.size.width, self.view.frame.size.height*numberOfViews);
[self.view addSubview:nextVerticalScroll];
Thanks,
Raj
You probably do not want to have scrollviews within scrollviews because you can't control which gets the horizontal gestures and which gets the vertical gestures (without a lot of hassle, at least). It is much easier to have a single scrollview with a contentSize that is bigger horizontally and vertically than the bounds of the scrollview itself, e.g.:
- (void)configureScrollView
{
NSInteger rows = 4;
NSInteger cols = 5;
CGFloat width = self.scrollView.bounds.size.width;
CGFloat height = self.scrollView.bounds.size.height;
// configure my scroll view itself
self.scrollView.contentSize = CGSizeMake(width * cols, height * rows);
self.scrollView.pagingEnabled = YES;
self.scrollView.backgroundColor = [UIColor darkGrayColor];
// now let's add the labels to the scrollview
for (NSInteger row = 0; row < rows; row++)
{
for (NSInteger col = 0; col < cols; col++)
{
// making my label just a little smaller than the scrollview's bounds so I can easily see the scrolling/paging
CGRect frame = CGRectMake((width * col) + 20.0, (height * row) + 20.0, width - 40.0, height - 40.0);
// create and configure the label
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.text = [NSString stringWithFormat:#"%1.0f", row * cols + col + 1.0];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor lightGrayColor];
// add it to my scrollview
[self.scrollView addSubview:label];
}
}
}
I'm just filling my scroll view with 20 different text labels (and colored the background of the scrollview differently from the labels so I could see them), but it demonstrates the basic idea.
Please if you have added Paging in XIB then remove it...I think it will help you.
textViewBusiness = [[UITextView alloc] initWithFrame:CGRectMake(25,332,268,60)];
textViewBusiness.text=strMyBusiness;
textViewBusiness.editable=NO;
textViewBusiness.font = [UIFont fontWithName:#"Arial" size: 17.0];
textViewBusiness.layer.borderWidth = 2.0f;
textViewBusiness.layer.borderColor = [[UIColor grayColor] CGColor];
textViewBusiness.backgroundColor = [UIColor clearColor];
[textViewBusiness setTextColor:[UIColor blackColor]];
[self.scrollView addSubview: textViewBusiness];
CGRect frame = textViewBusiness.frame;
frame.size.height = textViewBusiness.contentSize.height;
textViewBusiness.frame = frame;
with the increase in contents i want to increase the size of text field....
This code is not working for me ...
Thanks
You don't need to take UITextView for showing un-editable text. Besides this you can use UILabel and you can find out the height of label at run time. Each time the content vary in size set frame of UILabel accordingly.
Use this to find out height of your label at run time
- (CGFloat) heightOfTextLabel:(NSString *) contentText
{
CGSize constraint = CGSizeMake(268,4000);
CGSize size = [contentText sizeWithFont:[UIFont fontWithName:#"Arial" size: 17.0] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
return size.height;
}
This method would return you a variable height of the content each time.
Now set this height to your UILabel
CGFloat heightOfLabel = [self heightOfTextLabel:strMyBusiness];
UILabel* textToShowLabel = [[UILabel alloc] initWithFrame:CGRectMake(25,332,268,heightOfLabel)];
textToShowLabel.text=strMyBusiness;
textToShowLabel.font = [UIFont fontWithName:#"Arial" size: 17.0];
textToShowLabel.layer.borderWidth = 2.0f;
textToShowLabel.layer.borderColor = [[UIColor grayColor] CGColor];
textToShowLabel.backgroundColor = [UIColor clearColor];
[textToShowLabel setTextColor:[UIColor blackColor]];
[self.scrollView addSubview: textToShowLabel];
You need to divide your code into 2 fragments, then place them into proper places, then your code should work.
Fragment 1 (in my test, I place this fragment in viewDidLoad):
textViewBusiness = [[UITextView alloc] initWithFrame:CGRectMake(25,332,268,60)];
textViewBusiness.text=strMyBusiness;
textViewBusiness.editable=NO;
textViewBusiness.font = [UIFont fontWithName:#"Arial" size: 17.0];
textViewBusiness.layer.borderWidth = 2.0f;
textViewBusiness.layer.borderColor = [[UIColor grayColor] CGColor];
textViewBusiness.backgroundColor = [UIColor clearColor];
[textViewBusiness setTextColor:[UIColor blackColor]];
[self.scrollView addSubview: textViewBusiness];
Ensure that the above fragment run, and your text view is displayed in the screen, then run the second fragment. If your text view is not displayed in the screen, then the contentView is not initialized, and the height is undefined.
Fragment 2 (in my test, I place this fragment in viewDidAppear):
CGRect frame = textViewBusiness.frame;
frame.size.height = textViewBusiness.contentSize.height;
textViewBusiness.frame = frame;
Good luck!
GrowingTextView is a reusable iOS compenent that does exactly what you need, you can find it on GitHub here .
I hope this helps.
I have a view that is pushed onto the Screen via navigation controllers:
Inside is a UIScrollView.
Then inside the UIScrollView are a few static objects like Images and Labels.
Then comes the hard bit, There is a UITextView with its text loaded from different text files of varying length.
I need to be able to have the UITextView size dynamically to its contents, and the same for the UIScrollView. Is this possible?
float length = [yourText length];
textview.frame = CGRectMake(44, 87, 923, ceilf(length/142)*25);
Here 25 is the constant value assumed as text font width. From this you can set scrollview frame reference to the textview frame.
You can do that with the help of following code. I had done that code for Label and same way you can do that with the help of text-field.
NSString *cellText = "Text Of Your Text-Field";
UIFont *cellFont = [UIFont fontWithName:#"Your Font Name" size:FONT_SIZE];//UIFont *cellFont = [UIFont fontWithName:#"Helvetica-Bold" size:13.0];
CGSize constraintSize = CGSizeMake(#"Width Of Your Text-Field", MAXFLOAT);//CGSize constraintSize = CGSizeMake(220.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
int height = labelSize.height;
frame.origin.x = Starting Position of X;
frame.origin.y = Starting Position of Y;
frame.size.width = Width Of Your TextField;
frame.size.height = height;
UILabel *lblName = [[[UILabel alloc] initWithFrame:frame] autorelease];
lblName.numberOfLines = 0;
lblName.lineBreakMode = UILineBreakModeWordWrap;
lblName.textAlignment = UITextAlignmentLeft;
lblName.textColor = [UIColor whiteColor];
lblName.backgroundColor = [UIColor clearColor];
lblName.font = [UIFont boldSystemFontOfSize:13.0];
And same way you can do For the Scrollview. Just you have to set the Frame of that scrollview and you are done.
Yes you could do something like this:
if ([textView length] > int//any number you want) {
textView.frame = CGRectMake(//just adjust the size an position here);
scrollView.contentSize = CGSizeMake(//adjust scrollView size)
}
else if ([textView length] > int//just another number) {
// you can continue looping that for how often you want
}
For the int in the if statement you check how long the text is. Based on that you adjust the size of both the scrollView and textView.
I am trying to give my UILabel dynamic height so that my layout of other labels looks correct in both landscape and portrait.
In portrait, my text wraps to the second line, in landscape it does not. So, when using sizeWithFont:constrainedToSize:lineBreakMode: I get the same height when rotating both ways, when I had assumed it would be a larger number when the text was 2 lines.
How can I get the height of my UILabel when it has two lines of text or more (portrait) and get the new height which is one line, when in landscape?
I guess I am not understanding how to get dynamic height working...
UILabel *itemTitle = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, top, screen.size.width - 20, 200.0f)];
itemTitle.text = self.newsAsset.title;
itemTitle.adjustsFontSizeToFitWidth = NO;
itemTitle.autoresizingMask = UIViewAutoresizingFlexibleWidth;
itemTitle.font = [UIFont boldSystemFontOfSize:18.0];
itemTitle.textColor = [UIColor blackColor];
itemTitle.shadowColor = [UIColor whiteColor];
itemTitle.shadowOffset = CGSizeMake(0, 1);
itemTitle.backgroundColor = [UIColor blueColor];
itemTitle.lineBreakMode = UILineBreakModeWordWrap;
itemTitle.numberOfLines = 0;
[itemTitle sizeToFit];
// Set the height
CGSize maximumLabelSize = CGSizeMake(300,9999);
CGSize titleSize = [itemTitle.text sizeWithFont:itemTitle.font constrainedToSize:maximumLabelSize lineBreakMode:itemTitle.lineBreakMode];
NSLog(#"Height: %.f Width: %.f", titleSize.height, titleSize.width);
//Adjust the label the the new height
CGRect newFrame = itemTitle.frame;
newFrame.size.height = titleSize.height;
itemTitle.frame = newFrame;
// Add them!
[headerView addSubview:itemTitle];
[itemTitle release];
top += titleSize.height;
change the line where you set maximumLabelSize to
CGSize maximumLabelSize = CGSizeMake(headerView.bounds.size.width, CGFLOAT_MAX);
In your code as it is now, in either orientation you will get the same width and height, since you always pass a width of 300 to the sizeWithFont method. If you make it dynamic, maybe the result of the sizeWithFont will also change dynamically.