Sub Views are not visible after change the UILabel Background colour - ios

I have changed my UILabel background colour like this. Actually I have added an image to it. I did it like this.
self.lblMsg=[[UILabel alloc] initWithFrame:CGRectMake(8.0, self.lblDateTime.frame.origin.y+self.lblDateTime.frame.size.height, size.width-5.0, 100.0)];
UIImage *img = [UIImage imageNamed:#"msgBackground"];
CGSize imgSize = self.lblMsg.frame.size;
UIGraphicsBeginImageContext( imgSize );
[img drawInRect:CGRectMake(0,0,imgSize.width,imgSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.lblMsg.backgroundColor = [UIColor colorWithPatternImage:newImage];
[self.contentView addSubview:self.lblMsg];
Now I want to add another UILabelon to this and that label should include an image as an attachment. So I did it like this.
self.lblWeatherTitle=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, self.lblMsg.frame.size.width, 50.0)];
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:#"splashLogo"];
NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
NSMutableAttributedString *myString= [[NSMutableAttributedString alloc] initWithString:#"My label text"];
[self.lblWeatherTitle setTextColor:[UIColor redColor]];
[myString appendAttributedString:attachmentString];
self.lblWeatherTitle.attributedText = myString;
[self.lblMsg addSubview:self.lblWeatherTitle];
But My 2nd UILabel is not visible.
This is what I need to do
This black text is a long description. As the title of it it should show those red label and the yellow icon. so I added that top label as a subview to this white label. This whole thing is inside a custom UITableViewCell
Please help me. How can I do this in correct way?
Thanks

Related

How can I draw a banner at the top of a UIImageView in Obj C

Some Context
I am creating a feature that enables the users to share some Images from my App to their Facebook/Twitter account.
The feature works, I implemented the IBAction and I can share some Images within Facebook and Twitter.
The Issue
My company's marketing department is now asking me to add at the top of the shared images, a banner with the company's logo and the date.
This is what I have to display :
For now I only share the white rectangle. Now I have to add this banner, the blue rectangle.
My Thoughts
I was wondering how I will be developing it. First of all I was thinking about drawing it programmatically within a view.
UIView *banner = [[UIView alloc] initWithFrame:CGRectMake(0, 35, self.view.frame.size.width, 60)];
banner.backgroundColor = [UIColor blueColor];
[self.view addSubview:banner];
The problem is I really don't know how to make it resizable in order to fit all the different screen sizes. But also, how to add the logo within this rectangle, the label date and finally, how can I fit it in my shared UIImage
Then I was wondering, "maybe I can draw it like in Android, directly in the XML file". But from all my investigations, it looks easier to do it programmatically.
Help needed
I would appreciate some advices in order to focus myself on a better way to develop it, because right now my ideas are kind of fuzzy.
Thank you for reading.
You can do it in this way
Consider the img1 is the image which you get from URL.
Here I have taken a bannerView, You can add the label and image into it.
Code :
UIImage *img1 = [UIImage imageNamed:#"Screen.png"];
//Only banner view, on which we will design the label and logo
UIView *vwBanner = [[UIView alloc] initWithFrame:CGRectMake(0, 0, img1.size.width, 60)];
vwBanner.backgroundColor = [UIColor grayColor];
//Logo design
UIImageView *imgVwLogo = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(vwBanner.frame) - 100 , 0, 100, 100)];
imgVwLogo.backgroundColor = [UIColor redColor];
[vwBanner addSubview:imgVwLogo];
//Label design
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, CGRectGetWidth(vwBanner.frame) - imgVwLogo.frame.size.width, CGRectGetHeight(vwBanner.frame))];
lbl.text = #"Thurseday, 20 October 2016";
[vwBanner addSubview:lbl];
//Full view which contains the downloaded image and banner image
UIView *vwWithBanner = [[UIView alloc] initWithFrame:CGRectMake(0, 0, img1.size.width, img1.size.height+vwBanner.frame.size.height)];
[vwWithBanner addSubview:vwBanner];
//imageView with downloaded image
UIImageView *imgVw = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(vwBanner.frame), img1.size.width, img1.size.height)];
imgVw.image = img1;
[vwWithBanner addSubview:imgVw];
//Draw the image of full view
UIGraphicsBeginImageContextWithOptions(vwWithBanner.bounds.size, NO, img1.scale);
[vwWithBanner drawViewHierarchyInRect:vwWithBanner.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Output :
See the gray bar is the Banner View and the below is the image. I did not have image so taken a screen shot of simulator to demonstrate the example.
UIView *banner = [[UIView alloc] initWithFrame:CGRectMake(0, 35, self.view.frame.size.width, 60)];
banner.backgroundColor = [UIColor blueColor];
UIImageView *imgBanner=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, banner.frame.size.width, banner.frame.size.height)];
[imgBanner setImage:#"img.png"];
imgBanner.contentMode = UIViewContentModeScaleAspectFit;
[banner addSubView:imgBanner];
[self.view addSubview:banner];
Call this method once you get the image,
UIImage *img = [self drawText:#"Some text"
inImage:img
atPoint:CGPointMake(0, 0)];
Function implementation like this,
+(UIImage*) drawText:(NSString*) text
inImage:(UIImage*) image
atPoint:(CGPoint) point
{
UIFont *font = [UIFont boldSystemFontOfSize:12];
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height);
[[UIColor whiteColor] set];
[text drawInRect:CGRectIntegral(rect) withFont:font];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

Clickable events in NSAttributedString in ios

i had inserted UIimage inside uitextview as NSTextAttachment using nsattributedstring.
Now because of some default property for attrbutestring i am getting long press event on my image.
i.e When i do long press in UIImage it got UIActionsheet giving me option to "SAVE IMAGE" or "COPY".
I had not written any code for this..
What i actully want on the long press of UIIMAGE is i want to open that small UIIMAGE into my full screen like we have in whatapp like chatting.
When user send some image and user click on that image it get displayed in full screen.
Any one please help me to customise this click event of nstextattachment.
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:#"Somestring"];
CGRect rect = CGRectMake(0,0,100,100);
UIGraphicsBeginImageContext( rect.size );
[image drawInRect:rect];
UIImage *picture1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(picture1);
UIImage *img=[UIImage imageWithData:imageData];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image =img;
NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
[attributedString replaceCharactersInRange:NSMakeRange(0,0) withAttributedString:attrStringWithImage];
UITextView *messageContentView=[[UITextView alloc] init];
messageContentView.attributedText=attributedString;

merging uilabel and uiimageview

I picked an image from the photo album and put it in an UIImageView called imageView
I then added some text over the image using UITextView as source of input and a UILabel on top of the uiimageview to display the text.
So far so good.
Now I'm trying to merge the label and the imageview in order to do other stuff with that but I 'm not getting it to work. I have tried some solutions given to similar questions but they didn't work for me.
I tried this
UIGraphicsBeginImageContextWithOptions(_imageView.bounds.size, NO, 0.0); //retina res
[self.imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
[_displaylabel.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
it tells me that Received type "CAlayer for instance message is a forward declaration".
any other suggestions?
Try this:
UIGraphicsBeginImageContextWithOptions(_imageView.bounds.size, NO, 0.0); //retina res
[self.imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Make sure that the UILabel is a subview of your UIImageView, so that rendering the layer will include all sublayers (the UILabel included). It seems like you forgot to include UIGraphicsEndImageContext(); too, which may have been the issue
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, imgView.frame.size.width, imgView.frame.size.height)];
[tempView addSubview:imgView];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(tempView.frame.size.width/2, 0, 200, tempView.frame.size.height/2)];
[label setText:#"Hello... You there"];
[label setTextColor:[UIColor blackColor]];
[label setTextAlignment:NSTextAlignmentLeft];
[label setBackgroundColor:[UIColor clearColor]];
[tempView addSubview:label];
UIGraphicsBeginImageContext(tempView.bounds.size);
[tempView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Repeating background image for UITextView?

Currently I am doing this to have a background image for my UITextView:
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, textView.frame.size.width, textView.frame.size.height)];
imgView.image = [UIImage imageNamed:#"background.png"];
[textView addSubview: imgView];
[textView sendSubviewToBack: imgView];
[imgView release];
But the problem is, it does not repeat. What I want, is the image to repeat after each UIImage.
Is this possible?
Thanks!
textView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed#"background"]];
Yes, omit the .png from the image name, it will allow you to provide a Retina version background#2x.png.

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