Nsattributedstring with different NSMutableParagraphStyle - ios

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

Related

UILabel display emojis

I have a UILabel which is displaying emojis incorrectly.
Here is a screenshot from iOS app:
And here is a screenshot from Android app which is displaying the same text with the emoji correctly.
I have tried answers from here but they did not help.
Example string : "تم البيع والله يبارك للمشتري♥️"
Here is the code:
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:comment.body];
UIFont *cellFont = [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline];
NSDictionary *attributesDictionary;
NSMutableParagraphStyle *paragraphStyle =
[[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 10;
paragraphStyle.alignment = NSTextAlignmentRight;
paragraphStyle.allowsDefaultTighteningForTruncation = true;
attributesDictionary = #{
NSParagraphStyleAttributeName : paragraphStyle,
NSFontAttributeName : cellFont,
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: #(NSUTF8StringEncoding)
};
[str addAttributes:attributesDictionary
range:NSMakeRange(0, str.length)];
cell.commentTextLabel.attributedText = str;
Any help would be appreciated.
Use the emoji's unicode.
in your case, the unicode for the red heart is: U+2764 U+FE0F .
Objective C code :
cell.commentTextLabel.text = #"تم البيع والله يبارك للمشتري \U00002764 \U0000FE0F";
Swift code:
cell.commentTextLabel.text = "تم البيع والله يبارك للمشتري \u{2764} \u{FE0F}"
For more emojis, In Xcode go Edit -> Emoji & Symbols, choose an emoji then right click on it and click Copy Character Info Button. Paste it and you will get for the red heart:
❤️
red heart
Unicode: U+2764 U+FE0F, UTF-8: E2 9D A4 EF B8 8F
I was able to solve this issue by using the following code:
NSData *data = [comment.body dataUsingEncoding:NSUTF16StringEncoding allowLossyConversion:false];
NSMutableAttributedString *str = [[NSMutableAttributedString alloc ] initWithData:data options:#{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
UIFont *cellFont = [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline];
NSDictionary *attributesDictionary;
NSMutableParagraphStyle *paragraphStyle =
[[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 10;
paragraphStyle.alignment = NSTextAlignmentRight;
attributesDictionary = #{
NSParagraphStyleAttributeName : paragraphStyle,
NSFontAttributeName : cellFont
};
[str addAttributes:attributesDictionary range:NSMakeRange(0, str.length)];
cell.commentTextLabel.attributedText = str;
In here comment.body is the string fetched from server which contains the emoji.
The result was like this:
Thanks to Larme for his help in the comments.

Have multiple ranges with different attributes on NSAttributedString

I have a multiline UILabel of which I would like to increase the line height, but I also want part of it to be a different color, only the line height works fine. But as soon as I try to change the color for a certain range it just goes back to the stock appearance, no line either..
Anyone a tip? This is done in the content setter.
- (void)setContent:(NSString *)content {
_content = content;
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self.content];
NSMutableAttributedString *mutableAttrString = [attributedString mutableCopy];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setLineSpacing: 5.0f];
NSDictionary *attributes = #{
NSFontAttributeName: [UIFont fontWithName:#"BentonSans-Regular" size:16.0],
NSParagraphStyleAttributeName: paragraphStyle
};
NSDictionary *colorAttributes = #{
NSForegroundColorAttributeName: [UIColor redColor]
};
[mutableAttrString addAttributes:attributes range:NSRangeFromString(self.content)];
[mutableAttrString addAttributes:colorAttributes range:NSMakeRange(4, 8)];
[self.label setAttributedText: mutableAttrString];
}
Thanks!
The NSRangeFromString function expects a string like #"{3,10}". In other words, it expects a string that contains two numbers that specify the starting location and length of the range. I suspect that the content string isn't a string like that.
So this line
[mutableAttrString addAttributes:attributes range:NSRangeFromString(self.content)];
should be
[mutableAttrString addAttributes:attributes range:NSMakeRange(0,mutableAttrString.length)];
in your viewdidLoad Method assign string to self.content :
self.content = #"pass your text ";
// Remove the First line of your method it is not needed
- (void)setContent:(NSString *)content {
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self.content];
NSMutableAttributedString *mutableAttrString = [attributedString mutableCopy];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setLineSpacing: 5.0f];
NSDictionary *attributes = #{
NSFontAttributeName: [UIFont fontWithName:#"BentonSans-Regular" size:16.0],
NSParagraphStyleAttributeName: paragraphStyle
};
NSDictionary *colorAttributes = #{
NSForegroundColorAttributeName: [UIColor redColor]
};
[mutableAttrString addAttributes:attributes range:NSRangeFromString(self.content)];
[mutableAttrString addAttributes:colorAttributes range:NSMakeRange(4, 8)];
[self.label setAttributedText: mutableAttrString];
}

Intrinsic content size of UILabel with NSAttributedString and indentation

Why does setting an indent on the paragraph style attribute not have an affect on a UILabel using NSAttributedString?
For example:
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.firstLineHeadIndent = 50;
NSDictionary *attributes = #{
NSForegroundColorAttributeName : [UIColor redColor],
NSParagraphStyleAttributeName : paragraphStyle,
};
NSString *string = #"some string";
self.label.attributedText = [[NSAttributedString alloc] initWithString:string
attributes:attributes];
UILabel with indentation (green background set to see the label size):
UILabel without indentation (green background set to see the label size):

UILabel text alignment when English and Hebrew mixed as text?

I have UILabel which having a text which is mixed of English and Hebrew. I wanted to alignment as per language means for English LTL and for Hebrew RTL.
Text is
I bless You, God, for creating the fruit of the vine:
בָּרוּךְ אַתָּה יְיָ אֱלֹהֵֽינוּ מֶֽלֶךְ הָעוֹלָם, בּוֹרֵא פְּרִי
הַגָּֽפֶן.
Here is a sample you could be inspired with.
Note: I don't know how you create your all text, so I don't know how you'll know what's in English or what in Hebrew, but you'll get the idea.
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:#"I bless You, God, for creating the fruit of the vine:\n בָּרוּךְ אַתָּה יְיָ אֱלֹהֵֽינוּ מֶֽלֶךְ הָעוֹלָם, בּוֹרֵא פְּרִי הַגָּֽפֶן."];
NSMutableParagraphStyle *englishParagraphStyle = [[NSMutableParagraphStyle alloc] init];
[englishParagraphStyle setAlignment:NSTextAlignmentLeft];
NSMutableParagraphStyle *hebrewParagraphStyle = [[NSMutableParagraphStyle alloc] init];
[hebrewParagraphStyle setAlignment:NSTextAlignmentRight];
NSRange englishRange = NSMakeRange(0, [#"I bless You, God, for creating the fruit of the vine:" length]);
NSRange hebrewRange = NSMakeRange([#"I bless You, God, for creating the fruit of the vine:" length],
[[attrStr string] length] - [#"I bless You, God, for creating the fruit of the vine:" length]);
[attrStr addAttribute:NSParagraphStyleAttributeName
value:englishParagraphStyle
range:englishRange];
[attrStr addAttribute:NSParagraphStyleAttributeName
value:hebrewParagraphStyle
range:hebrewRange];
[myLabel setAttributedText:attrStr];
You also may want to do this before setting it to your UILabel:
[attrStr addAttribute:NSFontAttributeName
value:[UIFont whateverFontWithWhateverSize]
range:NSMakeRange(0, [attrStr length])];

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;

Resources