UITextField *theLastNameTF = [[UITextField alloc]init];
theLastNameTF.borderStyle = UITextBorderStyleNone;
theLastNameTF.font = [UIFont fontWithName:#"Helvetica-neue" size:24.0];
theLastNameTF.textColor = [UIColor grayColor];
theLastNameTF.textAlignment = NSTextAlignmentLeft;
[theScrollView addSubview:theLastNameTF];
I'm creating a textfield using the above code.I need to show a line under the textfield,so i'm adding a gray coloured UILabel to text field.
-(void)addLineToTextField:(UITextField *)theTextfield
{
UILabel *theSeparatorLine = [[UILabel alloc]initWithFrame:CGRectMake(0, theTextfield.frame.size.height-0.5, theTextfield.frame.size.width, 0.5)];
theSeparatorLine.frame = CGRectIntegral(theSeparatorLine.frame);
theSeparatorLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
theSeparatorLine.backgroundColor = kCS_LightGrayColor;
[theTextfield addSubview:theSeparatorLine];
theSeparatorLine = nil;
}
I'm facing a issue in textfield,while entering texts,the text are half visible and cursor is not moving towards left of text.
Please refer the image,I have entered "arun... arun 123",as expected the cursor should be at left of 123,but the cursor is not moving further.
How can i fix this this?
EDIT
This code is to reframe the UI
(call layoutViewForCurrentorientation () inside orientation change method)
-(void)layoutViewForCurrentorientation
{
if ([[UIApplication sharedApplication]statusBarOrientation] == UIInterfaceOrientationPortrait) {
[self potraitUI];
}
else
{
[self landscapeUI];
}
}
If i remove the line textField.font = [UIFont fontWithName:#"Helvetica-neue" size:24.0]; .I issue is resolved but i need bigger font in textfield.
I was using font [UIFont fontWithName:#"Helvetica-neue" size:24.0] and the textfield size was 25.I made my UITextfield height to 30 and the issue got resolved.Don't know the actual reason but it worked.
Please set the textfield frame
UITextField *theLastNameTF = [[UITextField alloc]initWithFrame:CGRectMake(50,50,250,30)];
I hope it helps you.
I'm seeing the same problem in 9.3. Appears to be an iOS bug.
When the UITextField is first responder, the UITextField contains a scrollview (UIFieldEditor) which contains the input view (a _UIFieldEditorContentView, -[UITextField inputView].) The bug is that the input view is the incorrect size. The input view is not wide enough and thus the scrollview's -contentSize is also incorrect. The UITextField is scrolling as far right as it can, but that's not far enough because the input view is too small.
My workaround is to reset the text field's text. You'll also probably need a flag to ignore delegate callbacks:
_ignoreTextDelegate = TRUE;
NSString *text = textField.text;
textField.text = nil;
textField.text = text;
_ignoreTextDelegate = FALSE;
Try this
UITextField *theLastNameTF = [[UITextField alloc]initWithFrame:CGRectMake(20,50,250,30)];
theLastNameTF.borderStyle = UITextBorderStyleNone;
theLastNameTF.font = [UIFont fontWithName:kHelveticaneue size:15.0];
theLastNameTF.textColor = [UIColor grayColor];
theLastNameTF.textAlignment = NSTextAlignmentLeft;
[theScrollView addSubview:theLastNameTF];
And set the line frame's y value should be with respect to textField frame's y value
-(void)addLineToTextField:(UITextField *)theTextfield
{
UILabel *theSeparatorLine = [[UILabel alloc]initWithFrame:CGRectMake(0, theTextfield.frame.origin.y + 0.5, theTextfield.frame.size.width, 0.5)];
theSeparatorLine.frame = CGRectIntegral(theSeparatorLine.frame);
theSeparatorLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
theSeparatorLine.backgroundColor = kCS_LightGrayColor;
[theTextfield addSubview:theSeparatorLine];
theSeparatorLine = nil;
}
Related
I am trying to create dynamic UITextViewController for my iOS application.So from my ApplicationViewController I am trying to get the TextView.
Textview is coming properly but the input cursor for my UITextView in coming down.How to get the blue input cursor from the top?
TextViewController.m
+(UITextField *)prepareUITextView
{
UITextView *uiTextView= [[UITextView alloc] initWithFrame:CGRectMake(
10, 80, 300, 100)];
uiTextView.layer.borderWidth = 1.0;
uiTextView.layer.cornerRadius = 8;
uiTextView.font = [UIFont systemFontOfSize:15];
uiTextView.autocorrectionType = UITextAutocorrectionTypeNo;
uiTextView.keyboardType = UIKeyboardTypeDefault;
uiTextView.returnKeyType = UIReturnKeyDone;
[uiTextView becomeFirstResponder];
uiTextView.selectedRange = NSMakeRange(0, 0);
return uiTextView;
}
ApplicationViewController.m
[self.view addSubview:[TextViewController prepareUITextView]];
Try this code
uiTextView.contentInset = UIEdgeInsetsMake(-7.0,0.0,0,0.0);
Adjust the Top value the way you want. this will solve your issue.
I created a UIView with an outlet named _waypointSubview inside a UIScrollView, which is also inside another UIView, and all 3 were created in my storyboard. From here I am adding UITextFields programmatically, and once there are over 5 UITextFields added it is clipped from the UIScrollView. The UIScrollView and the _waypointSubview are then resized to compensate for the extra UITextField added. The user can then scroll down to see all the UITextFields that were clipped from the view. Only the UITextFields that were clipped do not allow editing. I am not sure why that is. So if I add 10 of them, I cannot edit 6 thru 10. I tried programmatically telling it to enable user interaction using the call [waypointTextField setUserInteractionEnabled:YES];but it did not work.
Can someone please explain why I cannot edit those fields? Thank you.
- (IBAction)addWaypoint:(id)sender {
UITextField *waypointTextField = [[UITextField alloc] initWithFrame:CGRectMake(xCord, yCord, 250, 40)];
waypointTextField.borderStyle = UITextBorderStyleRoundedRect;
waypointTextField.font = [UIFont systemFontOfSize:18];
waypointTextField.placeholder = #"enter waypoint";
waypointTextField.autocorrectionType = UITextAutocorrectionTypeNo;
waypointTextField.keyboardType = UIKeyboardTypeDefault;
waypointTextField.returnKeyType = UIReturnKeyDone;
waypointTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
waypointTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
waypointTextField.delegate = self;
[_waypointSubview addSubview:waypointTextField];
UITextField *commentTextField = [[UITextField alloc] initWithFrame:CGRectMake(xCord + 300, yCord, 380, 40)];
commentTextField.borderStyle = UITextBorderStyleRoundedRect;
commentTextField.font = [UIFont systemFontOfSize:18];
commentTextField.placeholder = #"enter comment";
commentTextField.autocorrectionType = UITextAutocorrectionTypeNo;
commentTextField.keyboardType = UIKeyboardTypeDefault;
commentTextField.returnKeyType = UIReturnKeyDone;
commentTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
commentTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
commentTextField.delegate = self;
[_waypointSubview addSubview:commentTextField];
// if waypointArray is not initialized yet, create and add textfield
if (waypointArray == nil) {
waypointArray = [NSMutableArray arrayWithCapacity:1];
[waypointArray insertObject:waypointTextField atIndex:0];
commentArray = [NSMutableArray arrayWithCapacity:1];
[commentArray insertObject:commentTextField atIndex:0];
}
else{
// check amount of space left in scroll view, and adjust if needed
if ([waypointArray count] > 4) {
self.waypointScrollView.contentSize = CGSizeMake(708, 255 + (([waypointArray count] - 4)*50));
CGRect frame = self.waypointSubview.frame;
frame.size.height += 50;
self.waypointSubview.frame = frame;
//[waypointTextField setUserInteractionEnabled:YES];
}
// add new text fields to corresponding arrays
[waypointArray addObject:waypointTextField];
[commentArray addObject:commentTextField];
}
yCord += 50; // update the yCoordinate that will be used for the next textField placement
}
I am assuming you need to connect _waypointSubview to the view controller on your storyboard by just reading the comments.
In order to do that
Right click on the the view controller icon on the storyboard view controller
Click and drag the little circle to the object you are trying to edit or the method you're trying to implement from the IBAction.
Check your bounds of your textfield as -
UITextField *commentTextField = [[UITextField alloc] initWithFrame:CGRectMake(xCord + 300, yCord, 380, 40)];
Here if you observe the xcordinate, an Iphone has a width of 320points.
If your textfield is 380points wide then how can you accomodate it WHEN its (0,0) is starting from xCord(I dont know what is its value) + 300.
Lower the xcordinate value and color its background, to check whether it falls in bounds as pointed out by #rdelmar
I have a UITextField that I'm trying to center in a UIView. However, when I call sizeToFit it leaves a small right margin in the textField which causes the text to be slightly off center when the textField background is clear. I've colored it here so you can see what I'm talking about:
messageField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
messageField.delegate = self;
messageField.font = [UIFont systemFontOfSize:15];
messageField.textColor = [UIColor darkGrayColor];
messageField.textAlignment = NSTextAlignmentLeft;
messageField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"not fitting right" attributes:#{NSForegroundColorAttributeName: [UIColor darkGrayColor]}];
[messageField layoutSubviews];
[messageField sizeToFit];
messageField.backgroundColor = [UIColor lightGrayColor];
And the textfield looks like this (I would post a picture but need more reputation points, this is basically the same though):
not fitting right
Any help would be greatly appreciated!
I should not that if I create a UILabel of the same font size and text, call sizeToFit on the label and then assign the UITextField frame to be the same as the UILabel, it works like a charm.
This is a little different from all the other "How do I center the text in a UILabel" questions here...
I have a UILabel with some text in it, I want to center the text vertically in the UILabel. What's the big deal, right? That's the default. The problem comes because my text is dynamic and I have autoshrink turn on. As the text grows larger, the font size shrinks. You get this behavior.
Notice that the font baseline has not moved, I want it to move so the numbers are centered vertically in the UILabel's frame.
Easy, right? I just remember the frame's original center in viewDidLoad
self.workoutTimeCenter = _workoutTimeLabel.center;
and then I call sizeToFit after I change the the text, right?
[_workoutTimeLabel sizeToFit];
_workoutTimeLabel.center = _workoutTimeCenter;
Well, sizeToFit did, I guess, exactly what it was supposed to do, resize the frame so the text fits without shrinking!
How can I vertically center the text in a UILabel while respecting baselines and autoshrink? (Note, an iOS5 and later solution is fine and I can even deal with an iOS6 and later solution.)
In my experience you can just set the -[UILabel baselineAdjustment] property to UIBaselineAdjustmentAlignCenters to achieve the effect you're describing.
From the docs:
baselineAdjustment
Controls how text baselines are adjusted when text
needs to shrink to fit in the label.
#property(nonatomic) UIBaselineAdjustment baselineAdjustment
Discussion
If the adjustsFontSizeToFitWidth property is set to YES,
this property controls the behavior of the text baselines in
situations where adjustment of the font size is required. The default
value of this property is UIBaselineAdjustmentAlignBaselines. This
property is effective only when the numberOfLines property is set to
1.
and
UIBaselineAdjustmentAlignCenters
Adjust text based relative to the center of its bounding box.
EDIT: adding a full view-controller that demonstrates this:
#interface TSViewController : UIViewController
#end
#implementation TSViewController
- (void) addLabelWithFrame: (CGRect) f baselineAdjustment: (UIBaselineAdjustment) bla
{
UILabel* label = [[UILabel alloc] initWithFrame: f];
label.baselineAdjustment = bla;
label.adjustsFontSizeToFitWidth = YES;
label.font = [UIFont fontWithName: #"Courier" size: 200];
label.text = #"00";
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor lightGrayColor];
label.userInteractionEnabled = YES;
[self.view addSubview: label];
UIView* centerline = [[UIView alloc] initWithFrame: CGRectMake(f.origin.x, f.origin.y+(f.size.height/2.0), f.size.width, 1)];
centerline.backgroundColor = [UIColor redColor];
[self.view addSubview: centerline];
UITapGestureRecognizer* tgr = [[UITapGestureRecognizer alloc] initWithTarget: self action: #selector(onTap:)];
[label addGestureRecognizer: tgr];
}
- (void) viewDidLoad
{
[super viewDidLoad];
[self addLabelWithFrame: CGRectMake(0, 0, 320, 200)
baselineAdjustment: UIBaselineAdjustmentAlignCenters];
[self addLabelWithFrame: CGRectMake(0, 220, 320, 200)
baselineAdjustment: UIBaselineAdjustmentAlignBaselines];
}
- (void) onTap: (UITapGestureRecognizer*) tgr
{
UILabel* label = (UILabel*)tgr.view;
NSString* t = [label.text stringByAppendingString: #":00"];
label.text = t;
}
#end
when working in IB, be sure to set align baselines to center
Note: line break CANNOT be word wrap for this to work, so it will NOT work multiline (good to set the line break to Truncate tail)
-(void)fitVerticallyToLabel:(UILabel *)lbl
{
CGFloat fontSize = lbl.frame.size.width / lbl.text.length;
[lbl setFont:[UIFont fontWithName:#"Helvetica-Bold" size:fontSize]];
CGRect rect = lbl.frame;
rect.origin.y += rect.size.height - fontSize;
rect.size.height = fontSize;
[lbl setFrame:rect];
}
How to Use: Call this method after setting the text to your label.
label.text = #"text";
[self fitVerticallyToLabel:label];
Note: I ahev taken UILabel from xib. You can take it programmatically too in that case you will have to set its text alignment NSTextAlignMentCenter
Try to implement this logic:
-(void)adjustLabel1Text1:(NSString *)text1
{
UILabel *lbl_first = [UIFont fontWithName:#"Helvetica" size:12];
text1 = [text1 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
float hedingLblHeight = [self calculateHeightOfTextFromWidth:text1 : [UIFont fontWithName:#"Helvetica" size:12] :118 :UILineBreakModeWordWrap];
lbl_first.text=text1;
[lbl_first setFrame:CGRectMake(lbl_first.frame.origin.x, lbl_first.frame.origin.y, 118, hedingLblHeight)];
lbl_first.lineBreakMode = UILineBreakModeWordWrap;
lbl_first.numberOfLines = 0;
[lbl_first sizeToFit];
//////////Adjust the lable or any UIControl below this label accordingly.
float endResultHeight=[self calculateHeightOfTextFromWidth:text2 : [UIFont fontWithName:#"Helvetica" size:15] :299 :UILineBreakModeWordWrap];
if(hedingLblHeight>secondImgTitleHeight)
{
[lbl_endResult setFrame:CGRectMake(lbl_endResult.frame.origin.x, lbl_first.frame.origin.y+lbl_first.frame.size.height+5, 299, endResultHeight)];
}
else
{
[lbl_endResult setFrame:CGRectMake(lbl_endResult.frame.origin.x, lbl_first.frame.origin.y+lbl_first.frame.size.height+5, 299, endResultHeight)];
}
lbl_endResult.lineBreakMode=UILineBreakModeWordWrap;
lbl_endResult.numberOfLines = 0;
[lbl_endResult sizeToFit];
}
-(float) calculateHeightOfTextFromWidth:(NSString*)text : (UIFont*) withFont:(float)width :(UILineBreakMode)lineBreakMode
{
CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];
return suggestedSize.height;
}
It has helped me a lot.Hope it works for you.
Try
yourLabel.textAlignment = UITextAlignmentCenter;
I have a UITextView embedded inside a grouped table view cell that I can't seem to update on rotation. It basically keeps the same width (assumed portrait) when it becomes landscape, causing about 40% of the view to be blank on the left side.
Is there a way to update this easily? I was looking at willRotateToInterfaceOrientation but I wasn't sure if that was the right thing to update the view on rotation.
Specifically, I'm thinking the width value should be updated, since it's based on the width.
if (indexPath.section == 1) {
switch (indexPath.row) {
case 0:
{
NSString *label = [self.pedal detail];
CGFloat width = [UIScreen mainScreen].bounds.size.width * .95;
CGSize stringSize = [label sizeWithFont:[UIFont systemFontOfSize:15]
constrainedToSize:CGSizeMake(320, 9999)
lineBreakMode:UILineBreakModeWordWrap];
UITextView *textField = [[UITextView alloc] init];
[textField setFrame:CGRectMake(50,0, width-10, stringSize.height+55)];
[textField setEditable:NO];
textField.backgroundColor = [UIColor clearColor];
textField.font = [UIFont systemFontOfSize:15];
textField.textAlignment = UITextAlignmentLeft;
cell.accessoryView = textField;
textField.text = [self.pedal detail];
[textField release];
break;
}
}
}
You need to set textField.autoresizingMask = UIViewAutoresizingFlexibleWidth, which streches the object on its width on view size change.
Read more at the UIView documentation
You have tried setting the sizing properties in IB right? The way the object scales and what points are anchors should be able to be set there.