ios uilabel tail truncated trailing dot color - ios

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;

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

Custom multiline UILabel with black background behind text

I need to create custom multiline UILabel with black background behind text, with some pading of 2 pixel from left, right, top and bottom. Padding must be same for all side, let say if padding between [Songs.. and black border = 2 then I need 2pixel padding between "Perfect" and right border, same after "mp3"
I tried this:
[string addAttribute:NSBackgroundColorAttributeName
value:[UIColor blackColor]
range:NSMakeRange(0, string.length)];
but its not giving me extra padding.
Could you guys suggest me any library or some hint will also be helpful
I would use a UITextView rather than a UILabel. Do something like the following:
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, y, self.view.frame.size.width - 20, 40)];
textView.textAlignment = NSTextAlignmentJustified;
textView.scrollEnabled = NO;
textView.backgroundColor = [UIColor blackColor];
textView.userInteractionEnabled = NO;

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 .

Letterpress effect for UILabel in iOS 7 SDK

In the WWDC 2013 videos they show that a developer can use a letterpress effect on text.
Does anyone have any example code of how to do this/how to do this with a UILabel?
They've made it pretty simple now.
//Use any font you want or skip defining it
UIFont* font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
//Use any color you want or skip defining it
UIColor* textColor = [UIColor blueColor];
NSDictionary *attrs = #{ NSForegroundColorAttributeName : textColor,
NSFontAttributeName : font,
NSTextEffectAttributeName : NSTextEffectLetterpressStyle};
NSAttributedString* attrString = [[NSAttributedString alloc]
initWithString:note.title
attributes:attrs];
myTextLabel.attributedText = attrString;
It's part of NSAttributedString UIKitAdditions

Title text color in bar button item

I have a bar button item that only functions to display a string that is updated when something is performed on screen. I have set the text color to white. But, it is displayed on screen with a gray color. Whatever color I change the text to I still get a grayish color instead of the desired color. Why am I not getting the the right color? Is it a property that I'm missing?
UIColor *buttonColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0];
_timeButton.title = [NSString stringWithFormat:#"Updated at: %#",dateString];
[_timeButton setStyle:UIBarButtonItemStylePlain];
_timeButton.tintColor = [UIColor clearColor];
[_timeButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont boldSystemFontOfSize:15], UITextAttributeFont,
buttonColor ,UITextAttributeTextColor,
nil]
forState:UIControlStateNormal];
Turns out that my text color of my button was gray because the button was not enabled. Setting the button to enabled changed the color of the text to white.
[_timeButton setEnabled:YES];

Resources