Is there any easy way to set a text field's hint to italic, while keeping the actual text non-italicized? The image below shows what I'm trying to accomplish, but doesn't actually work, as text typed into the first text field is still italicized.
If there's no easy way to do this, I'm thinking that I'll have to implement methods on each of the text fields to check the length of the text, and if zero apply the italic font. This would work, right? Is there a better way?
Try this:
UIFont* italicFont = [UIFont italicSystemFontOfSize:[UIFont systemFontSize]];
[yourTextField setValue:italicFont forKeyPath:#"_placeholderLabel.font"];
EDIT:
Ok, here is another (legal) way to do it:
1.Subclass UITextField and override drawPlaceholderInRect::
TextFieldSubclass.h:
#interface TextFieldSubclass : UITextField
#end
TextFieldSubclass.m:
- (void) drawPlaceholderInRect:(CGRect)rect
{
[[UIColor lightGrayColor] setFill];
UIFont* italicFont = [UIFont italicSystemFontOfSize:[UIFont systemFontSize]];
[[self placeholder] drawInRect:rect withFont:italicFont];
}
2.Change your UITextField class and set it to TextFieldSubclass:
And that's it. Enjoy.
Related
I am trying to create a UILabel or UITextView with bold and normal text inside.
I have gone through the attributedstring but when I am setting this in label of my custom cell it doesn't display any text.
I have also used the UITextView setContentToHTMLString: method, but it is undocumented and app get rejected.
Can anyone give some sort of solution to this?
Use "NSAttributedString" to set multiple font text in a single label & use CATextLayer to render it:
just #import "NSAttributedString+Attributes.h"
and then implement it like this:
NSString *string1 = #"Hi";
NSString *string2 = #"How are you ?";
NSMutableAttributedString *attr1 = [NSMutableAttributedString attributedStringWithString:string1];
[attr1 setFont:[UIFont systemFontOfSize:20]];
NSMutableAttributedString *attr2 = [NSMutableAttributedString attributedStringWithString:string2]
[attr2 setFont:[UIFont boldSystemFontOfSize:20]];
[attr1 appendAttributedString:attr2]
CATextLayer *textLayer = [CATextLayer layer];
layer.string = attr1;
layer.contentsScale = [[UIScreen mainScreen] scale];
(Your_text_label).layer = textLayer;
OR (if you want to render on a view directly)
[(Your_View_Name).layer addSublayer:textLayer];
Up until iOS 6.0, you couldn't do this with a normal UILabel or UITextView, but you can use NSAttributedString objects with a few possible open source solutions.
Like TTAttributedLabel or OHAttributedLabel.
One solution built into the iOS SDK, you could also use a CATextLayer which has a string property that can be set to a NSAttributedString.
And, like the commenters below say, yes you can do this with the "attributedText" property. Horray! (for Apple listening to developer's very often repeated feature requests)
I know this is an old thread, but this is something I just discovered myself. At least in Xcode version 4.6.3 this is possible by using an attributed textView. What's even better is that it's possible to all be done in Interface Builder!
Here are the steps:
Place your textView at the desired location
Select the textView and open up the Attributes tab under the Utilities panel
Change the textView text to "attributed"
Enter your desired text
Now, highlight whatever text you want bolded, underlined, etc.
Click on the "T" button next to the fontName
In the popup, select your desired typeface (ex: Bold)
You should see the desired typeface displayed in the Utilities panel
Enjoy!
The following code is for iOS 6.0 and above. The result is that the text "This is bold" will be in bold and "This is not bold." will be normal text.
if ([self.registrationLabel respondsToSelector:#selector(setAttributedText:)])
{
// iOS6 and above : Use NSAttributedStrings
const CGFloat fontSize = 17;
UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
UIFont *regularFont = [UIFont systemFontOfSize:fontSize];
//UIColor *foregroundColor = [UIColor clearColor];
// Create the attributes
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
boldFont, NSFontAttributeName, nil];
NSDictionary *subAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
regularFont, NSFontAttributeName, nil];
const NSRange range = NSMakeRange(0,12); // range of " 2012/10/14 ". Ideally this should not be hardcoded
// Create the attributed string (text + attributes)
NSString *text = #"This is bold and this is not bold.;
NSMutableAttributedString *attributedText =
[[NSMutableAttributedString alloc] initWithString:text
attributes:subAttrs];
[attributedText setAttributes:attrs range:range];
// Set it in our UILabel and we are done!
[self.registrationLabel setAttributedText:attributedText];
}
If you have an issue with editing the attributed text in the inspector, copy/paste the text into a rich text editor, adjust it, switch the textView Text options to Attributed and paste. Vwala.
I have a UITextField with a placeholder. When the user wants to submit the form and he/she hasn't typed anything in the textfield, I would like the placeholder's text color become red. Here are my questions:
Would that go against Apple's User interface guidelines? I don't want my app to be rejected because of such small detail.
How I would do it?
I know I can override the method drawPlaceholderInRect: in a subclass of UITextField. But if I did this, the text would be always red and as I wrote before, I would like it to become red depending on a user defined action.
The only solution I can think of is to use a "default" text for my UITextField (the placeholder's text), display it in light grey as long as the user hasn't typed anything and display it in red when I need it. In other words, I would just mock the placeholder's behavior. But of course, this is not very elegant.
Any ideas?
Just look at this:
Digdog Dig - Change UITextField’s placeholder color without subclassing it
[self.MyTextField setValue:[UIColor darkGrayColor] forKeyPath:#"_placeholderLabel.textColor"];
You can Change the Placeholder textcolor to any color by using the below code. Just try this.
UIColor *color = [UIColor lightTextColor];
YOURTEXTFIELD.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"PlaceHolder Text" attributes:#{NSForegroundColorAttributeName: color}];
like the answer from verklixt but without accessing private api and using UIAppearance:
[[UILabel appearanceWhenContainedIn:[UITextField class], nil] setTextColor:[UIColor darkGrayColor]];
(tested on 5.0 through 7.1)
We can gain access to place holder label using key path,so that we can change color i.e.:
[self.textField setValue:[UIColor **"your color"**]
forKeyPath:#"_placeholderLabel.textColor"];
You can set the placeholder text as a NSAttributedString using this property
NSAttributedString *coloredPlaceholder = [[NSAttributedString alloc] initWithString:#"I am a placeholder string" attributes:#{NSForegroundColorAttributeName: [UIColor redColor]}];
[self.textField setAttributedPlaceholder:coloredPlaceholder];
override
-(void) drawPlaceholderInRect:(CGRect)rect {
[[UIColor darkGrayColor] setFill];
[[self placeholder] drawInRect:rect withFont:[UIFont fontWithName:#"Helvetica-Oblique" size:15.0]];
}
when user does not write any thing in textfield. then put this text as a textfield.text text and change font color.
I had some difficulty implementing color change of placeholder, instead I've found another solution which works perfectly for me.
//On button action change color to red
-(void)btnAction
{
// authentication for missing textfields
if ([textField_firstName.text isEqualToString:#""])
{
textField_firstName.textColor=[UIColor redColor];
textField_firstName.text=#"Enter First Name";
}
}
// in the delegate method of UITextField change the following
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//match the following string with above string and change the string to empty string on textfield click
if ([textField_firstName.text isEqualToString:#"Enter First Name" ])
{
textField_firstName.text=#"";
}
//change back to the text color you use
textField_firstName.textColor=[UIColor blackColor];
}
I have a 3 UITextField with placeholder text set. On one of the UITextField I want the placeholder text to be red.
Now after googling it seems the best way to do this is to subclass UITextField and override drawPlaceholderInRect.
How do I go about subclassing and overriding drawPlaceholderInRect? I've not found any code examples or tutorials on this and I'm new to objective-c and iOS development so finding it tricky to work it out.
Answer:
Created a new objective-c class called CustomUITextFieldPlaceholder which subclassed UITextField. In CustomUITextFieldPlaceholder.m put the following code
#implementation CustomUITextFieldPlaceholder
- (void)drawPlaceholderInRect:(CGRect)rect {
// Set colour and font size of placeholder text
[[UIColor redColor] setFill];
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:12]];
}
#end
To implement the above in your project
#import "CustomUITextFieldPlaceholder.h"
and
IBOutlet CustomUITextFieldPlaceHolder *txtName;
Note: This works and I believe is correct practice, however I have not fully tested it. Hope this example helps others in my situation.
Edit:
Changed
[[UIColor redColor] setFill];
for
[[UIColor colorWithRed:255.0 green:0.0 blue:0.0 alpha:0.7] setFill];
So that I could set the opacity to 70% to mimic default placeholder.
To answer you specific question, this is how subclassing works:
// CustomTextField.h
#interface CustomTextField : UITextField {
}
#end
Here's how to override the method:
#implementation
- (CGRect)placeholderRectForBounds:(CGRect)bounds {
return CGRectMake(x,y,width,height);
}
#end
However I don't think that's the method you want to override. I think this is what you're looking for:
#implementation
- (void)drawPlaceholderInRect:(CGRect)rect {
// Your drawing code.
}
#end
Subclassing won't change the color. I have put forward a work around here.
UITextField placeholder font color white iOS 5?
[yourTextfield setValue:[UIColor colorWithRed:62.0/255.0f green:62.0/255.0f blue:62./255.0f alpha:1.0f] forKeyPath:#"_placeholderLabel.textColor"];
I guess this would be helpful
If I have white text in my UITextField, the selection window (when selecting text) is invisible because the background on the little window is also white.
Any way to fix this?
Sure! The background color of the loupe always matches the backgroundColor property of the text field. (But not the background property.) Example:
textField.textColor = [UIColor whiteColor];
textField.backgroundColor = [UIColor blackColor];
If your text field absolutely requires a transparent background, you'll have to fake it—by using a background image containing the graphics underneath the text field. You may do it manually—by taking a screenshot of your interface and cropping it—or programmatically, like this:
#import <QuartzCore/CALayer.h>
...
// `view` contains the graphics underneath the text field
UIGraphicsBeginImageContext(textField.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGPoint origin = [textField convertPoint:textField.bounds.origin toView:view];
CGContextTranslateCTM(context, -origin.x, -origin.y);
[view.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
textField.background = image;
Since the background is drawn on top of the background color, the text field will appear transparent, and you'll be able to use any background color you want for the loupe.
I'm afraid not. The problem's existed since the earliest days of the iPhone OS—white text appears as white-on-white in the cursor-positioning loupe as well. If this is a serious problem for your app, your only real options are to change the text color or to file a radar feature request with Apple.
Glass takes color from textView.backgroundColor. So I made some dirty hack that works great:
#interface FakeBgTextView : UITextView {
UIColor *_fakeBackgroundColor;
}
- (UIColor *)backgroundColor;
- (void)setFakeBackgroundColor:(UIColor *)color;
#end
#implementation FakeBgTextView
...
- (UIColor *)backgroundColor {
return _fakeBackgroundColor;
}
- (void)setFakeBackgroundColor:(UIColor *)color {
[_fakeBackgroundColor release];
_fakeBackgroundColor = [color retain];
}
...
#end
I know this is a little old, but in iOS5, it appears to be a simulator-only issue. It will render correctly on the device.
One solution that I've employed for this issue is to change the text color to a that will show up in the loupe (but may not look as good in the overall view) while editing and then changing it back to the better display color when finished editing.
It behaves as if you were highlighting the text of the field from a visual standpoint and allows you to use your preferred color for the display of the entered data when not editing.
Set the color to a highlight color that will have enough contrast in the loupe on didBeginEditing, then change it back on didEndEditing.
Just one other possible approach and one I've used in a couple of apps.
eg.
- (void)textFieldDidBeginEditing:(UITextField *)textField {
textField.textColor = [UIColor colorWithRed:116.0/255.0 green:160.0/255.0 blue:246.0/255.0 alpha:1.0];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
textField.textColor = [UIColor colorWithRed:224.0/255.0 green:224.0/255.0 blue:224.0/255.0 alpha:1.0];
}
Is there a way to draw colored text using the UIKit addition for NSString on the iPhone?
Brad's solution adds quite a bit of code if you do not have a context handy (for example if you are doing this in a drawRect: method).
The way to do it without CG is
[[UIColor redColor] set];
or whatever colour you want.
If by the NSString UIKit additions, you mean the category methods drawAtPoint: and drawInRect:, then all you need to do to change their drawn color is place
CGContextSetFillColorWithColor(context, textColor);
before you call drawAtPoint: or drawInRect:. The context's fill color is used as the text's color when it is drawn manually.
The NSString itself has no knowledge of how it will be displayed; it's simply data. The display is handled in whatever view is presenting the text to the user. It can be a UILabel, a UITextfield, etc... These classes typically store their text in either a UILabel or a text property. The text color can usually be set by with:
aLabel = [[UILabel alloc] init];
aLabel.text = #"My String";
aLabel.textColor = [UIColor blackColor];
On the desktop you can use NSAttributedString, but this isn't available on the iPhone. If you're just trying to put colored text on the screen, the easiest way is to just use a UILabel. You can manipulate the text, font and color like this:
myLabel.text = #"Your text";
myLabel.font = [UIFont boldSystemFontOfSize:16];
myLabel.textColor = [UIColor redColor];