NSAttributedString not changing UILabel - ios

Hi I am trying to indent all but the first lines in a bullet point list. to do thins I am using an attributed string with a NSParagraphStyle. However it does not change the label at all. Any help?
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.HeadIndent = 50;
NSDictionary *styles=[NSDictionary dictionaryWithObject:style forKey:NSParagraphStyleAttributeName] ;
NSAttributedString* attributedText= [[ NSAttributedString alloc]initWithString:tipsString attributes:styles];
[coachingTips setAttributedText:attributedText];
Coaching tips is the UILabel.
Thanks,
Josh

You have to get the string value from the attributed string.
try following..
NSDictionary *styles=[NSDictionary dictionaryWithObject:style forKey:NSParagraphStyleAttributeName] ;
NSAttributedString* attributedText= [[ NSAttributedString alloc]initWithString:tipsString attributes:styles];
[coachingTips setAttributedText:[attributedText string];

Related

lineBreakMode for multiline attributed string

I have a multiline label that will contain an attributed string. This is the code of the attributed string:
NSString * titleString = #"Title";
NSString * informationString = self.myObject.content;
NSString * allString = [NSString stringWithFormat:#"%#\n\n%#",titleString,informationString];
NSMutableAttributedString* attributedMessage = [[NSMutableAttributedString alloc]initWithString:allString];
NSMutableParagraphStyle* style=[[NSMutableParagraphStyle alloc]init];
style.alignment = NSTextAlignmentLeft;
NSDictionary *attributesTitle = [NSDictionary dictionaryWithObjectsAndKeys:style,NSParagraphStyleAttributeName,[UIColor blackColor],NSForegroundColorAttributeName,[UIColor clearColor],NSBackgroundColorAttributeName,Font(#"OpenSans-Semibold", 17+(int)((self.view.bounds.size.width-290.)/15.)),NSFontAttributeName,nil];
NSMutableParagraphStyle* styleText=[[NSMutableParagraphStyle alloc]init];
styleText.alignment = NSTextAlignmentLeft;
styleText.lineBreakMode = NSLineBreakByTruncatingTail;
NSDictionary *attributesInformation = [NSDictionary dictionaryWithObjectsAndKeys:styleText,NSParagraphStyleAttributeName,[UIColor colorWithRed:91./255 green:91./255 blue:91./255 alpha:1.],NSForegroundColorAttributeName,[UIColor clearColor],NSBackgroundColorAttributeName,[UIFont fontWithName:#"Roboto-Light" size:13.+(int)((self.view.bounds.size.width-290.)/15.)],NSFontAttributeName,nil];
[attributedMessage setAttributes:attributesTitle range:[allString rangeOfString:titleString]];
[attributedMessage setAttributes:attributesInformation range:[allString rangeOfString:informationString]];
In my case the label is shown with ellipse "..." in all the lines. how can i resolve this issue. I want that will be shown only in the last visible line.
The solution that i adopted is
while (newHeight>=heightOfLabel){
remove some letter from the end of the string
calculate new height
}
replace the last three charachter with "..."
and it is done :)
I know that it is not a clean solution but it resolve my issue until finding a clean one.

Multiple text alignment with same label using NSMutableAttributedString on iOS

I want to set Text direction for my label using NSMutableAttributedString. for example, I have chat view, which contains message and time. I want to set left alignment for message and right alignment for time using UILabel.
I have used following code, but it's not working,
NSString *Time = [Functions stringFromGivenDate:msg.time withFormate:#"hh:mm a"];
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#\n%#",msg.text,Time]];
NSDictionary *attrDictionary = #{NSWritingDirectionAttributeName:#[#(NSTextWritingDirectionOverride)]};
[str addAttributes:attrDictionary range:NSMakeRange(msg.text.length+1, Time.length)];
if I understood correctly, this should help:
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.alignment = alignment;// type NSTextAlignment
NSDictionary *attributtes = #{NSParagraphStyleAttributeName : style,};

Justified text with UITextView and NSMutableAttributedString

I'm trying to put a justified text for a UITextView with NSMutableAttributedString, the NSMutableAttributedString is composed of different NSAttributedString because I need bold and regular font, so I append different NSString, this is my NSMutableAttributedString:
NSAttributedString *one = [[NSAttributedString alloc] initWithString:#"abc"
attributes:boldDict];
NSAttributedString *two = [[NSAttributedString alloc] initWithString:#" def"
attributes:regularDict];
NSAttributedString *three = [[NSAttributedString alloc] initWithString:#" ghi"
attributes:boldDict];
NSMutableAttributedString *string =
[[NSMutableAttributedString alloc] initWithAttributedString:one];
[string appendAttributedString:two];
[string appendAttributedString:three];
I have tried this:
[self.text_view setTextAlignment:NSTextAlignmentJustified]
and this:
NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
paragraphStyles.alignment = NSTextAlignmentJustified;
Dictionary *attributes = #{NSParagraphStyleAttributeName: paragraphStyles};
and apply this to NSMutableAttributedString, but neither works. how i can do?
I fixed the same problem by specifying transparent background color for the NSAttributedString.
Looks like a bug in the UILabel code which should render simple NSAttributedString like NSString.
Xamarin.iOS example:
var paragraphStyle = new NSMutableParagraphStyle();
paragraphStyle.Alignment = UITextAlignment.Justified;
var attributedText = new NSAttributedString(simpleString,
paragraphStyle: paragraphStyle,
backgroundColor: Color.Transparent.ToUIColor());
myLabel.AttributedText = attributedText;

Nsattributedstring with different NSMutableParagraphStyle

I have 1 attributed string with multiple paragraph.
I had given the FirstLineHeadIndent = 2.12.
Now I want to give the FirstLineHeadIndent=0 to 1st paragraph
and FirstLineHeadIndent=2 to 2nd paragraph in the attributed string.
If I set the property like
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing =Line_space;
paragraphStyle.firstLineHeadIndent =2;
paragraphStyle.headIndent =margin_space;
paragraphStyle.tailIndent =-margin_space;
paragraphStyle.paragraphSpacing=paragraph_space;
NSDictionary *ats = #{
NSFontAttributeName : [UIFont fontWithName:self.bookView.defaultFontFamily size:self.bookView.defaultFontSize],
NSParagraphStyleAttributeName : paragraphStyle,
};
It will give the head space to both the paragaraph.
Please help me.
I am uploading the image for more help:-
So, the solution is to use 2 paragraph styles.
Your NSString is separated by a \n, that indicate the separations between the 2 paragraphs.
NSMutableParagraphStyle *firstParagraphStyle = [[NSMutableParagraphStyle alloc] init];
[firstParagraphStyle setFirstLineHeadIndent:0];
//Do the rest of the settings
NSMutableParagraphStyle *secondParagraphStyle = [[NSMutableParagraphStyle alloc] init];
[secondParagraphStyle setFirstLineHeadIndent:2];
//Do the rest of the settings
//You may use the same paragraphStyle, changing just the firstLineHeadIndent, and set the attributes, but for a clearer explanation, I used 2 paragraph styles
NSRange range = [[yourAttributedString string] rangeOfString:#"\n"];
[yourAttributedString addAttribute:NSParagraphStyleAttributeName value: firstParagraphStyle range:NSMakeRange(0, range.location)]; //The range is from the start to the \n
[yourAttributedString addAttribute:NSParagraphStyleAttributeName value: secondParagraphStyle range:NSMakeRange(range.location, [[yourAttributedString string] length]-range.location)]; //The range is from the start of \n to the end

Right to left UILabels

I need to display a text using a UILabel ( can't use UIWebView ), and it sometimes contains both Hebrew and English. using UILabel's default settings the sentence gets mixed up and doesn't make sense. I have failed to found a way to make UILabel display text rtl.
Does anybody know anyway to do that, or a code that implements this?
Have a look at this SO, it contains some info on this subject that might help you out.
It seems to work for some by adding the code \u200F to the strings to be displayed.
NSString *RTFstr = "1. בבוקר"; //This could be any right-to-left string
NSString *directionalString = [#"\u200F" stringByAppendingString:[note text]];
[someUITextView setString:directionalString];
This will work
-(void)fnForWritingDirection:(UILabel*)label textFor:(NSString *)stringForText{
NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString: [stringForText stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setBaseWritingDirection:NSWritingDirectionRightToLeft];
[attrStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attrStr length])];
label.attributedText=attrStr;
}

Resources