Segmented Control set attributed title in each segment - ios

Actually i have a segmented control with 4 segment. I need to add attributed text in each segment like "Notification (2)". Here (2) will be in different color and Notification will be in different color.
I have search some third party library , But it doesn't work for me
Thanks & Regards

There is limitation to use attributed text as we use for label or button. But you may try below method to achieve your requirements.
-(void)SetSegmentValue:(NSString *)value forSegment:(int)index RangeOfBlueColor:(NSRange)bluecolorRange{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:value];
NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle defaultParagraphStyle]mutableCopy];
[paragrahStyle setAlignment:NSTextAlignmentCenter];
[paragrahStyle setLineBreakMode:NSLineBreakByWordWrapping];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, value.length)];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor blackColor]
range:NSMakeRange(0, value.length)];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor blueColor]
range:bluecolorRange];
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13.0] range:NSMakeRange(0, value.length)];
int i =0 ;
for (UIView *v in [[[segment subviews] objectAtIndex:0] subviews]) {
if ([v isKindOfClass:[UILabel class]]&& i== index) {
UILabel *label=(UILabel *)v ;
[label setAttributedText:attributedString];
i++;
}
}
}
NOTE : You have to modified some part of code as it's just suggestion to solve your problem

Related

iOS: Can I change color part of a Button Tittle?

I am new in iOS. I want to know how can I change the color for each part of UIButton title.
For example, a part of title in my UIButton is black and another the part of title in my UIButton is yellow
This is the describe image
Any help would be appreciated
U can use NSMutableAttributedString then add it to button with [button setAttributedTitle:attString forState:UIControlStateNormal];
To change color of a range in the Attribute String, read this, basically its just 1 of the attribute dictionary
Read this SO Question
I solve my question by using the code below.
For UILabel
NSMutableAttributedString *text =
[[NSMutableAttributedString alloc]
initWithAttributedString: self.lblFriendBand.attributedText];
[text addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(1, 2)];
[self.lblFriendBand setAttributedText: text];
For UIButton
NSMutableAttributedString *text2 =
[[NSMutableAttributedString alloc]
initWithAttributedString: self.btnFriendName.titleLabel.attributedText];
[text2 addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(1, 2)];
[self.btnFriendName setAttributedTitle:text2 forState:UIControlStateNormal];

What is the equivalent of Android's "Spannable" in Objective-C

Im on an iOS app that should able to highlight text, and make it clickable too.
I read about NSAttributedString in iOS but it still more complicated than Spannable in android.
Is there any other Objective c way to do that, if not; what should i do using NSAttributedString to highlight a paragraph word by word, and how to make my text clickable.
Update:
What exactly i want that each word should be clickable and can be
highlighted as a single word in one paragraph.
I found a perfect solution using UITextView, it will make every word in within the UITextView clickable.
Firstly, Create an UITextView and add an UITapGestureRecognizer to it as follows:
CGRect textViewFrame = CGRectMake(0, 40, 100, 100);
textView = [[UITextView alloc]initWithFrame: textViewFrame];
textView.textAlignment = NSTextAlignmentCenter;
textView.backgroundColor = [UIColor clearColor];
textView.editable = NO;
textView.selectable = NO;
[self.view addSubView:textView];
// i used to `NSMutableAttributedString` highlight the text
string = [[NSMutableAttributedString alloc]initWithString:#"Any text to detect A B $ & - +"];
[string addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:40.0]
range:NSMakeRange(0, [string length])];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init] ;
[paragraphStyle setAlignment:NSTextAlignmentCenter];
[string addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];
[textView setAttributedText:string];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapRecognized:)];
//modify this number to recognizer number of tap
[singleTap setNumberOfTapsRequired:1];
[textView addGestureRecognizer:singleTap];
Then add the UITapGestureRecognizer #selector:
- (void)tapRecognized:(UITapGestureRecognizer *)recognizer{
if(recognizer.state == UIGestureRecognizerStateRecognized)
{
CGPoint point = [recognizer locationInView:recognizer.view];
NSString * detectedText = [self getWordAtPosition:point inTextView: textView];
if (![detectedText isEqualToString:#""]) {
NSLog(#"detectedText == %#", detectedText);
} }
}
All this magic is related to this method, witch can detect any touch on the UITextView and get the tapped word:
-(NSString*)getWordAtPosition:(CGPoint)pos inTextView:(UITextView*)_tv
{
//eliminate scroll offset
pos.y += _tv.contentOffset.y;
//get location in text from textposition at point
UITextPosition *tapPos = [_tv closestPositionToPoint:pos];
//fetch the word at this position (or nil, if not available)
UITextRange * wr = [_tv.tokenizer rangeEnclosingPosition:tapPos withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight];
return [_tv textInRange:wr];
}
And for highlighting the text:
-(void)setTextHighlited :(NSString *)txt{
for (NSString *word in [textView.attributedText componentsSeparatedByString:#" "]) {
if ([word hasPrefix:txt]) {
NSRange range=[self.textLabel.text rangeOfString:word];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:range];
}}
[textView setAttributedText:string];
}
And thats it, hope this helps others.
Unfortunately the behavior that you're looking for isn't as simple in objective-c as Spannable.
However, it can be accomplished fairly easily:
Create a UILabel or UITextView.
Highlight some of the words using NSMutableAttributedString and NSRange (see below).
Apply the attributed string to the attributedText property.
Create UIButtons with clear backgrounds and no label and position over the top of the clickable words.
Add a target to each button and create a selector method.
Here's some example code for the attributed string to get you started:
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString: [NSString stringWithFormat:#"Text containing a bold word and another"];
[string addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"HelveticaNeue-Bold" size:14] range:NSMakeRange(18,4)];
[string addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"HelveticaNeue-Bold" size:14] range:NSMakeRange(32, 7)];
label.attributedText = string;

How can I set the color and alignment of attributed text in a UITextView in iOS 7?

The formatting of my textViews worked fine in iOS 6, but no longer in iOS 7. I understand with Text Kit much of the under the hood stuff has changed. It's become really quite confusing, and I'm hoping someone can help straighten it out a bit by helping me with something as simple as this.
My static UITextView originally was assigned a value for it's textColor and textAlignment properties. Then I made a NSMutableAttributedString, assigned it an attributes, then assigned it to the textView's attributedText property. The alignment and color no longer take effect in iOS 7.
How can I fix this? If these properties take no effect, than why do they exist anymore? Here's the creation of the textView:
UITextView *titleView = [[UITextView alloc]initWithFrame:CGRectMake(0, 90, 1024, 150)];
titleView.textAlignment = NSTextAlignmentCenter;
titleView.textColor = [UIColor whiteColor];
NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:#"Welcome"];
UIFont *font = [UIFont fontWithName:#"Avenir-Light" size:60];
[title addAttribute:NSParagraphStyleAttributeName value:font range:NSMakeRange(0, title.length)];
titleView.attributedText = title;
[self.view addSubview:titleView];
Curious, the properties are taken into account for UILabel but not for UITextView
Why don't you just add attributes for color and alignment to the attributed string similar to the way you are doing with the font?
Something like:
NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:#"Welcome"];
UIFont *font = [UIFont fontWithName:#"Avenir-Light" size:60];
[title addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, title.length)];
//add color
[title addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, title.length)];
//add alignment
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setAlignment:NSTextAlignmentCenter];
[title addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, title.length)];
titleView.attributedText = title;
Edit: Assign the text first, then change the properties and this way it works.
UITextView *titleView = [[UITextView alloc]initWithFrame:CGRectMake(0, 90, 1024, 150)];
//create attributed string and change font
NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:#"Welcome"];
UIFont *font = [UIFont fontWithName:#"Avenir-Light" size:60];
[title addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, title.length)];
//assign text first, then customize properties
titleView.attributedText = title;
titleView.textAlignment = NSTextAlignmentCenter;
titleView.textColor = [UIColor whiteColor];

NSAttributedString change color at end of string

This must be an easy thing to do, but I cannot figure it out.
I have a NSMutableAttributedString that has, for example, "This is a test" in it. I want to color the word "test" blue, which I do with this:
NSMutableAttributedString *coloredText = [[NSMutableAttributedString alloc] initWithString:#"This is a test"];
[coloredText addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(10,4)];
That works just fine. But now I want to set the text color back to black for anything typed after "test".
If I do:
[coloredText addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(coloredText.string.length, 1)];
I get an objectAtIndex:effectiveRange: out of bounds error. Assumedly because the range extends beyond the length of the string.
If I do:
[coloredText addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(coloredText.string.length, 0)];
The error goes away but typing after the word "test" remains blue.
How do I set the current color at the insertion point when it is at the end of the string??
Cheers for any input.
In case someone else stumbles upon this, I wanted to post the code I used to solve the problem. I ended up using Kamil's suggestion and adding:
NSAttributedString *selectedString = [textView.attributedText attributedSubstringFromRange:NSMakeRange(textView.attributedText.string.length - 1, 1)];
__block BOOL isBlue = NO;
[selectedString enumerateAttributesInRange:NSMakeRange(0, [selectedString length]) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop) {
isBlue = [[attributes objectForKey:NSForegroundColorAttributeName] isEqual:[UIColor blueColor]];
}];
if (isBlue) {
NSMutableAttributedString *coloredText = [[NSMutableAttributedString alloc] initWithAttributedString:textView.attributedText];
[coloredText addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(textView.attributedText.string.length - 1, 1)];
textView.attributedText = coloredText;
}
to the text changed handler.
You need to recalculate the attributes if the text changes, because their effective range doesn't automatically change with the length of text.

Can i set both current time and buffer limit on the same indicator

I need to show both current time (blue color) and buffer limit (gray color) on the same UIProgressView for my AVPlayer instance. Is it possible?
You need to use NSAttributedStrings to accomplish this.
Attributed Strings
Here is an example...
Make a custom UIView that contains a UILabel and UIProgressView as subviews.
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:#"Hello. That is a test attributed string."];
[str addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(3,5)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(10,7)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"HelveticaNeue-Bold" size:20.0] range:NSMakeRange(20, 10)];
label.attributedText = str;
And here is an example of a custom UIProgressView implementation

Resources