UITextField "content size" property - ios

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;

Related

User can't able to enter amount before currency symbol in textfield in objective c

Below is an image of the screen, I just want to when the user enters the amount on this textfield then the user can't able to move cursor before $ sign and can't able to write amount before $ sign.
Please have a look on my problem, and give me idea about it,
You can simply add a textfield inside a view and before textfield add a lable with text "$". Remove the textfield border and add a border layer for view. It will work perfectly as per your requirement.
Try to set currency image as leftview of textfield
For Imageview
UIImageView *imageArrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"currency.png"] ];
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[paddingView addSubview:imageArrow];
imageArrow.center = paddingView.center;
textfield.leftView = paddingView;
textfield.leftViewMode = UITextFieldViewModeAlways;
For Label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
label.text = #"$";
textField.leftViewMode = UITextFieldViewModeAlways;
textField.rightView = label;
You can use left view property of UITextField
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
label.text = #"$";
label.backgroundColor = [UIColor white];
textField.leftViewMode = UITextFieldViewModeAlways;
textField.rightView = label;

NSAttributedString left spacing

I have an UITextView within NSAttributedString. And I need add some space before a letter. I need something like NSKernAttributeName, but for left spacing because NSKernAttributeName adds space on right side.
textfield.leftView= [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 0)];
textfield.leftViewMode= UITextFieldViewModeAlways;
textfield.rightView= [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 0)];
textfield.UITextFieldViewMode= UITextFieldViewModeAlways;
try this man

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];

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

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.

How to set a label at the top center

I want to set a label in the top center of the view, I've already tried this
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.myView.center.x, 25, 92, 46)];
But its not actaully in the center as I want, here is how it looks like:
And I want the middle of the label, in the x center, and not the beginning of the label in the center.
The easiest way is to make label's frame width equal to view's frame width and set the alignment of label to Center.
Hope this Helps !!!
Try like this ,
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
lbl.textAlignment=UITextAlignmentCenter;
It does exactly what you programmed. Unfortunately there is always some basic math involved in UI programming.
One possible solution:
Initialize it with some frame of any position but with the right size.
Then set the lable views' center property accordingly.
[label setCenter:CGPointMake(self.myView.center.x, 25)];
Or simply set the frame in a way, that it takes all the space from myView's left to right and just set the text alignment to NSTextAlignmentCenter.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 25, self.myView.frame.size.width, 46)];
label.textAlignment = NSTextAlignmentCenter;
Or calculate the top left corner accordingly and set the frame upon initialization. Use the center position and substract half of the width. Or use the full with of myView, substract the with and then devide by 2.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.myView.center.x-92/2, 25, 92, 46)];
or
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((self.myView.frame.width-92)/2, 25, 92, 46)];
Try to set the proper x-position as view.center.x - (labelwidth/2). Also set the textAlignment to center.
Using CGPointMake set center of label like this.
lblAppTitle.center= CGPointMake(self.view.bounds.size.width / 2, 25);
[lblAppTitle setTextAlignment:NSTextAlignmentCenter];
Try this
int x=(self.myView.frame.size.width-92)/2;
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(x, 25, 92, 46)];
lbl.textAlignment=UITextAlignmentCenter;
Try this:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 92, 46)];
label.center = CGPointMake(label.bounds.size.width / 2, 25);
or this:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.myView.center.x - 46, 25, 92, 46)];
Set it's center rather than setting rect
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 25, 92, 46)];
label.center = self.myView.center;
//Also if your frame is bigger than text then you can set this also
label.textAlignment = UITextAlignmentCenter;

Resources