Need to center placeholder text in custom UITextField vertically in iOS - ios

I am working on an iOS application where I have subclassed my UITextField in order to set the colour of my placeholder text. I have also centered it horizontally, but my problem is that my placeholder text is not centered vertically, and for some reason, the cursor is not centered within the placeholder text, but instead appears just after the very first character of the placeholder text.
My method in my subclass of UITextField which I override in order to set the colour of the placeholder text and center the text horizontally looks like this:
- (void)drawPlaceholderInRect:(CGRect)rect {
// Set colour and font size of placeholder text
[[UIColor whiteColor] setFill];
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:28] lineBreakMode:UILineBreakModeMiddleTruncation alignment:NSTextAlignmentCenter];
}
Here is my code where I create and set up my UITextField:
_field = [[CustomTextField alloc] initWithFrame:CGRectMake(190, 300, 644, 64)];
_field.delegate = (id)self;
[_field becomeFirstResponder];
[_field setBackgroundColor:[UIColor darkGrayColor]];
[_field setTextColor:[UIColor whiteColor]];
[_field setPlaceholder:#"Enter First and Last Name"];
[[_field layer] setMasksToBounds:8.0f];
[[_field layer] setBorderColor:[[UIColor grayColor] CGColor]];
[[_field layer] setBorderWidth:1.0f];
[_field setFont:[UIFont fontWithName:#"Arial" size:25]];
[_field setReturnKeyType:UIReturnKeyDone];
[_field setTextAlignment:NSTextAlignmentCenter];
_field.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:_field];
I initially subclassed my UITextField because while everything looked fine, my placeholder text for some reason was grey instead of white (which I set as the text colour for the textfield). Ironically, the placeholder text was centered both vertically AND horizontally, AND the cursor was centered. So in order to correct the placeholder text colour, I subclass the UITextField, only to open up more problems now.
Is there a way that I can now center the placeholder text vertically, as well as have the cursor centered as well? Better still, is there a way for me to change the grey colour of the placeholder text to white without having to subclass it? This would be ideal.

I think you forgot to implement in .h ( not in your custom TextField class )
#interface YourView : UIView<UITextFieldDelegate>
and the delegate method didn't called ! The placeholder textcolor must be that color what you want, if you do it like this.
#import "YourView.h"
#implementation YourView
// .....
_field.delegate = self;
_field.textAlignment = NSTextAlignmentCenter;
// ....
You can add some vertical padding like this:
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 30)];
_field.leftView = paddingView;
_field.leftViewMode = UITextFieldViewModeAlways;
from apple developer library:
leftView
The overlay view displayed on the left side of the text
field.
#property(nonatomic, retain) UIView *leftView
Discussion You can use
the left overlay view to indicate the intended behavior of the text
field. For example, you might display a magnifying glass in this
location to indicate that the text field is a search field.
The left overlay view is placed in the rectangle returned by the
leftViewRectForBounds: method of the receiver. The image associated
with this property should fit the given rectangle. If it does not fit,
it is scaled to fit.
If your overlay view does not overlap any other sibling views, it
receives touch events like any other view. If you specify a control
for your view, the control tracks and sends actions as usual. If an
overlay view overlaps the clear button, however, the clear button
always takes precedence in receiving events.
link to apple developer library / UITextField
I hope this helps !

Related

UISearchBar placeholder text color/location

However, I've applied different patches still I'm not able to fix placeholder text position and text color for UISearchbar.
But when I type something, it's showing up at a proper place.
What's the reason?
This is how you can change the colour of your placeholder text:
UITextField *searchField = [self.searchBar valueForKey:#"searchField"];
searchField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Some Text"];
UILabel *placeholderLabel = [searchField valueForKey:#"placeholderLabel"];
placeholderLabel.textColor = [UIColor blueColor];
As for the position, by default, placeholder text is vertically centre positioned in the UISearchBar. From your screenshot, it appears to me that there are few new line characters in the end of the text.
there's a category created on UITextField, inside it, the placeholder text is drawing. I just corrected the frame and now everything works fine, even that solve the color issue as well.

Button label color change doesn't work if font is also changed

Here is a simple example custom button that is set as a class of a button in IB:
#import "TestButton.h"
#implementation TestButton
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
UIFont * font = [UIFont systemFontOfSize:8];
if ( self.setFont )
self.titleLabel.font = font;
self.titleLabel.textColor = [UIColor redColor];
}
#end
If setFont is false, that is, the font is unchanged, the the label text color is red as expected. But if it is true, the button text color is whatever it is set to in IB.
So the question is what's going on here, and how can I change both the text and the font of a button that is assigned in IB programmatically.
In case someone wants to see these peculiarities and also how funky IBDesignable can be, see demo project
Replace
self.titleLabel.textColor = [UIColor redColor];
with
[self setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
The below code is for setting text:
[self setTitle:#"New Text" forState:UIControlStateNormal];
If you wanted to set the title or the color of the title anywhere other than within the button file itself, you could try adding two public properties to the button. One NSString property for the title (self.titleString) and one UIColor property for the title color. You could then set the title and or title color anywhere like in your view controller or view. You would then use these properties within your button file like
[self setTitle:self.titleString forState:UIControlStateNormal];

How to prevent UINavigationBar items hiding if title is long in iOS 6?

I have a problem with UINavigationBar in iOS 6: if navigation bar has too long title, then second (there are two button items) of right bar button items becomes hidden. iOS 7 is okay (must be fixed)
How to prevent such behaviour?
For this you can customize the title label of UINavigationBar. You can set its minimumFontSize property so make the text adjustable.
OR
For iOS 6 you can use below code, so that you can provide a custom label:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
label.textAlignment = UITextAlignmentCenter;
[label setFont:[UIFont boldSystemFontOfSize:16.0]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setText:text];
[self.navigationController.navigationBar.topItem setTitleView:label];
I would add a titleView with an embedded UILabel inside to have full control of how the title is displayed and how much it can "grow".
This question is the same as Back button title missing with long screen title in iOS 7
And the answer is the same answer I gave there: https://stackoverflow.com/a/22029442/341994
I quote that answer:
Make your screen title smaller. You can take control of it by using a titleView that's a UILabel. The advantage is that you can set its size, and that it can truncate its text and/or make the text occupy two lines if the text is too big (rather than just growing, as the title does).

Adding default text to UITextView

I was wondering how to display a default prompt in a UITextView. If I wanted the user to type a description in a text view, the UITextView could have "description" printed in it, and when the user starts to type, it disappears.
For UITextField, there is the placeholderText property, which will display a grayed out text that is removed once the user starts typing.
For UITextView, you can use a custom implementation, such as SZTextView, which implements a similar functionality of a placeholder text.
It wont be a wise idea to use a third party uitextview for placeholder property.
Follow these steps and you will achieve what you need-
set- textview.textcolor=[uicolor greycolor];
textview.text=#"Your initial placeholder text";
Now in -textViewShouldBeginEditing write these lines of codes-
if(textview.color==[uicolor greycolor]){
textview.color=[uicolor blackcolor];
textview.text=#"";
}
Cheers.
The below solution from #svmrajesh isn't complete. You still need to implement an auto-delete functionality so the default text deletes as soon as the user selects the textView.
In my implementation I set the text in the UITextView to lightGrayColor initially so that it looks like default text
[textView setTextColor:[UIColor lightGrayColor]];
Then in the header file I implement the UITextViewDelegate.
#interface YourViewController <UITextViewDelegate>
Then I set the UITextView delegate to self.
[textView setDelegate:self];
Then simply I implement the following delegate method which is fired when the user selects the textView to start typing in their text. The first thing it does is to check if the text color is still set to lightGray.
If it is then the default text is still being displayed, so it is deleted and the textColor is set to black. This simple solution works well for me.
-(void)textViewDidBeginEditing:(UITextView *)textView
{
if(textView.textColor == [UIColor lightGrayColor])
{
textView.textColor = [UIColor blackColor];
textView.text = #"";
}
}
Try this....
For UITextView :
UITextView *myUITextView = [[UITextView alloc] init];
myUITextView.delegate = self;
myUITextView.text = #"placeholder text here...";
For UITextField :
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 200)];
textField.placeholderText = #"Enter your text here";
[self.view addSubview textField];

Table View Header Section not being transparent

I am setting up a UITableView header view and would like this to be semi transparent. So that when the table scrolls up for example the top header is still shown but you can partially see the content through the view.
I have one view and one label (adding to the view) for the header view.
headerLabel.textColor = [UIColor colorWithWhite:0.4 alpha:1.0];
headerLabel.font = [UIFont systemFontOfSize:14.0];
[headerSectionView addSubview:headerLabel];
[headerLabel setBackgroundColor:[UIColor clearColor]];
[headerSectionView setBackgroundColor:[UIColor colorWithWhite:0.847 alpha:5.000]];
However, this does not show as transparent. I have tried to remove the label from the subviews of the view but this does still not work so it is not the label causing this. Is there anything else I need to set on the headerSectionView to have this work?
Your alpha value is incorrect, it should be 0.5f, not 5.0f:
[headerSectionView setBackgroundColor:[UIColor colorWithWhite:0.847 alpha:.5f]];

Resources