drawTextInRect: in UITextView class - ios

I use the drawTextInRect: method in the UITextField class to create outlined text (text with a black border/store effect).
But I need something similar to create the same outlined text in a UITextView, but the drawTextInRect: method is not included here. How can I do this for a textView?
This is the code I use in the UITextField class:
- (void)drawTextInRect:(CGRect)rect {
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(c, 1);
CGContextSetLineJoin(c, kCGLineJoinRound);
CGContextSetTextDrawingMode(c, kCGTextFill);
self.textColor = [UIColor whiteColor];
[super drawTextInRect:rect];
CGContextSetTextDrawingMode(c, kCGTextStroke);
self.textColor = [UIColor blackColor];
[super drawTextInRect:rect];
}
How can I create a similar solution for a textView in a UITextView class?

Are you targeting iOS 6.0+?
Something like this should work, perhaps:
NSAttributedString *yourString = [[NSAttributedString alloc] initWithString:#"Your text" attributes:#{
NSStrokeColorAttributeName:[UIColor redColor],
NSStrokeWidthAttributeName:[NSNumber numberWithFloat:1.0],
NSFontAttributeName:[UIFont systemFontOfSize:24.0f]
}];
yourTextField.attributedText = yourString;
Enjoy

Peter, use negative value for NSStrokeWidthAttributeName. In this case you can set any fill color for text.
NSAttributedString *yourString = [[NSAttributedString alloc] initWithString:#"Your text" attributes:#{
NSStrokeColorAttributeName:[UIColor redColor],
NSStrokeWidthAttributeName:[NSNumber numberWithFloat:-1.0],
NSFontAttributeName:[UIFont systemFontOfSize:24.0f],
NSForegroundColorAttributeName:[UIColor greenColor]
}];
yourTextField.attributedText = yourString;

Related

I have a text view created dynamically no outlet ,I need to change the textview text colour.How could i do this?

I have a text view created dynamically no outlet ,I need to change the textview text colour.i have set dynamically the selectable "True",But the text is always showing in black colour don't know why. I have reached every where in google but not lucky to find the answer.
UITextView *view = [[UITextView alloc]initWithFrame:CGRectMake(50, 100, 100, 100)];
view.textColor = [UIColor redColor];
view.userInteractionEnabled = YES;
[self.view addSubview:view];
Please set text color for each UITextView which you created dynamically :
SomeTextView.textColor = [UIColor redColor];
Hope This will help you !!!
UITextView *txtView = [[UITextView alloc]initWithFrame:CGRectMake(10, 100, 100, 100)];
txtView.textColor = [UIColor blueColor];
[self.view addSubview:txtView];
If u want to display the colour for particular words in textview u can try
NSMutableAttributedString *stringText = [[NSMutableAttributedString alloc] initWithString:#"This is awesome"];
//Green color for the first four characters.
[stringText addAttribute: NSForegroundColorAttributeName value: [UIColor greenColor] range: NSMakeRange(0, 4)];
// Sets the font color of last four characters to yellow.
[stringText addAttribute: NSForegroundColorAttributeName value: [UIColor yellowColor] range: NSMakeRange(14, 10)];
You can achieve this by doing it programatically. Use NSMutableAttributedString
NSMutableAttributedString *stringText = [[NSMutableAttributedString alloc] initWithString:#"This is awesome"];
//Green color for the first four characters.
[stringText addAttribute: NSForegroundColorAttributeName value: [UIColor greenColor] range: NSMakeRange(0, 4)];
// Sets the font color of last four characters to yellow.
[stringText addAttribute: NSForegroundColorAttributeName value: [UIColor yellowColor] range: NSMakeRange(14, 10)];
Set this text to your UITextView. It should work. Also make sure if text changes dynamically you need to care full with NSMakeRange.
Simplest way for changing textview text color is
UITextView *txtviewTextColor = [[UITextView alloc]init];
txtviewTextColor.frame = CGRectMake(10, 30, 200, 100);
txtviewTextColor.scrollEnabled = YES;
txtviewTextColor.textColor =[UIColor greenColor];
txtviewTextColor.userInteractionEnabled = YES;
[self.view addSubview:txtviewTextColor];

Adding Outside Stroke to NSString in UITextView

I am trying to add a stroke to a text and showing in UITextView.
Here is what I want.
In the picture (CREATED FROM PHOTOSHOP) there are same font used, first one has Postion "OUTSIDE" and bottom text has position INSIDE.
I am using following code in Objective-C
NSShadow * shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor blackColor];
shadow.shadowOffset = CGSizeMake(0, 0);
NSDictionary * textAttributes =
#{ NSForegroundColorAttributeName : [UIColor whiteColor],
NSShadowAttributeName : shadow,
NSStrokeColorAttributeName : [UIColor blackColor],
NSStrokeWidthAttributeName : [NSNumber numberWithFloat:-3.0],
NSFontAttributeName : [UIFont fontWithName:#"UbuntuCondensed-Regular" size:40] };
textTitleResult.attributedText = [[NSAttributedString alloc] initWithString:textTitle.text
attributes:textAttributes];
I am achieving INSIDE effect. How can I set position to OUTSIDE
I don't think there's a property that can do exactly when you want. However, I have an idea which will possibly provide you with a way to "fake" it.
You can add another label at the exact position of the original one. The new label would have half the size of the stroke but the stroke color would be the same color as the text. Since the stroke "sits" on the border (half part inside the text and half part outside), it would block half of the original stroke, making it look as if it's on the outside.
Without making the code too pretty, it would look something like this:
UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 40)];
infoLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:infoLabel];
UILabel *secondInfoLabel = [[UILabel alloc] initWithFrame:infoLabel.frame];
secondInfoLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:secondInfoLabel];
NSDictionary * secondTextAttributes =
#{ NSForegroundColorAttributeName : [UIColor whiteColor],
NSStrokeColorAttributeName : [UIColor whiteColor],
NSStrokeWidthAttributeName : [NSNumber numberWithFloat:-3.0],
NSFontAttributeName : [UIFont systemFontOfSize:40] };
secondInfoLabel.attributedText = [[NSAttributedString alloc] initWithString:#"Welcome To" attributes:secondTextAttributes];
NSShadow * shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor blackColor];
shadow.shadowOffset = CGSizeMake(0, 0);
shadow.shadowBlurRadius = 2;
NSDictionary * textAttributes =
#{ NSForegroundColorAttributeName : [UIColor whiteColor],
NSShadowAttributeName : shadow,
NSStrokeColorAttributeName : [UIColor blackColor],
NSStrokeWidthAttributeName : [NSNumber numberWithFloat:-6.0],
NSFontAttributeName : [UIFont systemFontOfSize:40] };
infoLabel.attributedText = [[NSAttributedString alloc] initWithString:#"Welcome To" attributes:textAttributes];
Another option would be to create your own custom view and draw it yourself just the way you want it (in drawRect).

How to get text for in self drawn view from viewcontroller

I've got self drawn view with text:
NSString* text = #"text";
UIFont* font = [UIFont systemFontOfSize:14.f];
NSShadow* shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeMake(1, 1);
shadow.shadowColor = [UIColor whiteColor];
shadow.shadowBlurRadius = 0.5;
NSDictionary* attirbutes =
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor grayColor], NSForegroundColorAttributeName,
font, NSFontAttributeName,
shadow, NSShadowAttributeName, nil];
CGSize textSize = [text sizeWithAttributes:attirbutes];
CGRect textRect = CGRectMake(0,
0,
textSize.width, textSize.height);
textRect = CGRectIntegral(textRect);
[text drawInRect:textRect withAttributes:attirbutes];
In my ViewController I want to get it from another class and draw it in my selfDrawnView. How Can I perform it?
Not really clear what exactly are you asking. If you subclassed UIView with your new view class and overridden drawRect method there you need to add a property to hold the text you're drawing.
Then in your view controller you just pass the text to that property and call [myCustomView setNeedDisplay] to force redrawing.

UITextField set placeholder color as dark in iOS

I have tried to set UITextField "Placeholder" color as dark.
NSAttributedString * search = [[NSAttributedString alloc] initWithString:#"Search" attributes:#{NSForegroundColorAttributeName: [UIColor blackColor]}];
textField.attributedPlaceholder = search;
But still UITextField showing light gray color in "Placeholder".
Is it possible to set dark "placeholder" color for UITextField?
Also I Have tried the another method as well
[textField setValue:[UIColor blackColor] forKeyPath:#"_placeholderLabel.textColor"];
But both methods are working in iOS 7, But not working on iOS 6.
Is it possible to set dark "placeholder" color for UITextField in iOS 6 target?
Thanks!
As suggested by Apple, subclassing UITextField and overriding - (void)drawPlaceholderInRect:(CGRect)rect is the way to go:
- (void)drawPlaceholderInRect:(CGRect)rect {
UIColor *colour = [UIColor lightGrayColor];
if ([self.placeholder respondsToSelector:#selector(drawInRect:withAttributes:)])
{ // iOS7 and later
NSDictionary *attributes = #{NSForegroundColorAttributeName: colour, NSFontAttributeName: self.font};
CGRect boundingRect = [self.placeholder boundingRectWithSize:rect.size options:0 attributes:attributes context:nil];
[self.placeholder drawAtPoint:CGPointMake(0, (rect.size.height/2)-boundingRect.size.height/2) withAttributes:attributes]; }
else { // iOS 6
[colour setFill];
[self.placeholder drawInRect:rect withFont:self.font lineBreakMode:NSLineBreakByTruncatingTail alignment:self.textAlignment];
}
}
Credit: http://www.brightec.co.uk/blog/how-change-colour-uitextfields-placeholder-text-ios7-and-still-support-ios6
Overriding the drawPlaceholderInRect: method to draw our own placeholder text.
- (void)drawPlaceholderInRect:(CGRect)rect
{
UIColor *colour = [UIColor darkColor];
if ([self.placeholder respondsToSelector:#selector(drawInRect:withAttributes:)]) {
// iOS7 and later
NSDictionary *attributes = #{NSForegroundColorAttributeName: colour, NSFontAttributeName: self.font};
CGRect boundingRect = [self.placeholder boundingRectWithSize:rect.size options:0 attributes:attributes context:nil];
[self.placeholder drawAtPoint:CGPointMake(0, (rect.size.height/2)-boundingRect.size.height/2) withAttributes:attributes];
}
else {
// iOS 6
[colour setFill];
[self.placeholder drawInRect:rect withFont:self.font lineBreakMode:NSLineBreakByTruncatingTail alignment:self.textAlignment];
}
}
OR
This case, likely crash if the internal variable changes in the future
Programmatically:
[self.MyTextField setValue:[UIColor blackColor] forKeyPath:#"_placeholderLabel.textColor"];
In UIStoryBoard:
OR
EDIT:
Playing with UITextFiled delegates. its like a tricky:
-(void)viewDidLoad{
self.mytxtField.text = #"PlaceholderText";
self.mytxtField.delegate = self;
self.mytxtField.textColor = [UIColor darkGrayColor];
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if ([self.mytxtField.text isEqualToString: #"PlaceholderText"]) {
self.mytxtField.text = #"";
self.mytxtField.textColor = [UIColor blackColor];
}
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
if ([self.mytxtField.text length]<1) {
self.mytxtField.text =#"PlaceholderText";
self.mytxtField.textColor = [UIColor darkGrayColor];
}
For iOS8, you can take advantage of #IBDesignable & #IBInspectable.
Here is a UITextField subclass in Swift which lets you set the placeholder color in Interface Builder and see the result instantly:
#IBDesignable class EDTextField: UITextField {
#IBInspectable var placeholderColor: UIColor = UIColor.whiteColor() {
didSet {
if let placeholder = self.placeholder {
let attributes = [NSForegroundColorAttributeName: placeholderColor]
attributedPlaceholder = NSAttributedString(string: placeholder, attributes: attributes)
}
}
}
}
Its very simple....
Try this to change placeholder text color..
UIColor *color = [UIColor blackColor];
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholderText attributes:#{NSForegroundColorAttributeName: color}];
Here is Latest way to Change FONT and COLOR of placeholder text
self.emailField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Email" attributes:#{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName :[UIFont fontWithName:#"OpenSans" size:14.0]}];
Try this
[textField setValue:[UIColor blackColor] forKeyPath:#"_placeholderLabel.textColor"];
To make it future-proof and completely customizable (color, font, size, etc.), I would just add a UILabel on top and hide/show it manually according to the UITextField contents.
Just make sure to set the label's userInteractionEnabled to NO.
It will change textField's palceholder textColor
[yourTextField setValue:[UIColor colorWithRed:150.0f/255.0f green:150.0f/255.0f blue:155.0f/255.0f alpha:1] forKeyPath:#"_placeholderLabel.textColor"];

Effect in Text - objective-c

I have the sources of these letters and I am trying to create the gray effect around in objective-c.
But I can not, does anyone have an idea how to do this?
Take a look at GSBorderLabel.
It's good because it adds outer border and not inner border on characters and it's very easy to customise it.
For example:
GSBorderLabel *myLabel = [[GSBorderLabel alloc] initWithFrame:CGRectMake(0, 50, 300, 100)];
myLabel.textColor = [UIColor yellowColor];
myLabel.textAlignment = NSTextAlignmentCenter;
myLabel.borderColor = [UIColor grayColor];
myLabel.borderWidth = 20;
myLabel.text = #"Review";
myLabel.font = [UIFont fontWithName:#"Party LET" size:60.0];
If you're targeting iOS 6 and up, there isn't much over head for this. You can simply use NSAttributedString to add a stroke to an existing font. Mind you, this will not add the stroke to the font, just render it on screen.
NSDictionary *attributes = #{NSStrokeColorAttributeName: [UIColor grayColor], NSStrokeWidthAttributeName : #3};
NSString *inputText = #"Some text";
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:inputText attributes:attributes];
[self.someLabel setAttributedText:attributedString];
You can try this:
#import <QuartzCore/QuartzCore.h>
....
titleLabel.layer.shadowColor = [[UIColor grayColor] CGColor];
titleLabel.layer.shadowRadius = 3;
titleLabel.layer.shadowOpacity = 1;
titleLabel.layer.shadowOffset = CGSizeMake(0, 0);

Resources