Multiple placeholder text color in iOS - ios

Is it possible to have multiple color text in a placeholder?
For example
Write Text - I will prepend this.
"Write Text" will be black, while "I will prepend this." will be grey.

Multiple Textfiled placeholder in ios.
self.txt_First.attributedPlaceholder = [self String:#"Brewery (Sierra Nevada)" Range:7];
In above textfield first seven character is red and other are gray.
Use the below mehod for setting multiple color.
-(NSAttributedString *)String:(NSString *)str Range:(int )Rannge
{
NSMutableAttributedString *attString =
[[NSMutableAttributedString alloc]
initWithString:str];
[attString addAttribute: NSForegroundColorAttributeName
value: [UIColor redColor]
range: NSMakeRange(0,Rannge)];
[attString addAttribute: NSForegroundColorAttributeName
value: [UIColor graycolor]
range: NSMakeRange(Rannge + 1,str.length - Rannge -1)];
return attString;
}
Multiple placeholder color Output :

I have Simple way for this .
Add UILabel in UITextField below and set UITextField clearColor .and set your UILabel text as you required and set that text color same as placeholder color .

Related

How to color text in UITextView

I've got four buttons on the view controller and a text view. These five buttons has colors, for example red, yellow, green, blue and black.
When the user started to type without pressing those buttons the color of the text view being typed should have black color text. If user press red button then the color of the text from that point should be red until the user press any other colored button.
How to do this ? Ive followed this tutorial https://www.objc.io/issues/5-ios7/getting-to-know-textkit/
But do not know how to customized it to what I want to achieve.
Here is how I proceed if it can helps you:
1- add one property to retain current Color and initialize it with black color
#property (nonatomic, retain) UIColor *curColor;//in your interface declaration
self.curColor = [UIColor blackColor];//Init it in Viewdidload for example
2- implements UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSAttributedString *currentText = self.Textview.attributedText;//To store current text and its attributs
NSAttributedString *newOneText = [[NSAttributedString alloc] initWithString:text attributes:#{NSForegroundColorAttributeName:self.curColor}];//for the new text with selected color
NSMutableAttributedString *shouldDisplayText = [[NSMutableAttributedString alloc] initWithAttributedString: currentText];
[shouldDisplayText appendAttributedString: newOneText];// add old and new text
self.Textview.attributedText = shouldDisplayText;//set it ton control
return NO;
}
3- Add IBAction for changing color
=>
- (IBAction) redColorClicked
{
self.curColor = [UIColor colorWithRed:1.0f green: 0.0f blue:0.0f alpha:1.0f];
}
- (IBAction) blueColorClicked
{
self.curColor = [UIColor colorWithRed:0.0f green: 0.0f blue:1.0f alpha:1.0f];
}
You need to use NSAttributedString class.
let defaultAttributes = [NSFontAttributeName: UIFont.systemFontOfSize(UIFont.systemFontSize()),
NSForegroundColorAttributeName: UIColor.blackColor()]
let text = "this text is red and yellow"
let str = NSMutableAttributedString(string: text, attributes: defaultAttributes)
str.setAttributes([NSForegroundColorAttributeName: UIColor.redColor()], range: (text as NSString).rangeOfString("red"))
str.setAttributes([NSForegroundColorAttributeName: UIColor.yellowColor()], range: (text as NSString).rangeOfString("yellow"))
textView.attributedText = str
You can use NSMutableAttributedString to achieve that. The idea is the following (I didn't tested it, just wrote it here by hand):
NSString *str = #"stackoverflow";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
// Set foreground color of "stack" substring in our string to red
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor];
range:NSMakeRange(0, 5)];
Using this method you can achieve what you want applying color to ranges you want in the text.
You can set the attributed text to you UILabel like that:
yourLabel.attributedText = attributedString

ios uilabel tail truncated trailing dot color

not sure if this has been asked before.
I got a UILabel that is white text on purple background colour.
What I notice is the tail truncation ... are not white colour but a grey color.
Is there a way to change the color to be white, matching the UILabel text color ?
This should work
UIColor *color = [UIColor whiteColor];
NSAttributedString *text = [[NSAttributedString alloc] initWithString:self.text attributes:#{ NSForegroundColorAttributeName : color }];
self.label.attributedText = text;

How to apply color for underline style in NSAttributedString (ios)?

Let's say i'm adding underline style in NSAttributedString:
[attrStr addAttribute:NSUnderlineStyleAttributeName value:#(NSUnderlineStyleSingle) range:range];
Is it possible to apply color (not only underline style) to underline without changing text color?
Yes. Use NSUnderlineColorAttributeName to set the underline color.
[attrStr addAttribute:NSUnderlineColorAttributeName value:[UIColor redColor] range:range];

How to change UILable text color change for a certain words? [duplicate]

This question already has answers here:
UILabel with text of two different colors
(20 answers)
Closed 8 years ago.
Say i have UIlabel and i want to change color of text at certain word. For example my label text is
"Shop now and get up 50% off Select shoes Limited time offer"
i which "50%" and "Limited" want change color
predefined some word if accrue in label text then change color. fist find and then change color and font or sub string.
To do this use NSAttributedString:
NSMutableAttributedString *text =
[[NSMutableAttributedString alloc]
initWithAttributedString: label.attributedText];
[text addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(10, 1)];
[label setAttributedText: text];
For details :https://github.com/joaoffcosta/UILabel-FormattedText
You can use NSMutableAttributedString
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:#"Shop now and get up 50% off Select shoes Limited time offer"];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(, )];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(, )];

Different colors in UITextView

I try to add string with different colors in my UITextView. I wrote this code
NSMutableAttributedString* attString =
[[NSMutableAttributedString alloc]initWithString:view.text]; //view is my UITextView
[attString addAttribute:(NSString*)kCTForegroundColorAttributeName
value:[UIColor greenColor]
range:(NSRange){attString.length-8, 8}];
view.attributedText = attString;
Maybe it is incorrect attribute, can you tell me what attribute change color for text?
Try using NSForegroundColorAttributeName instead of kCTForegroundColorAttributeName.
NSAttributedString attribute keys

Resources