UITextField placeholder shifts when subclassed - iphone

I am using a subclassed UITextField to use a custom font. When I set a placeholder for that textfield it gets shifted up a little, but the when I start entering the text, the frame of text has no problem. Is there a way to fix the placeholder shifting issue.

self.txtFirstName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.txtFirstName.autocapitalizationType = UITextAutocapitalizationTypeSentences;
self.txtFirstName.placeholder = #"Enter First Name here";
self.txtFirstName.textAlignment = UITextAlignmentLeft;

I had same issue and I tried setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter which solved my issue.
UITextField *txtPassword = [[UITextField alloc] initWithFrame:CGRectMake(15,55, 255, 25)];
[txtPassword setPlaceholder:#"Password..."];
[txtPassword setBackgroundColor:[UIColor whiteColor]];
[txtPassword setFont:[UIFont fontWithName:#"Helvetica" size:15]];
[txtPassword setSecureTextEntry:YES];
[txtPassword setTextAlignment:NSTextAlignmentCenter];
[txtPassword setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[txtPassword becomeFirstResponder];
Hope this helps.

Related

How to highlight only text in UILabel - IOS

I just want to highlight only text in UILabel, I have tried by giving backgroundColor for label, but it is highlighting the empty spaces also looks not good. So Is there any way to highlight text without resizing UILabel.
Please check the image, here labels are bigger than the text (center aligned)
Thanx.
Most of the other solutions don't consider text that spans multiple lines while still only highlighting the text, and they are all pretty hacky involving extra subviews.
An iOS 6 and later solution is to use attributed strings:
NSMutableAttributedString *s =
[[NSMutableAttributedString alloc] initWithString:yourString];
[s addAttribute:NSBackgroundColorAttributeName
value:[UIColor greenColor]
range:NSMakeRange(0, s.length)];
label.attributedText = s;
This will add a subview behind the text, with the correct size:
CGSize size= [[label text] sizeWithFont:[UIFont systemFontOfSize:18.0]];
NSLog(#"%.1f | %.1f", size.width, size.height);
NSLog(#"%.1f | %.1f", label.frame.size.width, label.frame.size.height);
UIView *highlightView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
[highlightView setBackgroundColor:[UIColor greenColor]];
[self.view insertSubview:highlightView belowSubview:label];
[highlightView setCenter:label.center];
And don't forget: [label setBackgroundColor:[UIColor clearColor]];
try this
MTLabel
MTLabel *label4 = [MTLabel labelWithFrame:CGRectMake(20, 270, 250, 60)
andText:#"This label is highlighted, has a custom line height, and adjusts font size to fit inside the label."];
[label4 setLineHeight:22];
[label4 setFont:[UIFont fontWithName:#"Helvetica" size:30]];
[label4 setAdjustSizeToFit:YES];
[label4 setMinimumFontSize:15];
[label4 setFontHighlightColor:[[UIColor orangeColor] colorWithAlphaComponent:0.5]];
[self.view addSubview:label4];
You can have a UIImageView, set its background color of your choice then place your UIlabel on it.
That will surely solve your problem.
Here is Workaround code :
UIImageView * imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 100, 40)];
[imgView setBackgroundColor:[UIColor brownColor]];
[self.view addSubview:imgView];
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextAlignment:UITextAlignmentCenter];
label.text = #"My Label";
[imgView addSubview:label];
[label release];
[imgView release];
You can also achieve the same using Interface Builder.

Creating custom UITextView with UILabels as well as text in it

I would like to create a custom UITextView with the ability to enter text in it, as well as programmatically adding some UILabels there that would act like text (I need to delete them with the 'backspace' button when the cursor comes near them).
This UITextView should be expandable and the labels can have different width.
Any ideas of how you can create this sort of thing, any tutorials or such?
you can create textfield using this code.
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = #"enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
[self.view addSubview:textField];
[textField release];
And for creating label you can use this code:
CGRect labelFrame = CGRectMake( 10, 40, 100, 30 );
UILabel* label = [[UILabel alloc] initWithFrame: labelFrame];
[label setText: #"My Label"];
[label setTextColor: [UIColor orangeColor]];
label.backgroundColor =[UIColor clearColor];
[view addSubview: label];
and for deleting label when backspace tape use this method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ([string isEqualToString:#""]) {
NSLog(#"backspace button pressed");
[label removeFromSuperview];
}
return YES;
}
If the backspace button is pressed, then the replacementString(string) will have null value. So we can identify backspace button press using this.

TailTruncation is not working with OHAttributedLable

I am having OHAttributedLable like following
lblHeaderTitle =[[OHAttributedLabel alloc]init];
[lblHeaderTitle setBackgroundColor:[UIColor clearColor]];
[lblHeaderTitle setFrame:CGRectMake(15, 8,(WidthForBoldText(txt1))+45, height)];
//[lblHeaderTitle setNumberOfLines:2];
[lblHeaderTitle setLineBreakMode:UILineBreakModeTailTruncation];
[lblHeaderTitle setNumberOfLines:0];
lblHeaderTitle.attributedText = attrStr;
it is displaying all in one line and remaining part is not visible
now the problem is that i am not able to wrap or truncate text in this lable can anybody help me with this.
what i want is if my text is
"Abcdefghijklmnopqrstuvwxyz" then it will display
"Abcdefghijklmn..." or
"Abcdefghijklmn
opqrstuvwxyz"
anything will work.
Thanks in advance.
lableName.numberOfLines = 0;
lableName.lineBreakMode = UILineBreakModeWordWrap;

How to change the uitextview font size

can any one help to change the font size in UITextView.
[textView setFont:[UIFont fontWithName:#"ArialMT" size:30]];
I use this one but it doesn't work.
Have you bind textView with xib or allocated textView?
If yes,
You can use following code to check whether this works
[textView setFont:[UIFont boldSystemFontOfSize:15]];
or
[textView setFont:[UIFont systemFontOfSize:15]];
Check, if "Selectable" is checked in the xib/Storyboard attribute inspector. If it's not selectable, the size wont be changed
Swift 4.0
textField.font = UIFont.systemFont(ofSize: 17.0)
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
[textView setFont: [UIFont fontWithName:#"ArialMT" size:30]];
textView.text = #"test my font size";
[self.view addSubview:textView];
[textView release];
Your code is correct. The problem is from other part of your code. Provide more details about your textView
Set custom font and size in Swift:
textView.font = UIFont(name: "ArialMT", size: 20)
Use this:
UITextView *myTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
myTextView.font = [UIFont fontWithName:#"arial" size:14];
Here is for swift 3.0
textview.font = UIFont(name: textview.font.fontName, size: 18)
I tried it the same way like you with
[textView setFont:[UIFont fontWithName:kFontName size:kFontSize]];
I set the UITextView inside ViewDidLoad. My problem was, I transformed my view:
[_viewEnd setTransform:CGAffineTransformMakeScale(0.0, 0.0)];
My problem was solved with setting the properties of the UITextView after transforming my UIView which includes the UITextView.

How can I make text in a UILabel non-centered?

I have a UILabel:
descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 30, 130, 150)];
[descriptionLabel setFont:[Utils getSystemFontWithSize:14]];
[descriptionLabel setBackgroundColor:[UIColor clearColor]];
[descriptionLabel setTextColor:[UIColor whiteColor]];
descriptionLabel.numberOfLines = 0;
descriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
[self addSubview:descriptionLabel];
If the text is only 1 line long it will appear in the middle of the label.
Is there anyway I can have the text display from the top left corner of the label instead of the middle center?
You can set
[descriptionLabel setTextAlignment:UITextAlignmentLeft];
or if you prefer dot syntax
descriptionLabel.textAlignment = UITextAlignmentLeft;
for left/right alignment set the textAlignment property of UILabel to either UITextAlignmentLeft or UITextAlignmentRight.
for more fine tuning you can override drawTextInRect: