UILabel text alignment when English and Hebrew mixed as text? - ios

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])];

Related

How to change properties of particular text in a label

I have a label and i set the text of the label programmatically. I want to set one of the word to be bold and the rest normal. However, i am unable to control the properties of the text. For example, I want this "This is an example" but am only able to achieve this "This is an example".
Try this:
NSString *text = #"This is an example";
NSString *textBold = #"example";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
[attributedString beginEditing];
[attributedString addAttribute:NSFontAttributeName
value:[UIFont boldSystemFontOfSize:20.0f]
range:[text rangeOfString:textBold]];
[attributedString endEditing];
[labelObj setAttributedText:attributedString];
Take a look at the attributedText property of the label. It lets you assign styled text using an NSAttributedString. Explaining how to build an NSAttributedString is beyond the scope of an SO answer, but you should be able to find ample information both in the Xcode help system and online.
Let me show you a demo about the attributedText.
NSDictionary*subStrAttribute1 = #{
NSForegroundColorAttributeName: [UIColor redColor],
NSStrikethroughStyleAttributeName:#2
};
NSDictionary *subStrAttribute2 =#{
NSForegroundColorAttributeName: [UIColor greenColor]
};
NSString *strDisplayText3 =#"Red and Green";
NSMutableAttributedString *attributedText3 = [[NSMutableAttributedString alloc] initWithString:strDisplayText3];
[attributedText3 setAttributes:subStrAttribute1 range:NSMakeRange(0,3)];
[attributedText3 setAttributes:subStrAttribute2 range:NSMakeRange(8,5)];
self.lblInfo3.attributedText= attributedText3;
Since ios6 uilabel supports attributed strings, So you can use it.
For your particular case below code will work-
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:#"This is an example"];
[string addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"HelveticaNeue-Bold" size:20.0] range:NSMakeRange(11, 7)];
label.attributedText = string;

Is there an effective way to load a lot of text in a UITextView?

UITextView is causing my App to slow down because it holds a lot of text. I was wondering if there is some kind of third party library that can load more text effectively?
I am using attributedText to hold all of the text. If there is a better way please let me know. Any tips or suggestions are appreciated.
textView.attributedText = attributeStr;
Really? It worked fine on me. Please check this out on how I implemented my UITextView.The HELP_STATIC_CONTENT there is a constant variable that holds static texts for my Help screen which contains lot of texts (about 500 lines).
UITextView *lblHelp = [[UITextView alloc]initWithFrame:CGRectMake(10, 10, self.view.frame.size.width - 20, self.view.frame.size.height - 20)];
[lblHelp setBackgroundColor:[UIColor clearColor]];
[lblHelp setEditable:NO];
[lblHelp setSelectable:NO];
lblHelp.showsVerticalScrollIndicator=NO;
lblHelp.showsHorizontalScrollIndicator=NO;
[self.view addSubview:lblHelp];
NSString *string = HELP_STATIC_CONTENT;
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.alignment=NSTextAlignmentJustified;
[paragraphStyle setLineSpacing:3];
[attrString beginEditing];
[attrString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, string.length)];
[attrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];
[attrString endEditing];
lblHelp.attributedText = attrString;
If you want to change only a part of the text, you can directly change it with the textStorage property e.g:
[textView.textStorage addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:range];

Disabling UITextView Kerning

I'm trying to disable any kind of kerning in UITextView
Somehow in arabic letters the text view kern the letters to fit the line but it just ruin the whole word, here's an examples:
textview has white background, and the code is:
NSString *aya =[mainArray objectAtIndex:indexPath.row];
cell.tx.text = aya;
[cell.tx sizeToFit];
cell.tx.frame = CGRectMake(5, 5, 265, cell.tx.frame.size.height);
cell.tx.backgroundColor = [UIColor whiteColor];
also tried to set NSMutableAttributedString NSKernAttributeName value but didn't work,,
---------------------Edit: This is the code for Kern Attribute:
NSMutableAttributedString *attributedString;
attributedString = [[NSMutableAttributedString alloc] initWithString:aya];
[attributedString addAttribute:NSKernAttributeName
value:#0.0
range:NSMakeRange(0,[aya length])];
[attributedString addAttribute:NSFontAttributeName
value:font
range:NSMakeRange(0,[aya length])];
NSMutableParagraphStyle *paragrapStyle = [NSMutableParagraphStyle new];
paragrapStyle.alignment = NSTextAlignmentRight;
[attributedString addAttribute:NSParagraphStyleAttributeName
value:paragrapStyle
range:NSMakeRange(0,[aya length])];
[cell.tx setAttributedText:attributedString];
It's a hack and I'm not near a computer to try it, but give this a shot:
Set the tx's text property
[tx setText:aya];
Create an NSMutableAttributedString from the text view's text
NSMutableAttributedString *attrStr = [[tx attributedText] mutableCopy];
Set the kern attribute on the attributed string
[attrStr setAttributes:#{ NSKernAttributeName: #(0.f) }
range:NSMakeRange(0, [attrStr length])];
Set the attrStr back on tx
[tx setAttributedText:attrStr];
This will (hopefully) preserve the attributes UITextView infers from your NSString.

Why does this NSMutableAttributedString addAttribute work only if I use a mutableCopy

I have the following code :
NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString:#"• Get Tested Son"];
NSMutableAttributedString *boldS = [[NSMutableAttributedString alloc] initWithString:#"Son"];
[boldS addAttribute:NSFontAttributeName value:SOMEBOLDFONT range:NSMakeRange(0, boldS.length)];
[attrS replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
withAttributedString:boldS];
As you can see, I want to bold the Son part. This does not work if I do the above statements but only works if I do :
[[attrS mutableCopy] replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
withAttributedString:boldS];
What might be the reason for that?
addAttribute works regardless of whether you take a mutableCopy. Your question is based on a false assumption. It therefore has no answer.
Run this:
NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString:#"• Get Tested Son"];
NSMutableAttributedString *boldS = [[NSMutableAttributedString alloc] initWithString:#"Son"];
UIFont *someBoldFont = [UIFont fontWithName:#"Arial" size:23.0f];
[boldS addAttribute:NSFontAttributeName value:someBoldFont range:NSMakeRange(0, boldS.length)];
NSMutableAttributedString *attrSCopy = [attrS mutableCopy];
[attrS replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
withAttributedString:boldS];
[attrSCopy replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
withAttributedString:boldS];
NSLog(#"%#", [attrS isEqual:attrSCopy] ? #"equal" : #"different");
It will output equal. Comment out the replaceCharactersInRange: for either attrS or attrSCopy and it will output different.

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