UIImage on UILabel - ios

I have a UILabel on a UITableViewCell which I am reusing at multiple places. It will hold text/statements of type NSString.
Question: For just one cell, I want an image before the text starts. Is it possible to place/display UIImage before text in iOS on UILabel. I can definitely place an image directly on the cell before the label, but I wish to avoid playing around with the constraints if it affects the other reusing cells.

Yes. UILabel is a subclass of UIView. Therefore, you can add anything derived from UIView to UILabel.
Below code is an example I show you.
UILabel *label = [UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bear.png"]];
imgView.frame = CGRectMake(0, 0, 10, 10);
[label addSubview:imgView];
I think that making a custom view which has any features you want is better than using UILabel.

You can do this with NSTextAttachment class available from iOS 7.Here is the easy answer for this

Not in the UILabel but you can definitively do it in the Cell, you need to create a custom class that inherits from UITableViewCell.
In there define your cell at your better convenience

Related

set value for a label without connection using code

how to set value for uilabel using objective-c without any connection from storyboard to source-file.
In a storyboard, suppose a label is dropped into uiview and we don't want to bound connection to .h file , so how is it possible to set value using objective-c.
You can create UILabel Programatically like,
UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(91, 15, 0, 0)];
myLabel.text = #"hello";
[self.view addSybview:myLabel];
Second thing you can set tag from storyboard and by this tag you can access label.
Third thing you can get NSArray of subviews of self.view means your main view and from that array you can access that label.
these are the possible ways. :)
hope this will help :)
You can assign a tag value to your UILabel from the storyboard, then you can use that value to access the view from your code:
UILabel *label = (UILabel *)[self viewWithTag:1];
[label setText:#"setting some value"];

UITableView cell separator line thicker?

I have attached an image of cells in a table view, and you can notice the 2 separator lines in the middle are a tiny bit thicker/darker than the 2 outer lines. I want the lines to all be consistent, and the color/thickness of the outer two lines. I could not find a solution, so I am wondering if anyone knows of any. I figure it would be a simple solution, but I haven't been able to figure it out. Thanks guys.
Select your UITableView from xib or storyboard, then make the UITableView separator to None.
See the image
Then place a UILabel with the width same as of UITableViewCell and height=1 in the bottom of your cell, clear the text of the label and set the backGround color as per your wish, this will solve your problem.
Hope this helps you.
Create a custom separator Line Programatically:
UIView *separatorLine = [[UIView alloc] initWithFrame:
CGRectMake(0, cell.contentView.frame.size.height - 1.0,
cell.contentView.frame.size.width, 1)];
separatorLine.backgroundColor = [UIColor blackColor];;
[cell.contentView addSubview: separatorLine];
or you can add a image view for separator in custom UITableviewCell in UI

Drawing line in UITableViewCell (without using drawRect)

I have a UITableViewCell in which I would like to draw some lines (say a 2pt line at 1/3rd from top of the TableCell covering the whole width). The lines would always be in the same place within the tableview cell.
One straightforward solution is to just have a -drawRect method that will use CGContextStrokePath to draw the line. But that seems to be like an overkill since it would call drawRect everytime which is inefficient.
I would think that there would be a way to be able to cache it somehow, so that all tableview cells are created with the lines by default. One way I can think of is to create 2pt*2pt png file and add it to a UIImageView on top of the tableview cell. Any other ways?
Try this -
Add this code in your cellForRowAtIndexPath, just adjust y position og this image view-
UIImageView *seperatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 49, 320, 1)];
seperatedImageView.backgroundColor = [UIColor colorWithRed:201.0/255.0 green:250.0/255.0 blue:152.0/255.0 alpha:1.0];
[cellBackView addSubview:seperatedImageView];
Another option is, to add a UIView and set its background color. For example:
UIView *lineView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,cell.contentView.bounds.size.width, 2)] autorelease];
lineView.backgroundColor = [UIColor grayColor];
[cell.contentView addSubview:lineView];
You can just add a UIView like a subView of your UITableViewCell.

How to put UIlabel on UIImageView programatically

I'm trying to put a UIlabel on a UIImageView which is in a UIScrollView. How can I accomplish this?
imageView is a UIView, so you can add subviews to it.
also make sure you set the label with an appropriate frame
You can do:
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,20)]; //or whatever size you need
[imageView addSubview:myLabel];
You can add it directly by dragging Label from to your storyboard/xib if you want it to e static. But if you want it to dynamically generate at runtime then you should add it by using
UILabel *yourlabel = [[UILabel alloc]initWithFrame:CGRectMake(YOUR COORDINATES)];
then you can add it to any of your view.For your imageview you can add it as
[imageview addSubview:yourlabel];
Often you may require to remove it from that view if not needed. You can similarly call removeFromSuperView also.
Hope this works :)

iOS: Adding a third label to UITableViewCell without creating a Custom cell?

I've worked with custom cells before but I just wondered for adding a third label (nothing else!) is there a simpler solution? All I want to do is show the title, content and date. I've set the first two to the textLabels and detailTextLabels but I of course need a third for date.
So, is there a simpler solution without making a custom cell?
Thanks
See A Closer Look at Table-View Cells, section called Programmatically Adding Subviews to a Cell’s Content View.
mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 220.0, 15.0)] autorelease];
mainLabel.tag = MAINLABEL_TAG;
mainLabel.font = [UIFont systemFontOfSize:14.0];
mainLabel.textAlignment = UITextAlignmentRight;
mainLabel.textColor = [UIColor blackColor];
[cell.contentView addSubview:mainLabel];
Try setting into cellForRowAtIndexPath (each time you create a new cell) modifying the textLabel and detailTextLabel Frame and adding to cell.contentView a new Label for the date...
But i think the correct way is to subclass the UItableViewCell class and create a custom cell

Resources