UITableViewCell with stretchable background image - iphone

I have cells in UITableView, I have made custom frame for the label on cell and this frame is resize according to the text.
But my problem is that how I can show those texts inside the bubble (image) as the text size increase the bubble image size also increases.

Try this code for image you use :
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight

Try this:
In Custom Cell.m file :
bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 30, 80, 40)];
[self.contentView addSubview:bgImageView];
messageContentView = [[UITextView alloc] init];
messageContentView.backgroundColor = [UIColor clearColor];
messageContentView.editable = NO;
messageContentView.scrollEnabled = NO;
[messageContentView sizeToFit];
[self.contentView addSubview:messageContentView];
And in tableView:cellForRowAtindexPath method:
static CGFloat padding = 20.0;
CGSize textSize = {260.0 , 10000.0};
CGSize size = [message sizeWithFont:[UIFont boldSystemFontOfSize:13] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
size.width += (padding/2);
UIImage *bgImage = nil;
if([sender isEqualToString:#"you"])
{
bgImage = [[UIImage imageNamed:#"orange.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
[cell.messageContentView setFrame: CGRectMake(padding, padding*2, size.width, size.height)];
[cell.bgImageView setFrame:CGRectMake(cell.messageContentView.frame.origin.x - padding/2,cell.messageContentView.frame.origin.y - padding/2,size.width+padding, size.height+padding)];
}
else
{
bgImage = [[UIImage imageNamed:#"aqua.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
[cell.messageContentView setFrame:CGRectMake(320 - size.width - padding,padding*2,size.width, size.height)];
[cell.bgImageView setFrame:CGRectMake(cell.messageContentView.frame.origin.x - padding/2, cell.messageContentView.frame.origin.y - padding/2,size.width+padding,size.height+padding)];
}
cell.bgImageView.image = bgImage;

you can stretch the image with caps, depending on your image shape it could work
You can use this code
//Your image view
UIImageView *imageView;
//Your image
UIImage *image;
//Left cap is the space you dont wanna stretch on the left side and right side of the image
int leftCap = 20;
//Left cap is the space you dont wanna stretch on the top side and bottom side of the image
int topCap = 20;
//this will only stretch the inner side of the image
imageView.image = [image stretchableImageWithLeftCapWidth:leftCap topCapHeight:topCap];
for more information check this tutorial

Related

Can't seem to figure out this issue with UIImage and UITableViewCell's content view

I need my cell's image view to have an area of 44x44 pixels so that it can be tapped. However, when I tried doing that, my 26x26 image got scaled to 44x44 without my permission, so now my 26x26 image is huge and blurry. I need to keep the 26x26 image at 26x26 but also increase the imageView frame to 44x44. This is what I've done but it hasn't worked:
//UITableViewCell subclass method
- (void) layoutSubviews {
[super layoutSubviews];
self.imageView.frame = CGRectMake(0, 0, 44.0, 44.0);
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
}
//TableView method
-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
UIImage *originalImage = [UIImage imageNamed:#"unchecked.png"];
CGRect screenRect = CGRectMake(0, 0, 26.0, 26.0);
UIGraphicsBeginImageContext(screenRect.size);
[originalImage drawInRect:screenRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
Try setting the content mode to centre:
self.imageView.contentMode = UIViewContentModeCenter;

uilabel not the same height as uiimage with the same height

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.

Trying to overlap two images and showing a overlapped imaged in a third image

I am trying to overlap two local images and trying to show the overlapped one in third image.
I am using this code but simulator shows nothing.
- (void)viewDidLoad
{
[super viewDidLoad];
image1 = [[UIImage alloc]init];
image1 = [UIImage imageNamed:#"iphone.png"];
imageA = [[UIImageView alloc]initWithImage:image1];
[self merge];
}
-(void)merge
{
CGSize size = CGSizeMake(320, 480);
UIGraphicsBeginImageContext(size);
CGPoint thumbPoint = CGPointMake(0,0);
imageview.image = imageA.image;
[imageA.image drawAtPoint:thumbPoint];
imageB = [[UIImage alloc]init];
imageB = [UIImage imageNamed:#"Favorites.png"];
CGPoint starredPoint = CGPointMake(0, 0);
[imageB drawAtPoint:starredPoint];
UIImage *imageC = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageview.image = imageC;
[self.view addSubview:imageview];
}
I can't figure out/don't know where i am making mistake.
Any help would be appreciable.
Remove all the code from every where except the below code in Merge.
-(void)merge
{
CGSize size = CGSizeMake(320, 480);
UIGraphicsBeginImageContext(size);
CGPoint point1 = CGPointMake(0,0);
// The second point has to be some where different than the first point, other wise, the second image will be above the first image, and you wont even know that the two images are there.
CGPoint point2 = CGPointMake(100,100);
UIImage *imageOne = [UIImage imageNamed:#"Image1.png"];
[imageOne drawAtPoint:point1];
UIImage *imageTwo = [UIImage imageNamed:#"Image2.png"];
// If you want the above image to have some blending, then you can do some thing like below.
// [imageTwo drawAtPoint:point2 blendMode:kCGBlendModeMultiply alpha:0.5];
[imageTwo drawAtPoint:point2];
UIImage *imageC = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(100,100,200,200)];
iv.image=imageC;
[self.view addSubview:iv];
}
here's a general purpose "merge" function wrote as a UIImage category... allows image overlay/underlay.
http://saveme-dot-txt.blogspot.com/2011/06/merge-image-function.html

How can I dynamically set the size of the imageView according to the size of the image

I user Interface Builder to layout my view and image view. I put a imageView on the view and set it as an outlet. The problem is that no matter what size of image (768*1024、1024*700) I set in my program:
self.imageView.image = [UIImage imageNamed:self.filename];
The size to be on the screen is always the size of the imageView I set in the Interface Builder. How can I dynamically set the size of the imageView according to the size of the image? Thanks!
Something like:
UIImageView *imageView = [[UIImageView alloc] init];
UIImage *image = [UIImage imageNamed:#"image.png"];
CGSize imageSize = [image size];
imageView.frame = CGRectMake(0, 0, imageSize.width, imageSize.height);
UIImage *image = [UIImage imageNamed:self.filename];
CGFloat width = image.size.width;
CGFloat height = image.size.height;
self.imageView.frame = CGRectMake(x, y , width, height);
You could subclass your image and do the following override for eachTime a new image is set :
#interface MyImageSubClass : UIImageView;
#end
#implementation MyImageSubClass
- (void)setImage:(UIImage *)image {
// Let the original UIImageView parent class do its job
[super image];
// Now set the frame according to the image size
// and keeping the original position of your image frame
if (image) {
CGRect r = self.frame;
CGSize imageSize = [image size];
self.frame = (r.origin.x, r.origin.y, imageSize.width, imageSize.height);
}
}
#end
UIImage *buttonImage2=[UIImage imageNamed:#"blank_button_blue.png"];
oButton=[[UIButton alloc] init];
// [oButton setImage:buttonImage2 forState:UIControlStateNormal];
[oButton setFrame:CGRectMake(0, 0, buttonImage2.size.width, buttonImage2.size.height)];
UIImage* sourceImage = [UIImage imageNamed:self.filename];;
CGRect rect;
rect.size.width = CGImageGetWidth(sourceImage.CGImage);
rect.size.height = CGImageGetHeight(sourceImage.CGImage);
[ yourimageview setframe:rect];

Can I add UIImageView inside custom UIAlertView?

I already have a custom UIAlertView and in which i want to add UIImageView for adding custom UIAlertView.
I have already added UIImageView with array and start animating it but it doesn't work. If you want, I can post the code.
Please help me ASAP. I'm stuck :(
Here is code
- (id)initWithImage:(UIImage *)image text:(NSString *)text
{
//CGSize imageSize = self.backgroundImage.size;
CGRect frame = [[UIScreen mainScreen] bounds];
width = frame.size.width;
height = frame.size.height;
if (self = [super init])
{
loadtext = [[UILabel alloc]initWithFrame:CGRectZero];
loadtext.textColor = [UIColor blackColor];
loadtext.backgroundColor = [UIColor whiteColor];
[self addSubview:loadtext];
//Create the first status image and the indicator view
UIImage *statusImage = [UIImage imageNamed:#"status1.png"];
activityImageView = [[UIImageView alloc]
initWithImage:statusImage];
//Add more images which will be used for the animation
activityImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"status1.png"],
[UIImage imageNamed:#"status2.png"],
[UIImage imageNamed:#"status3.png"],
[UIImage imageNamed:#"status4.png"],
[UIImage imageNamed:#"status5.png"],
[UIImage imageNamed:#"status6.png"],
[UIImage imageNamed:#"status7.png"],
nil];
//Set the duration of the animation (play with it
//until it looks nice for you)
activityImageView.animationDuration = 1.0;
//Position the activity image view somewhere in
//the middle of your current view
activityImageView.frame = CGRectZero;
//Start the animation
[activityImageView startAnimating];
//Add your custom activity indicator to your current view
[self addSubview:activityImageView];
//self.backgroundImage = image;
}
return self;
}
- (void) layoutSubviews{
//lblUserName.transform = CGAffineTransformMake;
//[lblUserName sizeToFit];
CGRect textRect = activityImageView.frame;
textRect.origin.x = (CGRectGetWidth(self.bounds) - CGRectGetWidth(textRect))/2;
textRect.origin.y = (CGRectGetHeight(self.bounds) - CGRectGetHeight(textRect))/2;
textRect.origin.x -= 100.0;
textRect.origin.y -= 60.0;
textRect.size.height = 30;
textRect.size.width = self.bounds.size.width - 50;
activityImageView.frame = textRect;
}
This code started working. But now this UIImageView is taking my whole screen and coming in front of UIAlertView. Though i give 10px height and width. It shows same. Can anyone help please?
Thanks,
Anks
Yes, you can add image view - in custom alert-view. instead of taking as UIView custom class, need to take UIImage-View & you can use image-view properties.
Change activityImageView.frame = CGRectZero;
to some custom frame.
eg:
activityImageView.frame = CGRectMake(0,0,100,100);