Different colors in UITextView - ios

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

Related

UILabel Character spacing to include background and underline attributes

I am wanting a UILabel where each character is underlined and has a background, while also having a space between the background of each individual character - so essentially each character is in it's own "box"
I have tried using attributedText on a UILabel but having set the NSBackgroundColorAttributeName and NSKernAttributeName the background fills the entire label, essentially I'd like a gap of clear color between each letter.
Is there a way of doing this with attributed text, or will this need to be resolved with another solution?
I currently have tried the following as an example:
NSMutableAttributedString* string = [[NSMutableAttributedString alloc] initWithString:#"EXAMPLE"];
[string addAttribute:NSBackgroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, string.length)]
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, string.length)]
[string addAttribute:NSKernAttributeName value:#(5) range:NSMakeRange(0, [string length])];
[label setAttributedText:string];
Current result:
Desired Result:
(you can ignore the colors, this is just an example)

Break UILabel into 2 lines with separate background?

http://imgur.com/yVkhHon.jpg
I want my text on lines but with separate background. How can i achieve this using UILabel ?
I tried padding etc but nothing works it just show single red box if i select background color for label.
Any help on how to get same effect like image ?
Use the following code:
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:self.yourLabel.text];
//EDITED
[text addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, text.length/2)];
[text addAttribute:NSBackgroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(text.length/2, text.length/2)];
[self.yourLabel setAttributedText: text];
Use NSAttributesString. Create a UILabel, create an NSAttributesString. Then instead of using setText of label, call setAttributedText using the string object you created earlier.
More help on Apple Documentation
[mutableAttributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:selectedRange];

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

Multiple placeholder text color in 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 .

Placing PlaceHolder In TextField

I want to add mandatory field mark in textfield placeholder text like below image please suggest me i am totally confused how to do it
You can use :
NSMutableAttributedString *attriButedString = [[NSMutableAttributedString alloc]initWithString:#"Mandatory*"];
[attriButedString addAttribute:NSForegroundColorAttributeName
value:[NSColor lightGrayColor]
range:NSMakeRange(0, 9)];
[attriButedString addAttribute:NSForegroundColorAttributeName
value:[NSColor redColor]
range:NSMakeRange(9, 1)];
[[self.textField cell] setPlaceholderAttributedString:attriButedString];
This will look like as:
Note: Instead of hard-coded range you can calculate according to your stringlength.
If you are creating TextField from xib click on text field and go to attribute inspecter and fill your placeholder name in placeholder text feild
Try with this.....
[tfSubject setValue:[UIColor colorWithRed:249.0/255.0 green:204.0/255.0 blue:88.0/255.0 alpha:1.0] forKeyPath:#"_placeholderLabel.textColor"];

Resources