Multiple text alignment with same label using NSMutableAttributedString on iOS - 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,};

Related

How to arrange NSString word in this type format without adding space in UILabel

I want to make this type format to word arrange in NSString on UILabel like
but I try to make add extra white space according to upper line word length but another way to make this type format suppose using NSAttributesString.
You can do this using NSMutableAttributedString without adding extra white space.
First, create a method that returns NSMutableAttributedString like this-
-(NSMutableAttributedString*)setIndent:(NSString*) title value:(CGFloat) value {
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.alignment = NSTextAlignmentLeft;
style.firstLineHeadIndent = value;
NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithString:title attributes:#{ NSParagraphStyleAttributeName : style}];
return attrText;
}
and use this method by the following way -
NSString *title = #"Charlie Chapline Cartoon";
NSArray* foo = [title componentsSeparatedByString: #" "];
NSMutableAttributedString *attrText5 = [[NSMutableAttributedString alloc] init];
CGFloat value = 0.0f;
for(int i = 0; i< foo.count; i++){
//change this value according to your need.
value = value + 20.0f;
[attrText5 appendAttributedString:[self setIndent:[NSString stringWithFormat:#"%#\n", foo[i]] value:value]];
}
_myLab.numberOfLines = 5;
_myLab.attributedText = attrText5;
Output:

How to adjust NSLineBreakByTruncatingTail to a portion of NSAttributedString?

I want to adjust NSLineBreakByTruncatingTail to a portion of NSAttributedString.
For example,
The result is below.
"abcde...LAST"
The 'abcde...' is a NSAttributedString with the properties different with that of 'LAST'.
Actually, I can do if 'abcde...' string object and 'LAST' string object are diffrent.
But, I want to make to same string object.
sample code is below.
NSMutableParagraphStyle *style1 = [[NSMutableParagraphStyle alloc] init];
NSMutableParagraphStyle *style2 = [[NSMutableParagraphStyle alloc] init];
[style2 setLineBreakMode:NSLineBreakByTruncatingTail];
NSMutableAttributedString *attrString1 = [[NSMutableAttributedString alloc] initWithString:#"abcdefghijklm"];
[attrString1 addAttribute:NSForegroundColorAttributeName:[UIColor whiteColor]];
NSMutableAttributedString *attrString2 = [[NSMutableAttributedString alloc] initWithString:#"LAST"];
[attrString2 addAttribute:NSForegroundColorAttributeName:[UIColor blueColor]];
[attrString1 appendAttributedString:attrString2];
Then, I expected like the third line.
But the result is like this.
"abcdefghij..."

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.

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;

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