UITextView subview UIImageView hiding entered text and scrolling with texts iPhone? - iphone

I have UITextView in my iPhone app. In the UITextView i have added UIImageView as subview. When the entered text reaches the final height the texts are scrolling to top. Instead the UIImageView (with image) also scrolling top. How can i handle to stop the image scroll and allow the text to scroll? Here is my code for your reference,
messageTextView = [[UITextView alloc] initWithFrame:CGRectMake(35, 5, 210, 30)];
messageTextView.delegate = self;
messageTextView.backgroundColor = [UIColor clearColor];
messageTextView.clipsToBounds = YES;
messageTextView.font = [UIFont fontWithName:#"Helvetica" size:14];
messageTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
textViewImageView = [[UIImageView alloc]initWithFrame: CGRectMake(0, 0, 210, 30)];
textViewImageView.image = [UIImage imageNamed: #"textbg.png"];
textViewImageView.contentMode = UIViewContentModeScaleToFill;
textViewImageView.contentStretch = CGRectMake(0.5, 0.5, 0, 0);
[messageTextView addSubview: textViewImageView];
[messageTextView sendSubviewToBack: textViewImageView];
[messageTextView addSubview:textViewLabel];
Can anyone please help me to solve this? Thanks in advance. Looking forward your reply.

have you considered to position the imageView behind the textView? Or is there any reason that forbids such a layout?
something like that:
UITextView *messageTextView = [[UITextView alloc] initWithFrame:CGRectMake(35, 5, 210, 30)];
// ...
messageTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:messageTextView];
UIImageView *textViewImageView = [[UIImageView alloc] initWithFrame:CGRectMake(35, 5, 210, 30)];
// ...
textViewImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:textViewImageView];
[self.view sendSubviewToBack:textViewImageView];

Maybe this could help.
Use the scrollview delegate to reposition your image when it reaches the bounds.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height) {
CGRect imageFrame = textViewImageView.frame;
imageFrame.origin.y = scrollView.contentOffset.y - (scrollView.contentSize.height - scrollView.frame.size.height);
textViewImageView.frame = imageFrame;
}
}

This should work:
[yourTxtView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"your image.png"]]];

Related

How to size a title label to the full width of the navbar

I have a title label in the navbar using the code below. I can't seem to get it to size to the width of the full view. Any idea how I might go about fixing this?
I thought self.view.bounds.size.width would size it to the full width of the view.
here's my navbar title label code and there's also a screenshot to visually show the problem.
thanks for the help
UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, 48)];
tlabel.text=self.navigationItem.title;
tlabel.text = #"someText";
tlabel.font = [UIFont fontWithName:#"DIN-Bold" size:20];
tlabel.textColor=[UIColor whiteColor];
tlabel.backgroundColor = [UIColor colorWithRed:(219/255.0) green:(52/255.0) blue:(31/255.0) alpha:1] ;
tlabel.adjustsFontSizeToFitWidth=YES;
tlabel.textAlignment = UITextAlignmentCenter;
self.navigationItem.titleView=tlabel;
after code change from Paras Joshi it's correct on the right side but off on the left.
Use this bellow code in which I've added UILabel in UIView and then set that view as a titleView...
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 48)];
UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 320, 48)];
tlabel.text=self.navigationItem.title;
tlabel.text = #"someText";
tlabel.font = [UIFont fontWithName:#"DIN-Bold" size:20];
tlabel.textColor=[UIColor whiteColor];
tlabel.backgroundColor = [UIColor colorWithRed:(219/255.0) green:(52/255.0) blue:(31/255.0) alpha:1] ;
tlabel.textAlignment = UITextAlignmentCenter;
[headerView addSubview:tlabel];
self.navigationItem.titleView = headerView;
or add as a subview like below.
[self.navigationController.navigationBar addSubview:tlabel];
Try it with:
UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, [UIScreen mainscreen].bounds.size.width, 48)];
May be it works.
you have imported uikit framework or not.import it to make it error free

how to add a textfield dynamically after clicking a button in iphone

How to add a textfields dynamically on clicking the button?
when clicking on the button textfield should appear with scrollview.
can anyone explain me how to do?
what have you tried??
May be this is what you are asking for.
-(IBAction)buttonClicked:(id)sender
{
UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(20, 20, 40, 21)];
[scrollView addSubview:textField];
[self.view addSubview:scrollView];
}
EDIT
IF not dynamically,
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(20, 20, 40, 21)];
[scrollView addSubview:textField];
[self.view addSubview:scrollView];
[scrollView setHidden:YES];
}
-(IBAction)buttonClicked:(id)sender
{
[scrollView setHidden:NO];
}
You can use below code to add Text Field programmatically..
UITextField * aTextfield = [[UITextField alloc] initWithFrame:frame]; // give your frame
aTextfield.borderStyle = UITextBorderStyleRoundedRect;
aTextfield.textColor = [UIColor blackColor];
aTextfield.font = [UIFont systemFontOfSize:17.0];
aTextfield.placeholder = #"Suchen";
aTextfield.backgroundColor = [UIColor clearColor];
aTextfield.autocorrectionType = UITextAutocorrectionTypeNo;
aTextfield.keyboardType = UIKeyboardTypeDefault;
aTextfield.returnKeyType = UIReturnKeySearch;
aTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;
aTextfield.delegate = self;
[scrollview addSubview:aTextfield];
In Button action, you can create the dynamic textfields and them to your scrollview.....
Take a look at my code.
http://rajneesh071.blogspot.in/2012/09/make-textfield-and-label-dynamically-in.html
According to this you can create number of textfield and label in scroll view, so you can change it according to your requirement.

adding a button in an UIView

I'm trying to add a button to a UIview, to cover the whole area of the view.. like this:
-(MBNHomeScreenButton*) initWithImage:(UIImage*)image
andHighlightedImage:(UIImage*)highlightedImage
andTitle:(NSString*)aTitle
withSelector:(SEL)actionButton
forDelegate:(UIViewController*)viewController{
self = [super init];
if (self) {
self.frame = CGRectMake(0, 0, 100, 120);
self.imageView = [[UIImageView alloc] initWithImage:image highlightedImage:highlightedImage];
self.imageView.center = self.center;
self.imageView.frame = CGRectMake(10, 0, HS_BUTTON_IMAGE_WIDTH, HS_BUTTON_IMAGE_HEIGHT);
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.highlighted = NO;
[self addSubview:self.imageView];
self.title = [[UILabel alloc] initWithFrame:CGRectMake(0, HS_BUTTON_IMAGE_HEIGHT, HS_BUTTON_IMAGE_WIDTH + 20, 30)];
self.title.text = aTitle;
self.title.textAlignment = UITextAlignmentCenter;
self.title.textColor = [UIColor whiteColor];
self.title.backgroundColor = [UIColor clearColor];
[self addSubview:self.title];
// This creates a transparent button over the view
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//button.backgroundColor = [UIColor redColor];
[button setFrame:self.frame];
[button addTarget:viewController action:actionButton forControlEvents:UIControlEventTouchUpInside];
[self addSubview: button];
}
return self;}
In the ViewController I create the View like this:
m_newsHSButton = [[MBNHomeScreenButton alloc] initWithImage:[UIImage imageNamed:NEWS_SHADED_IMAGE]
andHighlightedImage:[UIImage imageNamed:NEWS_HIGHLIGHTED_IMAGE]
andTitle:#"News"
withSelector:#selector(newsButtonPressed)
forDelegate:self];
m_newsHSButton.center = CGPointMake(m_backgroundView.frame.size.width / 4, m_backgroundView.frame.size.height / 4);
[backgroundImgView addSubview:m_newsHSButton];
Still... the newsButtonPressed doesn't get called.
Can you help?
Thanks!
====================
[edit]
Ok... figured it out.. Thanks guys.
It was the fact that I was trying to add the view as a subview to a UIImageView with the last line:
[backgroundImgView addSubview:m_newsHSButton];
Apparently, it won't get touches anymore.
Thanks for the effort guys!
Try making the button a #property or instance variable in your custom view. Then call [m_newsHSButton.button addTarget:self action:actionButton forControlEvents:UIControlEventTouchUpInside]; from the viewController.

UITextView auto-complete bubble location

I am confused regarding auto-complete bubble location while entering data in UITextView.
What i have observed is that depending upon its frame's origin , auto-complete bubble either comes on top or bottom. There is no fixed location, provided scrollEnabled is set to NO.
Here are the two links. Code is written in init()
http://www.flickr.com/photos/26021742#N00/6975525835/
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 150, 320, 41)] autorelease];
view.backgroundColor = [UIColor grayColor];
[self.view addSubview:view];
UITextView *growingTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 10, 200, 27)];
growingTextView.font = [UIFont fontWithName:#"Helvetica" size:13];
growingTextView.scrollEnabled = NO;
[view addSubview:growingTextView];
http://www.flickr.com/photos/26021742#N00/6975525727/
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 50, 320, 41)] autorelease];
view.backgroundColor = [UIColor grayColor];
[self.view addSubview:view];
UITextView *growingTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 10, 200, 27)];
growingTextView.font = [UIFont fontWithName:#"Helvetica" size:13];
growingTextView.scrollEnabled = NO;
[view addSubview:growingTextView];
Can anyone explain this observed behavior ???
I know that popovers and bubbles will always automatically adjust it's position for ease of use. It's already on Apple's code and in both images the bubble position makes sense.

Scroll view with an image is not scrolling

i added an image to the scroll view. but its not scrolling...
pls help out what is the problem..
Thanks.
boxImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 44, 320, 372)];
boxImage.image = [UIImage imageNamed:#"chapter1box.png"];
textScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 175, 320, 755)];
scrollTextImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 775)];
scrollTextImg.image = [UIImage imageNamed:#"chapter1narrationtext.png"];
textScroll.backgroundColor = [UIColor clearColor];
textScroll.scrollEnabled = YES;
textScroll.pagingEnabled = YES;
textScroll.directionalLockEnabled = YES;
textScroll.autoresizesSubviews = YES;
textScroll.contentSize = CGSizeMake(320, 775);
[self.view addSubview:boxImage];
[boxImage addSubview:textScroll];
[textScroll addSubview:scrollTextImg];
UIImageView has userInteractionEnabled property set to NO by default. Adding
boxImage.userInteractionEnabled = YES;
should help.
The scroll view size has to be smaller than the size of it's content for scrolling to work.
in the code i add the scrollview to an image view
i added it to the self.view its working now.
cant we add a scroll view to an imageview..?