Build UITextview with Line - iphone

Does anyone idea about how can create UItextivew with Line like so user can easily write text in proper format.
please check this link
https://devforums.apple.com/message/217857#217857
is this possible, i have no idea about this.
Thanks you,

Easiest solution is -
NSInteger distanceBetweenLines = 50; // Distance between each line
NSInteger startMargin = 20; // For any starting margin for our first line
for (int i = 0; i < numberOfLines; i++) {
UIView *lineView = [[UIView alloc] init];
[lineView setFrame:CGRectMake(0, distanceBetweenLines * i + startMargin,
200, 1)];
[lineView setBackgroundColor:[UIColor blackColor]];
[self addSubview:lineView];
[lineView release];
}
This will create multiple UIView lines over your text view. The alternative and perhaps easier way (albeit less flexible) to do it is:
// linesImage is an image with a white background and black lines
UIImageView *linesImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"linesImage"]];
[linesImageView setFrame:CGRectMake(x, y, width, height)];
[self addSubview:linesImageView];
[linesImageView release];
// Just need to maker sure the textview is added to the view after imageview is created to make sure it is on top... or just bring the textview forward in your subviews stack
textview.backgroundColor = [UIColor clearColor]; // So that lines image can be seen through the text view
Apologies if the code doesn't compile, as I haven't tested it in Xcode.
Cheers,
Raal
Dapp - the app design app

Related

How to show multiple images inside UIScrollView in iOS

I am making one iPhone app in which i need to show some images inside UIScrollView, on left and right there will be two buttons user can click on button and it will show the next or previous image. Also at the bottom there is one button, when user will click on that button it will show the image which we have selected in scrollview.I would like to know how do we show multiple images inside that scrollview and while selecting bottom button, how to find which image was there inside scrollview.
An example from danielbeard.wordpress.com
In case your image names aren't just numbers:
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scrollView setPagingEnabled:YES];
[scrollView setAlwaysBounceVertical:NO];
NSArray *imagesArray = [NSArray arrayWithObjects:#"img1.png", #"img2.png", #"img3.png", nil];
for (int i = 0; i < [imagesArray count]; i++)
{
CGFloat xOrigin = i * scrollView.frame.size.width;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, scrollView.frame.size.width, scrollView.frame.size.height)];
[imageView setImage:[UIImage imageNamed:[imagesArray objectAtIndex:i]]];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[scrollView addSubview:imageView];
}
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width * [imagesArray count], scrollView.frame.size.height)];
have look on to this example:
http://danielbeard.wordpress.com/2012/09/17/adding-a-uiscrollview-to-a-uiview-ios/
Apple.developer PhotoScroller demonstrates the use of embedded UIScrollViews and CATiledLayer to create a rich user experience for displaying and paginating photos that can be individually panned and zoomed.
CATiledLayer is used to increase the performance of paging, panning, and zooming with high-resolution images or large sets of photos.
you just simply first
create a new view on runtime and image and assign tag all views like 1000 or 100 and increment on tag value and then add this view to scrollview and two buttons for left and right moving and make their outlet actions and add all the scrollview sub views in to an mutable array like this
indexStart=100;
UIView *AddView=[[UIView alloc]initWithFrame:CGRectMake(55, 0, 100, 100)];
AddView.tag=indexStart;
btnTap.tag=indexStart;
UIImageView *imgview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
[AddView addSubview:imgview];
imgview.image=image;
imgview.tag=1000;
[ScrollView addSubview:AddView];
[imgArray addObject:AddView];
you use loop to do this and on left and right button you use this code and in this code editImgeView is the image view inbetween left and right button where you show the image you just simple indexstart ++ or -- if user choose right or left button
for (int index = 0; index < [imgAddArrayAfter count]; index ++ ) {
NSLog(#"index %d",index);
UIView *vc=[imgArray objectAtIndex:index];
NSLog(#"view tag %d",vc.tag);
if(vc.tag == indexStart)
{
UIImageView *modalView=(UIImageView *) [vc viewWithTag:1000];
editImgeView.image=modalView.image;
}
}
i hope you understand ;-)
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scrollView.contentSize = CGSizeMake(320, 465);
[scrollView setScrollEnabled:YES];
[scrollView setPagingEnabled:YES];
[scrollView setAlwaysBounceVertical:NO];
[self.view addSubview:scrollView];
NSMutableArray *arrImage = [NSMutableArray arrayWithObjects:#"food1.jpeg", #"food2.jpeg", #"food3.jpeg",#"food4.jpeg", #"food5.jpeg", #"food6.jpeg",#"food7.jpeg", #"food8.jpeg", #"foo9.jpeg",#"food10.jpeg", #"food11.jpeg", #"food12.jpeg",#"food13.jpeg", #"food14.jpeg", #"food15.jpeg", #"food16.jpeg", nil];
for (int i = 0; i < [arrImage count]; i++)
{
CGFloat xOrigin = i * scrollView.frame.size.width;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, scrollView.frame.size.width, scrollView.frame.size.height)];
[imageView setImage:[UIImage imageNamed:[arrImage objectAtIndex:i]]];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[scrollView addSubview:imageView];
}
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width * [arrImage count], scrollView.frame.size.height)];
}
I know this thread is old, but I think the problem with loading lot's of images in the scroll view still exists. If you ever want to load images in the scroll view you will face a problem of memory usage. The images most likely will consume most of your device's memory. The only way of doing is to create reusable scroll view that works similar to the uitableview - the scroll view that has pool of views (with images) ready to display. This can be quite complex to create.
I have created open source project to support reusable loading images in the scroll view. It is very easy to implement. You need to call couple of delegate and data source methods and that's it. There is also sample project for obj-c and swift in order to make easy to understand. Please feel free to contribute. I hope it will be useful.
here is a source: https://github.com/sumofighter666/ReusableScrollView

UIScrollView not scrolling all the way

I have programmed a UIScrollView to scroll 320x800 however it stops scrolling a certain way down, even if I change the size in code it still stops scrolling the same way down, my only idea is the view is 548px in height. Here is the code I currently have in 'ViewController.m'.
[ScrollView setScrollEnabled:YES];
[ScrollView setContentSize:CGSizeMake(320, 800)];
This code currently holds in - (void)viewDidLoad. All the links are made in Interface Builder and I get no errors while compiling. Just thought I should say I'm in Xcode 4.5.2.
Any help is appreciated, thanks.
Edit: I should say that the UIScrollView still scrolls but only some way.
You get a clearer picture of what's wrong by trying the following...
- (void)viewDidLoad {
[super viewDidLoad];
[ScrollView setContentSize:CGSizeMake(320, 800)]; // consider renaming to lowercase 'scrollView'
for (int i = 50; i < 800; i += 50) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, i, 100, 22)];
label.text = [NSString string withFormat:#"label at y=%d", i];
[ScrollView addSubview:label];
}
}
Note that the loop ends at 750, so that's the last label you should see.

How to add View below other view in ViewController in iOS?

In my app i am creating view controller with mixed of UILabel and UITextview which want to be scrollable because the text are dynamic and also exceeds vertical screen size.
I am currently having Scrollview which has subviews as below. Views are created in Xcode 4.3 Storyboard.
UILabel1(Say Heading)
UITextView1(Dynamic text which can be any size)
UILabel2(Second Heading)
UITextView2(Dynamic text which can be any size)
and so on.
The problem is
When the UITextView1 has more content then it overlaps with UILabel2 which i don't want.
I would like to have UILabel1 on top of scrollView and UITextView1 below the UILabel1. UILabel2 below UITextView1 and so on.
What i have to do to achieve this?
EDIT
In Storyboard
![enter image description here][1]
In Simulator
![enter image description here][2]
Thanks for your help guys. Much appreciated.
Code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[scrollView setScrollEnabled:YES];
[self.view addSubview:cockTailNameLabel];
[self.view insertSubview:txtIngredients belowSubview:cockTailNameLabel];
[self.view insertSubview:scrollView belowSubview:cockTailNameLabel];
//[scrollView]
[self.cockTailNameLabel setText:self.passcockTailName];
[_txtUse setText:self.passUse];
[_txtUse setEditable:NO];
[_txtUse setUserInteractionEnabled:NO];
CGRect useFrame = _txtUse.frame;
useFrame.size.height = _txtUse.contentSize.height;
_txtUse.frame = useFrame;
[txtIngredients setText:self.passIngredients];
[txtIngredients setEditable:NO];
[txtIngredients setUserInteractionEnabled:NO];
CGRect ingredientFrame = txtIngredients.frame;
ingredientFrame.size.height = txtIngredients.contentSize.height;
txtIngredients.frame = ingredientFrame;
[txtRecipe setText:self.passReceipe];
[txtRecipe setEditable:NO];
[txtRecipe setUserInteractionEnabled:NO];
CGRect recipeFrame = txtIngredients.frame;
recipeFrame.size.height = txtRecipe.contentSize.height;
txtRecipe.frame = recipeFrame;
[scrollView insertSubview:_txtUse belowSubview:cockTailNameLabel];
[scrollView insertSubview:titleIngredients belowSubview:_txtUse];
[scrollView insertSubview:txtIngredients belowSubview:titleIngredients];
[scrollView insertSubview:btnReceipe belowSubview:txtIngredients];
[scrollView insertSubview:btnHome belowSubview:txtIngredients];
[scrollView insertSubview:txtRecipe belowSubview:btnHome];
[scrollView insertSubview:btnfacebookShare belowSubview:txtRecipe];
[scrollView insertSubview:btnTwitterShare belowSubview:txtRecipe];
/*[scrollView addSubview:_txtUse];
[scrollView addSubview:titleIngredients];
[scrollView addSubview:txtIngredients];
[scrollView addSubview:btnReceipe];
[scrollView addSubview:btnHome];
[scrollView addSubview:txtRecipe];
[scrollView addSubview:btnfacebookShare];
[scrollView addSubview:btnTwitterShare];*/
[scrollView setContentSize:CGSizeMake(320, 1000)];
NSLog(#"RecipeName :%# ",passcockTailName);
}
In Storyboard or IB you can rearrange them freely.
In code you do - (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview.
In code (in viewDidLoad):
UIScrollView *scroll =[[UIScrollView alloc] initWithFrame:CGrectMake(0,0 320, 480)];
[self.view addSubview:scroll];
// code to init your UI object
UILabel *uilabel1 = [[UIlabel alloc] initWithFrame:CGrectMake(10,10, 100, 40)]; // example
uilabel1.font = [UIFont systemFontOfSize:10];
uilabel1.text = #"UILabel1";
uilabel1.backgroundColor = [UIColor clearColor];
//
//
UILabel *uilabel2 = [[UIlabel alloc] initWithFrame:CGrectMake(10, 10 + 10 + uilabel1.frame.origin.y, 100, 40)]; // example
uilabel2.font = [UIFont systemFontOfSize:10];
uilabel2.text = #"UILabel2";
uilabel2.backgroundColor = [UIColor clearColor];
//
//
[scroll addSubview:uilabel1];
[uilabel1 releale];
[scroll addSubview:uilabel2];
[uilabel2 releale];
//
//
// in end
float offset = 10.0 * 2; // offset between uiobjects, N - number of objects
scroll.contentSize = CGSizeMake (0, 0, uilabel1.frame.size.height + uilabel2.frame.size.height + offset, 320);
Note that you may set frame (and other properties) of yours uiobjects and adds it in order to descend.
You can tell next view to start from the end of the first view like so:
startYOfNextView = firstView.frame.origin.y position + firstView.frame.size.height;
Do the same for the rest of the other view. If your view has variable text length, you may need to precaculate the height of the string based on a specific font e.g.:
CGSize maxSize = CGSizeMake(widthOfView, 9999999);
CGSize expectedSize = CGSizeMake(stringVar sizeWithFont:[UIFont fontWithName:#"Arial"] withMaxSize:maxSize lineBreakMode:UILineBreakModeWordWrap);
then tell your dynamic view to use the height of expectedSize variable like so:
myDynamicView = [[UILabel alloc] initWithFrame:CGRectMake(..., expectedSize.height)];
Your issue is labels are coming on top of the textview (Overlapped), right?
In This case, you are modifying the height of the textview dynamically. If its just for display purpose you can set the text to a UILabel with numberOfLines = 0 instead of UITextview; As its added to scrollview adding a label will be fine.
Also you need to modify the origin.y of the remaining views so that it will be properly aligned. ie, secondView.origin.y = firstView.origin.y + firstView.size.height + offset. Likewise modify the origin of the remaining views wrt the just above view. So that its origin will lie outside the previous view frame.
In my case I had to project it over the parent view, so remember there is also something,
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview
I think adding a transparent UI button is the fastest solution. You could add it once and then show / hide whenever you need to disable all views below the current one (assuming the "current" view, say a popup, was added last to your superview).
Declare an instance variable UIButton* _parentDisablingOverlay; in your popup view, then add this in its init or layoutSubviews method:
_parentDisablingOverlay = [[UIButton alloc] initWithFrame:CGRectMake(0,0, self.superview.frame.size.width, self.superview.frame.size.height)];
[self.superview insertSubview:_parentDisablingOverlay belowSubview:self];
_parentDisablingOverlay.hidden = true;
Later on, when you want to show the popup and disable everything below:
- (void) open {
self.hidden = false;
_parentDisablingOverlay.hidden = false;
}
Similarly to close and re-enable everything:
- (void) close {
self.hidden = true;
_parentDisablingOverlay.hidden = true;
}

how to get current uilabel text from uiscroll view runtime

I have task to develop the application with words counting and display in runtime generated uiscrollview with uilabel as subview.
The process is like when user will load page at that time the 1000 of word will fill in the rutime generated uilabel with scrollview.And all thing is set from runtime. but it the application we have one button to add the current uilabel text on button click.
and the words are coming random from database. when i click on the button it gives me the different word. from scroll view displayed word.
Following is my code to fill the data in uilabel with scrollview :
wordName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement, 2)];
[wordNameArray addObject:wordName];
int i = 0;
for (i = 1; i <= kNumImages; i++)
{
randomN = [wordNameArray objectAtIndex:arc4random() % [wordNameArray count]];
UIImage *image = [UIImage imageNamed:#"word_bg.png"];
word = [[UILabel alloc]initWithFrame:CGRectMake(15,12,250,120)];
[word setFont:[UIFont fontWithName:#"Helvetica-Bold" size:36.0f]];
word.textColor = [UIColor whiteColor];
word.lineBreakMode = UILineBreakModeWordWrap;
word.numberOfLines = 2;
word.textAlignment = UITextAlignmentCenter;
word.backgroundColor = [UIColor clearColor];
[word setText:randomN];
lblWord.text = word.text;
word.tag = i;
NSLog(#"tag no:%d",word.tag);
imageView = [[UIImageView alloc] initWithImage:image];
[imageView addSubview:word];
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i;
//NSLog(#"%#",word.text);
//NSLog(#"%#",lblWord.text);
// [scrollView1 addSubview:l];
[scrollView1 addSubview:imageView];
touchToSee.hidden = TRUE;
[imageView release];
}
[self layoutScrollImages];
Following is my scroll event to get the word from label but it is giving me last word from array:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
touchToSee.hidden = TRUE;
NSLog(#"word-%#",word.text);
}
And other when i click on button i am not getting the perfect work so for that following is my code:
NSString *getword = [NSString stringWithFormat:#"%#",word.text];
NSLog(#"lblCountdownBackward:%#",word.text);
So please help me and provide me some sample code for it if it possible.
thanks in advance.
You appear to be creating lots of word objects, and adding them to the screen.
In your for loop, you perform your code upto kNumImages. And in each run, the word Object is replace with a new one.
So when you get the word later on, it will be the very last word object you created (hence the last word).
To get arround this you could use an NSMutableArray and keep adding a new UILabel object to that (instead of creating the word object). And then later on do something like
[array objectAtIndex:0].text;

can't add subview to UIScrollView

I am trying to create a horizontal scroll view which has a UILabel as elements, by doing the following in viewDidLoad:
for(int index=0; index < [self.category count]; index++)
{
UILabel *label = [[UILabel alloc] init];
label.text = [self.category objectAtIndex:index];
label.textColor = [UIColor blackColor];
CGSize maximumLabelSize = CGSizeMake(100,9999);
CGSize expectedLabelSize = [[self.category objectAtIndex:index] sizeWithFont:label.font
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];
label.frame = CGRectMake(5+xOffset, 0, expectedLabelSize.width, 40);
self.scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,110);
[self.scrollView addSubview:label];
xOffset += 170;
}
However, I can't see anything when I run the app in the simulator, what am I missing here? Pretty sure that the UIScrollView is connected via the IBOutlet and I know that the text exists as I tried printing that out via NSLog
UPDATE:
Also how do I check which UILabel is clicked? I wanted to know this as well..
I think you did'nt initialize xOffset=0 before running for loop. so that it is taking a garbage value and then executing xOffset += 170; instruction.
so please initialize xOffset=0;
If you're positioning the labels with a frame you should also set autoresizingMask to make sure if you position based on a portrait layout but are testing the app in landscape, your labels don't get auto-positioned outside the new bounds. Also, make sure to autorelease those labels or else you'll be leaking memory.
Your expectedLabelSize calculation seems to adjust the height, leaving the width at 100. When you set the frame of your UILabel you are just using this width.