UISearchBar placeholder text color/location - ios

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.

Related

iOS text field doesn't show placeholder text

So I have some basic textfields with a custom background, that is a line at the bottom and the rest of it it's transparent, but now the placeholders don't show anymore.
These are the text fields
And config
I have found a solution that works, you need to select the input and set the following on User Defined Runtime Attributes
It's better to do this:
if ([self.textField respondsToSelector:#selector(setAttributedPlaceholder:)]) {
self.textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.textField.placeholder attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
I had the same problem. Turns out it had nothing to do with me having set borderStyle to UITextBorderStyleNone. It was caused by me subclassing UITextField to add a manually drawn border along the bottom only and failing to call the super implementation in my override of layoutSubviews. Once I had sorted that, my placeholder text was back.

how to set different font size to text of button name in storyboard

I want to make application in which i want to make a login screen. It has button having name "Log inn with Facebook". I want font size of Facebook greater than Log in with. How can I set it in main storyboard?Can anyone plz help me?
Follow these steps-
1.Select button
2.Change its type to custom in Attributes inspector
3.Change its title from Plain to Attributed
4.Change the part of title which you want to be set bold
A solution would be to have a UIView with two different UILabel as subviews ( your "Log in with" label and your "Facebook" label ).
Then, above this UIView you could have a UIButton with a white background and an alpha equal to 0.01 so the button would be touchable but invisible.
This UIButton would be the functional part of your UI.
NSMutableAttributedString *titleText = [[NSMutableAttributedString alloc] initWithString:#"Login with Facebook"];
[yourbutton.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
// Set the font to bold from the beginning of the string to the ","
[titleText addAttributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] range:NSMakeRange(0, 10)];
// Set the attributed string as the buttons' title text
[yourbutton setAttributedTitle:titleText forState:UIControlStateNormal];
Instead of creating NSAttributedString create NSMutableAttributedString then you can just set the string like this.

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

Need to center placeholder text in custom UITextField vertically in 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 !

Text in UITextView not display correctly, invisible, but occupied spaces

There is a UITextView in the view, the view controller is showing with Modal style. In my viewDidLoad method, I set the text of this UITextView, but, the text is not showing. Image below showing the error. Text color is black.
The weird thing is , when I long tap in the text view or tap [return] in keyboard, the text become visible. One thing I noticed is this error only occurred when the text I set is longer than the UITextView frame width, and the last word is not broken such as a long url.
I think the problem is maybe the word wrap not work correctly.
Thanks in advance.
Code like below:
UITextView *myTextView = [[UITextView alloc]initWithFrame:CGRectMake(10, 10, 520, 220)];
myTextView.textColor = [UIColor blackColor];
myTextView.font = [UIFont systemFontOfSize:20];
myTextView.layer.cornerRadius = 5;
myTextView.delegate = self;
myTextView.text = #"long text should some like http://stackoverflow.com/posts/11200726/edit";
[self.view addSubview:myTextView];
[myTextView release];
RESOVLED. In viewDidLoad method, add code below:
CGRect tempFrame = myTextView.frame;
[myTextView setFrame:CGRectZero];
[myTextView setFrame:tempFrame];
Subclass UITextView, to a class of your own, let's say MyUITextView:UITextView.
Initialize it offscreen with
[[MyUITextView alloc] initWithFrame:CGRectZero]
In MyUITextView override the method
-(void)willMoveToWindow:(UIWindow *)newWindow
and in it, set self.frame to the proper value.
this worked for me!
Are you sure the text color is black? Also make sure that the image is behind the textView in the view hierarchy. TextView handles Word wrap automatically.
I think in this case, the viewDidLoad is getting called before modelViewController is presented.
Set the text after modalViewController is presented.
If that also not working, then after presenting model, call a method to set the text explicitly.
Check your view hierarchy. You might have added another textView above that view.
Try changing the background color of textView. It must appear is UITextView is added there. And make sure you are not adding another view as a subview above your UITextView. If the textColor is black it must be visible, wordwrap is done by default automatically unless you change the property.Also try to check the attributes settings in XIB if added and plz show the code if adding programmatically.
I think problem in:
self.view
Try add this code line at top
self.view = [[[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
And if not will help, try remove this code line
myTextView.layer.cornerRadius = 5;

Resources