set UIView's Content to CAlyer's Contents in iphone - ios

I am trying to add view's content to LAyer's content, IS it possible?
v1=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 30, 30)];
v1.backgroundColor=[UIColor redColor];
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(05, 05, 20, 20)];
lbl.text=#"ABC";
[v1 addSubview:lbl];
CALayer *iconImageLayer = [[CALayer alloc] init];
iconImageLayer.contents=?????? //Try to add view
Help me solve this
Thank you,

Use the layer property of UIView.
CALayer *iconImageLayer = v1.layer
or just simply use v1.layer wherever you would have used iconImageLayer.

Related

UIImageView size changes

I have navigation bar with custom view on it. UIImageView and UILabel are subviews of titleView. And I add UITapGestureRecognizer to titleView to make it show another UIViewController when the user taps on it. When you tap on titleView other UIViewController opens. But when you click on back button in my titleView size of UIImageView changes. Here is the code
UIView *titleView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, 30)];
UILabel *title = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width/2, 30)];
title.text = _groupName;
[title setTextColor:[self colorFromHexString:#"#474747"]];
[title setFont:[UIFont fontWithName:#"Roboto-Bold" size:16]];
[title setTextAlignment:NSTextAlignmentCenter];
[title setBackgroundColor:[UIColor clearColor]];
_imageView.frame = CGRectMake(0, 0, 30, 30);
_imageView.layer.cornerRadius = 15.0;
_imageView.layer.masksToBounds = YES;
_imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
_imageView.layer.borderWidth = 0.1;
_imageView.contentMode = UIViewContentModeScaleAspectFit;
title.frame = CGRectMake(title.frame.origin.x+20, title.frame.origin.y, self.view.frame.size.width/2, 30);
UITapGestureRecognizer *recognizer;
recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(titleTapped:)];
titleView.userInteractionEnabled = YES;
[titleView addGestureRecognizer:recognizer];
[titleView addSubview:title];
[titleView setBackgroundColor:[UIColor clearColor]];
[titleView addSubview:_imageView];
self.navigationItem.titleView = titleView;
Here are the images where you can see the changes
I check your code Add clips to bounds and do not need to title frame two times.
_imageView.frame = CGRectMake(0, 0, 30, 30);
_imageView.layer.cornerRadius = 15.0;
_imageView.layer.masksToBounds = YES;
_imageView.clipsToBounds = true;
Rather than setting the frame of the views directly, you might try using Auto Layout; I suspect what's happening is that your custom view is getting re-layed-out after the back button, and it's reverting to the image view's instrinsicSize.
What you could do instead is turn off translatesAutoresizingMaskIntoConstraints on the subviews, and then add constraints to position them, and finally add width and height constraints to the image view.
Using Auto Layout is also recommended if you ever intend to internationalize your app -- if you set up your constraints correctly, then the translated title will fit and you won't have to ever play around with hardcoded values.

How to set UIView inside UITextView

The below picture shows that there is a view Inside TextView. Now I want that Where UIView ends from there textview starts editing, not only from Left-top also from left-bottom.
How to do it ?
Is it possible ?
If Yes, then how ?
If no, then any other suggestion ?
In case of text filed below code is applied for left padding
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 20)];
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;
You can wrap textfiled using below code
UIBezierPath * imgRect = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 30, 30)];
self.textView.textContainer.exclusionPaths = #[imgRect];
And, now you can add any control in place of "imgRect"
UIView *spacerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
spacerView1.backgroundColor = [UIColor redColor];
[textView addSubview:spacerView1];

UITextField "content size" property

Is there a way to make the right side of a UITextField's content "end sooner" like in this graphic:
I'm using the following to give the content text some left padding, but can't figure out an easy solution to reduce the available content size on the right size: self.textfield.layer.sublayerTransform = CATransform3DMakeTranslation(10, 0, 0);
I'd rather not subclass UITextField but I'm fine doing so if need be.
try these...
i hope it helps..
UITextField *txt=[[UITextField alloc]initWithFrame:CGRectMake(10, 100, 150, 30)];
[txt setBackgroundColor:[UIColor lightGrayColor]];
[txt setPlaceholder:#"Hello my friend"];
[self.view addSubview:txt];
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(10, 100, 50, 30)];
txt.rightView = paddingView;
txt.rightViewMode = UITextFieldViewModeAlways;

Add Subview above mainviews layer

I am having trouble adding a subview above the CALayer of its parent view.
I tried to attach an image to show what i mean but i dont have enough reputation so here is a link to the image:
http://imageshack.us/photo/my-images/843/img0219u.png/
Part of the subview is obscured by the parent views border.
How can i make the subview appear over the top of the parent views layer?
Any help is appreciated.
Here is my code if it helps:
Code for adding layer:
UIView *student = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
student.backgroundColor = [UIColor grayColor];
CALayer *studentLayer = [[CALayer alloc] init];
studentLayer.borderColor = [UIColor grayColor].CGColor;
studentLayer.borderWidth = 5;
student.layer.borderColor = studentLayer.borderColor;
student.layer.borderWidth = studentLayer.borderWidth;
Code for adding subview:
UILabel *ilp = [[UILabel alloc] initWithFrame:(CGRectMake(-20, -10, 40, 20))];
ilp.text = #"ILP";
ilp.backgroundColor = [UIColor yellowColor];
ilp.textColor = [UIColor blackColor];
ilp.textAlignment = NSTextAlignmentCenter;
[student addSubview:ilp];
Thats not possible so far, The best solution i can think of is that add both the subview and its parent view inside another view than its just a matter of reordering two subviews, so that your subview is above the other and its border.
Hope that help!

How do I add Two UILabels in my UIScrollView for the iphone?

myscrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];
myscrollview.delegate = self;
[self.view addSubview:myscrollview];
CGSize scrollViewContentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height+500);
[myscrollview setContentSize:scrollViewContentSize];
lblluckyno = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 200, 50)];
lblfirstmsg = [[UILabel alloc] initWithFrame:CGRectMake(15, 50, 295, 800)];
But I don't know My Firstmasg length.... after that lblfirstmsg I want to add second lblsecongmgs.... So how can I add?
After you set first label and set it content you can call [lblfirstmsg sizeToFit]; to fit frame to its content. Then you can add your second UIlabel next to the first one [[UILabel alloc] initWithFrame:CGRectMake(lblfirstmsg.frame.origin.x + lblfirstmsg.frame.size.width, lblfirstmsg.frame.origin.y, 200, 50)];
[myscrollview addSUbview:lblluckyno];
and same way add 2nd label.
This is your scroll view code.
myscrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];
myscrollview.delegate = self;
[self.view addSubview:myscrollview];
[myscrollview setContentSize:CGSizeMake(self.view.frame.size.width,self.view.frame.size.height+500);];
After you set two label With CGRect and add in Scroll View
[myScrollView addSubView:lblluckyno];
......
Hello Mayur P Bhansali,
You can find the position and dimension of your first label like so:
lblluckyno = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 200, 50)];
UILabel *secondLabel = [[UILabel alloc] initWithFrame: CGRectMake(lblluckyno.frame.origin.x + lblluckyno.frame.size.width, 200, 100)];
Each UIView has a "frame" object and this frame object has a "origin" C structure and "size" C structure. Using labelName.frame.size.width gives you the width of a frame and labelName.frame.origin.x gives you the X coordinate of the frame.
You could access these properties like this too:
[labelName frame].size.width
[labelName frame].size.origin.x
lblluckyno = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 200, 50)];
lblfirstmsg = [[UILabel alloc] initWithFrame:CGRectMake(15, 50, 295, 800)];
[myscrollview addSUbview:lblluckyno];
[myscrollview addSUbview:lblfirstmsg];

Resources