UILabel for UIImageView that has contentMode UIViewContentModeScaleAspectFit - ios

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

Related

How could I get a normal title image like twitter app?

UIImageView *googleLogo = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
googleLogo.image = [UIImage imageNamed:#"googlelogo.png"];
self.navigationItem.titleView = googleLogo;
UIImageView *titleImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 78, 20)];
[titleImage setImage:[UIImage imageNamed:NSLocalizedString(#"google.png", nil)]];
self.navigationItem.titleView = titleImage;
set the size of the image based on your requirements
If your image size is 78 X 20 you have to set the frame that during initialisation.
May be your image size and frame size you defined is different, both the image size and frame should be same to get the proper result
Change the image view mode that fit the image as button size will not stretch that image
googleLogo.contentMode = UIViewContentModeScaleAspectFit;

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.

Ios 7 speed dial cell style

I want to create UItableview cells that have circle image and text in circle image. Should I draw every circle and text in it? Is there any cell style for this? I saw Apple did this in Phone->Speed Dial
You can use UIButton/UIImageView according to your requirement. To make that component as circle you should set corner radius.
object.layer.cornerRadius = 40;
object.clipsToBounds = YES;
I used circle image and added UILabel in the centre of image.
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 25, 14)];
myLabel.text = #"yourExtension"
myLabel.font=[UIFont fontWithName:#"Helvetica" size:10.0f];
myLabel.textAlignment=NSTextAlignmentCenter;
UIImage* im= [UIImage imageNamed:#"unknown.png"];
UIImageView* imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 35, 35)];
imageView.image=im;
[myLabel setCenter:imageView.center];
[imageView addSubview:myLabel];

Overlay textLabel and UIImageView in UITableViewCell

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]

setting an image in background of a label programmatically

I have a label (many labels in fact) now I want an image in the background of my label (same image) so that text writtenin my label is shown to user. How to do it programmatically?
The simplest way would just be to simply layer a UIImageView behind the UILabel. If you wanted to make it more robust you could make a UILabel subclass that exposes a method or property to set the image background and it would add the image to itself.
CGPoint labelPos = CGPointMake(123, 234);
UIImage *theImage = [UIImage imageNamed:#"button_bg.png"];
UIImageView *theImageView = [[UIImageView alloc] initWithImage:theImage];
theImageView.frame = CGRectMake(labelPos.x, labelPos.y, theImage.size.width, theImage.size.height);
UILabel *theLabel = [[UILabel alloc] initWithFrame:CGRectMake(labelPos.x, labelPos.y, 100, 50)];
theLabel.backgroundColor = [UIColor clearColor];
// ...label config...
[self.view addSubview:theImageView];
[self.view addSubview:theLabel];
[theImageView release];
[theLabel release];

Resources