How can I set up a background image to UITextView?
You can have an UIImageView containing the background image and the UITextView as siblings, then in Interface Builder move the text view to overlap the image view (or add them both to the same parent view if doing it programmatically). You also need to make sure that text view is not opaque and give it a 0% opacity background.
UITextView *textView = [[UITextView alloc]initWithFrame: window.frame];
textView.text = #"text\n text\n text";
UIImageView *imgView = [[UIImageView alloc]initWithFrame: textView.frame];
imgView.image = [UIImage imageNamed: #"myImage.jpg"];
[textView addSubview: imgView];
[textView sendSubviewToBack: imgView];
[window addSubview: textView];
#durai: Your image has a height limit after which white background will appear if empty background appears after it scrolls down then you can repeat the same image.
This might be helpful:
textView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed: #"Image.png"]];
Just one clarification, when trying out answer #9 from #oxigen, I found out that this line:
UIImageView *imgView = [[UIImageView alloc]initWithFrame: textView.frame];
Is relative to the textView.frame. So your x and y values need to be 0,0 if you want it to overlap completely which means you want something like:
UIImageView *imgView = [[UIImageView alloc]initWithFrame:textView.bounds];
I found one simple method how to set background image.
h file
#interface FNTextView : UITextView
#end
m file
...
- (void)drawRect:(CGRect)rect
{
[self.bgImage drawInRect:rect];
[super drawRect:rect];
}
- (void)initHandler
{
self.bgImage = [[UIImage imageNamed:#"textview_bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(4, 4, 4, 4)];
}
...
[txtView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"image.png"]]];
For tiled background i like this solution
UIImage *patternImage = [UIImage imageNamed:#"image.png"];
UIColor* color = [UIColor colorWithPatternImage:patternImage];
textView.backgroundColor = color;
Not sure about, but you can try the following, assuming that your background image is handled by a UIImageView:
[myTextView addSubview: myImageView];
Note that you may need to change the value of the alpha/opaque properties of your UITextView.
Kind Regards.
UITextView *textView=[[UITextView alloc]initWithFrame: CGRectMake(20, 20, 40, 40)];
textView.text = #"this is a test \n this is test \n this is a test";
UIImageView *img = [[UIImageView alloc]initWithFrame: textView.frame];
img.image = [UIImage imageNamed: #"image.png"];
[self.view addSubview:textView];
[self.view insertSubview:img belowSubview:textView];
The answers of #dulcanwilcox and #oxigen both work, but, an addition would be to make the background image resizable, in case the image is being used to show some kind of border.
UITextView *textView = [[UITextView alloc]initWithFrame: window.frame];
textView.text = #"Some text...";
UIImageView *imgView = [[UIImageView alloc]initWithFrame: textView.frame];
imgView.image = [[UIImage imageNamed: #"image"] resizableImageWithCapInsets:UIEdgeInsetsMake(8, 8, 8, 8)];
[textView addSubview:imgView];
[textView sendSubviewToBack:imgView];
[window addSubview:textView];
Check out the documentation for resizableImageWithCapInsets:(UIEdgeInsets)capInsets.
Related
I am adding imageview in viewcontroller.
It is working good in ios6 but in ios7 It display with white background.
I have tried clearColor but it's not working.
Here is my code
[imgcharacter setImage:[UIImage imageNamed:[NSString stringWithFormat:#"character%d.png",reelBtnTag]]];
[imgcharacter setFrame:CGRectMake(100,100, 247.5f, 385.0f)];
imgcharacter.userInteractionEnabled=YES;
[imgcharacter setBackground : [UIColor clearColor]];
[captureview addSubview:imgcharacter];
Try this way....
NSData *imageData = UIImagePNGRepresentation(Your Image);
UIImage *image=[UIImage imageWithData:imageData];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 52, 40, 50)];
imageView.backgroundColor = [UIColor clearColor];
imageView.image = image;
I want to put Custom image on my UINavigationBar,
When i tried this two code snippets,
[self.navigationItem setTitleView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]]];
And
UIButton *logoView = [[UIButton alloc] initWithFrame:CGRectMake(0,0,70,30)];
[logoView setBackgroundImage:[UIImage imageNamed:#"logo.png"] forState:UIControlStateNormal];
[logoView setUserInteractionEnabled:NO];
self.navigationItem.titleView = logoView;
image which i can see on my Device is getting blur or it is getting streched.
I made image with Resolution of 180*40(Width*Height)
I want to know the exact size of Image. what, it would be ?
Thanks in advance.
use this
UIImageView *navigationImage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 98, 34)];
navigationImage.image=[UIImage imageNamed:#"topNav-logo.png"];
UIImageView *workaroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 98, 34)];
[workaroundImageView addSubview:navigationImage];
self.navigationItem.titleView=workaroundImageView;
hope this helps.
Create Image with size (320 x 44) and set as a Title like bellow..
self.navigationItem.titleView = yourImageView;
See whole example like bellow...
- (void)viewDidLoad{
UIImageView* imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
self.navigationItem.titleView = imgView;
[imgView release];
}
You need to first initialize the titleView. and then set it the imageView as a subView:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
self.navigationItem.titleView = [[UIView alloc] initWithFrame:imageView.bounds];
[self.navigationItem.titleView addSubview:imageView];
I use this code, that work, really size of image 160x148 but image button is view very big on all screen!
UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
animatedImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"button1.png"],
[UIImage imageNamed:#"button2.png"],
[UIImage imageNamed:#"button3.png"],
[UIImage imageNamed:#"button4.png"], nil];
animatedImageView.animationDuration = 1.0f;
animatedImageView.animationRepeatCount = 0;
[animatedImageView startAnimating];
[yourButton addSubview: animatedImageView];
How fix that?
The images are big because you called initWithFrame: and passed in your view's bounds. Change it to a different-sized rect, such as:
CGRectMake(0, 0, 160, 148)
... and then reposition it as you see fit.
Try this code.It will help you for correct solution.
UIImage *myimage=UIImage imageNamed:#"button1.png";
Button.imageview.image=myimage;
I'm new to programming, and I know how to add a static image behind a tableView in a .xib file, but not in code. I have tried the following code:
UIImage * image = [UIImage imageNamed:#"pic.png"];
[self.view addSubView: image];
but nothing happens when I write this!
To display an UIImage you have to use an UIImageView and put it under the UITableView using the same frame. Don't forget to set the table view's background to transparent.
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pic.png"]];
iv.frame = CGRectMake(0.0, 0.0, 300.0, 600.0);
UITableView *tv = [[UITableView alloc] initWithFrame:iv.frame style:UITableViewStylePlain];
tv.opaque = NO;
tv.backgroundColor = [UIColor clearColor];
[self.view addSubView: iv];
[self.view addSubView: tv];
[iv release];
[tv release];
You need to use a UIImageView, not a UIImage. Also, when adding a view programmatically, don't forget to set the frame.
i can show image on left hand side of table view but why my right hand side is not working
CGRect frame;
frame= CGRectMake(310 ,10, 50, 50);
UIImageView * myImageView = [[UIImageView alloc]init];
cell.myImageView.image = [UIImage imageNamed:#"bowl.png"];
myImageView.frame = frame;
[myImageView release];
Try this, it works for me:
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yellow_dot.png"]];
cell.accessoryView = image;
Do this code in willDisplayCell and it should work. Also remember the default cell already has an imageview, you can just move its frame.