ios uiview uiimageview uitextview - ios

I have a main UIView that has an UIImageView on it, and the UIView adds an UITextView as a subview. UITextView's frame is smaller than UIView. UIImageView is actually a frame and the rest of the image is transparent and fits the full size of the UIView, but the UITextView is always upon UIView. What I want to achieve is UITextView shows UIView in foreground, while it is possible to scroll up and down on UITextView.
What is the proper way to make it possible?
My current code that does not work:
in loadView:
self.contentview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.contentview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"ChristmasFrame.png"]];
self.contentview.opaque = NO;
self.contentview.userInteractionEnabled = YES;
[self setView:self.contentview];
in viewDidLoad:
[self.contentview addSubview:self.textview];
[self.contentview sendSubviewToBack: self.textview];
I want UITextView to be behind self.contentview, so it could show its background image upon the UITextView.

try setting setUserInteractionEnabled: to NO on UIView what is on top of UITextView
if that not working you could try to override hitTest:withEvent:

make clear color as textview's background color.
and add text view above UIView

Related

How To Fix UITextView in storyboard TextView

Here i want to fix a UITextView which i created programatically in textView added on storyboard. I have tried it this way but not working.
_textView is a textView in which i have to fix UITextView *descrip
contentView is the view on which i have _textView.
UITextView *descrip = [[UITextView alloc]init];
descrip.view.frame = CGRectMake(_textView.frame.origin.x,_textView.frame.origin.y,_textView.frame.size.width,_textView.frame.size.height);
[self.contentView addSubview:descrip.view];
You can do something like this--
UITextView *descrip = [[UITextView alloc]initWithFrame:_textView.frame];
[self.contentView addSubview:descrip];
there is no need to to use CGRectMake if you want to give same frame as _textView frame.
here is proper method
UITextView *descrip = [[UITextView alloc]initWithframe:CGRectMake(_textView.frame.origin.x,_textView.frame.origin.y,_textView.frame.size.width,_textView.frame.size.height)]
[self.contentView addSubview:descrip];
If I understood correctly, you are trying to add descrip into_textView; try this:
UITextView *descrip =[[UITextView alloc]initWithFrame:CGRectMake(0, 0, _textView.frame.size.width, _textView.frame.size.height)];
descrip.backgroundColor=[UIColor blueColor];
[self.contentView addSubview:descrip]; //here add subview to texview not content view

Best way to dynamically change the width of an image view to match width of label text?

I'm constructing a UITableViewCell via a nib file. I also have a corresponding UITableViewCell subclass which the nib's outlets are connected to.
I need a dynamically sized UIImageView (only the width) to be the background of my tableviewcell's label. Is there a way this can be achieved via autolayout?
I've tried setting the imageview as a subview of the label in code:
[theLabel sizeToFit]
CGRect boundingRect = [theLabel.text boundingRectWithSize: theLabel.frame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:0 context:0];
UIImageView *background = [[UIImageView alloc]initWithFrame:boundingRect];
background.image = [UIImage imageNamed: #"validImageName"]];
[theLabel addSubview: background];
However this does not result in the correct frame. I've also considered setting the background of the label with [UIColor colorWithPatternImage: #"aValidImageName"]; but this does not include padding on the background image. Also, the background image contains rounded corners which I want to retain (which this method cuts). Any ideas are appreciated.
Try this,
1) You can use Autoresizing to change image width according to size of screen.
(OR)
2) Set Custom frame for your imageview
Ex:
UIImageView *background = [[UIImageView alloc]initWithFrame:(0,0,Your_Custom_Width_Size_Value,100)];
background.image = [UIImage imageNamed: #"validImageName"]];
[theLabel addSubview: background];
(or)
//Change width according to the label
UIImageView *background = [[UIImageView alloc]initWithFrame:(0,0,Your_Label_Name.frame.size.width,100)];
background.image = [UIImage imageNamed: #"validImageName"]];
[theLabel addSubview: background];
I would advise you to use a UIView object as a container and add your UIImageView and UILabel as subViews to it. Add constraints to the Image View and the Label so that their height and width are always equal to the superview. Then just give the container UIView whatever frame you want.

How to remove the UIImageView From the custom UIView

i create the the imageView by programatically and add that imageView to the Custom UIView.then i by click the button i want to add another imageView to the same custom UIView,here the old and new imageView also displaying,so i need to remove the first added imageView then i add the new imageView. help me to remove the imageView..
CGRect rect=CGRectMake(10, 10, 100,100);
UIImageView *imageView=[[UIImageView alloc]init];
imageView.frame=rect;
imageView.backgroundColor=[UIColor redColor];
[self.customView addSubview:imageView];//add the imageView to UIView
When you add the first imageView set its tag like
imageView.tag = 232;
Later you can ask your customView for this imageView and remove it.
Call this function.
[[customView viewWithTag:232] removeFromSuperview];
Use this code.
[[self.customView viewWithTag:1000] removeFromSuperView]
CGRect rect=CGRectMake(10, 10, 100,100);
UIImageView *imageView=[[UIImageView alloc]init];
[imageView setTag:1000];
imageView.frame=rect;
imageView.backgroundColor=[UIColor redColor];
[self.customView addSubview:imageView];

UITextView not scrolling to top when using textContainerInset

I am using a UITextView to add an image and some attributedText to:
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.textView.editable = NO;
self.textView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self.textView setAttributedText:attrStr];
I then need to set the textContainerInset since I added the image as a subview (I want it to scroll with the text) so that the text doesn't go over the image.
self.textView.textContainerInset = UIEdgeInsetsMake(insetTop, 8, 8, 8);
But, for some reason, on iPad when loading it is scrolled to the bottom. So, I try to scroll to the top when loading using:
[self.textView scrollRangeToVisible:NSMakeRange(0, 0)];
But, that doesn't work. Removing the textContainterInset DOES allow it to scroll.
How can I still use the textContainerInset and scroll to the top? And should I be using a UIScrollView instead to add my subviews to?

how to enable to move cursor in UITextView

I create UItextView in pragmatically, and when I tap to move cursor in UITextView, it does not worked. But it worked using IB. Do I supposed to set some attributes to UItextView or UIView?
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0,320, 460)];
textView.userInteractionEnabled = YES;
textView.editable = YES;
textView.multipleTouchEnabled =YES;
[self.view addSubview:textView];
textView.delegate = self;
I found default long tap does not working.
I have problem with add subView for gesture recognizer inside custom cell.
When I blocked it or move to background it've started to work:
contentView.insertSubview(view, atIndex: 0)
where view is view with gesture recognizer.

Resources