I have a problem positioning a textLabel and detailTextLabel into a custom UITableViewCell that has a UIImageView appearing behind the textLabel and detailTextLabel.
Refer to the image - it will make more sense
--> what you're looking a UITableView, where each bubble represents a different UITableViewCell.
Essentially, I am trying to fit the textLabel and detailTextLabel into the bubble you see by expanding the bubble dimensions. However, no matter what I try the bubble will not change it's width and height even if I change the UIImageView frame or bounds or contentMode.
Here is the relevant code in cellForRowAtIndexPath:
//Set font and style
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
// Assign our own background image for the cell
UIImage *cellBackgroundImage = [UIImage imageNamed:#"Chat.png"];
UIImageView *cellBackgroundImageView = [[UIImageView alloc] initWithImage:cellBackgroundImage];
//[cellBackgroundImageView setBounds:CGRectMake(0, 0, 20, 20)];
//[cellBackgroundImageView setImage:cellBackgroundImage];
//cellBackgroundImageView.frame = CGRectMake(cellBackgroundImageView.frame.origin.x, cellBackgroundImageView.frame.origin.y, 20, 20);
//cellBackgroundImageView.bounds = CGRectMake(cellBackgroundImageView.frame.origin.x, cellBackgroundImageView.frame.origin.y, 0, 0);
cellBackgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
cellBackgroundImageView.image = cellBackgroundImage;
cell.backgroundView = cellBackgroundImageView;
Any help would be appreciated.
Don't do it here, I did it by writing frame of image and label in LayoutSubviews method in custom cell class.
Just
-(void)layoutSubviews
{ setframe: for both UI components }
Dont use Aspect FIll, use either scaleToFill, or set image to image view using imageCapInsets .
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode
For your Image, it must be like
UIImage *cellBackgroundImage = [[UIImage imageNamed:#"Chat.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 70, 40, 20) resizingMode:UIImageResizingModeStretch]
Related
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.
I have a cell that has some text in it and was trying to add an image to the cell.
This is what Ive tried:
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(30, 30, 30, 30)];
imageView.backgroundColor = [UIColor clearColor];
[imageView.layer setCornerRadius:5.0f];
[imageView.layer setMasksToBounds:YES];
[imageView setImage:[UIImage imageNamed:#"test2.jpeg"]];
[cell.contentView addSubview:imageView];
cell.imageView.clipsToBounds = YES;
This is what it looks like right now:
Is there a way to avoid the image from overlapping the text in the cell? Thanks in advance!
tableviewcells have their own imageView that will appear to the left of the content or atleast to the left of the cells text.
You are creating an extra imageView that just happens to have the same name.
Just change the location
cell.imageView.center = CGPointMake(x,y);
Check out https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW11 which has a nice example of how to use the built-in UITableViewCell image view.
Every UITableViewCell already has an UIImageView property 'imageView', but it's only shown if you set the imageView properties image property (cell.imageView.image = /* image code */;)
The best thing you can do is create a custom UITableViewCell. Or perhaps add another UILabel to the current cell and set your text in that instead of using the default UILabel.
So, when I do this with a regular old view:
UIView *topBlock = [[UIView alloc] initWithFrame:CGRectMake(0,-frameSize.height, frameSize.width, frameSize.height/2)];
[viewController.view addSubview:topBlock];
topBlock.autoresizesSubviews = NO;
topBlock.clipsToBounds = YES;
UIImage *topImage = [UIImage imageNamed:#"BloktLayout"];
UIImageView *topImageView = [[UIImageView alloc] initWithImage:topImage];
topImageView.frame = viewController.view.frame;
[topBlock addSubview:topImageView];
I get the nice old image where I want it, in the top view. But the middle view is a UILabel, and when I try the same thing:
UILabel *midBar = [[UILabel alloc] initWithFrame:CGRectMake(midBarOrigin.x, midBarOrigin.y, midBarWidth, midBarHeight)];
midBar.text = #"Blokt";
midBar.textColor = [UIColor blackColor];
midBar.textAlignment = NSTextAlignmentRight;
midBar.font = [UIFont fontWithName:#"HelveticaNeue-UltraLight" size:80.0f];
[viewController.view addSubview:midBar];
midBar.autoresizesSubviews = NO;
midBar.clipsToBounds = YES;
UIImage *midImage = [UIImage imageNamed:#"BloktLayout"];
UIImageView *midImageView = [[UIImageView alloc] initWithImage:midImage];
midImageView.frame = viewController.view.frame;
[midBar addSubview:midImageView];
I don't see any image at all in the UILabel. Any help?
Seems like the issue is related to your frames.
Tough to say without additional info. Can you post viewController.view.frame, frameSize, and midBarOrigin / midBarWidth / midBarHeight?
In the second codeblock, midBar.clipsToBounds == YES, but it looks like the midImageView.frame is likely very different / outside of midBar.frame in which case it wouldn't be visible.
Edit Some screenshots would help but aren't necessary
Edit 2 Note that subviews' origin points are always relative to the coordinate system of their superview, never relative to the coordinate system of any other view in the view heierarchy. This is likely the heart of the issue here. If you do want to convert CGPoints or CGRects from one coordinate system to another there are methods on UIView such as convertRect: and convertPoint: etc.
Interface Builder doesn't even let you add a control inside of a UILabel.
Instead, if you wish to group multiple controls, you can add them both as subviews of a UIView.
In other words, your image view and label can share the same superview, but the image view cannot be a subview of the label.
If they share the same superview, you can position the image view behind the label, and it should appear "through" the label as long as the label's background is clear.
Simple Way to do.....
You can add UIImageView, UILabel as subview of cell.textLabel
UIImageView *statusImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 4, 8, 8)];<br/>statusImage.backgroundColor = [UIColor redColor];<br/>
statusImage.layer.cornerRadius = 4;<br/>
statusImage.clipsToBounds = YES;<br/>
[cell.textLabel addSubview:statusImage];<br/>
UILabel *Lbl = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, cell.textLabel.frame.size.width - 15, cell.textLabel.frame.size.height)];<br/>
Lbl.text = #"xyz";<br/>
[cell.textLabel addSubview:Lbl];<br/>
I just had this problem as well.
I found that ImageView was behind the label.
When I replaced label with UIView, it works properly.
I need to set a label that has a custom background color on top of a UIImageView. The problem is that I want it's width to be the exact size as the image from the image view is. This is the code so far:
UIImageView *pictureV = [[UIImageView alloc] initWithFrame:CGRectMake(pictureX, 0, pictureSize.width, pictureSize.height)];
pictureV.image = self.picture.image;
pictureV.contentMode = UIViewContentModeScaleAspectFit;
And the label:
UILabel *lblName = [[UILabel alloc]init];
lblName.backgroundColor=[UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.6];
lblName.frame = CGRectMake(pictureV.bounds.origin.x, pictureV.bounds.origin.y + pictureV.bounds.size.height - 30, self.picture.image.size.width, 30);
[lblName setText:self.pictureNote.text];
[lblName setTextAlignment:UITextAlignmentCenter];
[lblName setTextColor:[UIColor whiteColor]];
[pictureV addSubview:lblName]
When setting the contentMode as UIViewContentModeScaleAspectFit, some images are smaller than my UIImageView and I can't seem to make the label's width the same size.
If you use UIViewContentModeScaleAspectFit, the image will not take the full size of UIImage View.
Instead of this set your pictureV's contentMode property to UIViewContentModeScaleToFill ,
then the image will take the whole size of your imageView
I've been trying to do this for a while now. I just want to remove the rounded corners you can see on this picture :
Does anyone know how to do it ?
[EDIT 1 : CELL BUILDING]
I am subclassing UITableViewCell to make my own layout. And then I'm adding a background to these cells like below :
UIImageView* imageView = [ [ UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320.0, 44.0)];
imageView.image = cellBackground;
cell.backgroundView = imageView;
I you need to edit the backgroundview of the corresponding cell. E.g.
UIView *bg = [[UIView alloc] initWithFrame:cell.frame];
bg.backgroundColor = [UIColor whiteColor]; // or any color
cell.backgroundView = bg;
[bg release];
The corners aren't rounded by default, just so you know.
Do you have a background image for the cell that makes the corners
seem rounded (most likely)?
Is each cell actually a section of the
tableview and there is no space between them?
Are you testing on a
jailbroken iPhone and you have some winterboard theme installed that
changes the style of tableviews?