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;
}
Related
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];
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,};
I'm doing something wrong with the range (I think) in setting this NSMutableAttributedString. Can anyone tell me why this is crashing my program? It looks right to me, but I'm obviously wrong! Thanks.
NSString *placeHolderString = #"USERNAME";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
float spacing = 5.0f;
// crashes on this line
[attributedString addAttribute:NSKernAttributeName
value:#(spacing)
range:NSMakeRange(0, [placeHolderString length])];
self.userNameTextField.attributedPlaceholder = attributedString;
What I think was causing your issue is that you were never really accounting for your placeholderString in the first place. In addition, your value parameter could simply use numberWithFloat as the application would then known what type you are using all the time.
Once you account for the placeHolderString, you are then going to use the length for the attributeString, as it now just contains the contents of your placeholderString. Then we just simply copy that string which contains your attribute using the UITextField property attributedText.
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:placeHolderString];
float spacing = 5.0f;
[attributeString addAttribute:NSKernAttributeName
value:[NSNumber numberWithFloat:spacing]
range:(NSRange){0,[attributeString length]}];
userNameTextField.attributedText = [attributeString copy];
For more context, attributes like NSUnderlineStyleAttributeName exist and you can do some really powerful things with NSMutableAttributedString. Refer to the documentation for options.
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])];
I have a UITextView which contains an attributed string configured through storyboard. Now i want a custom font to be applied. I am doing it through code
textView.font = [UIFont fontWithName:kCustomFont size:textView.font.pointSize];
The problem is fonts get changed losing all the attributes. How do i fix it?
(Working on iOS 5 and above)
What's in your kCustomFont? If the FontName is wrong, even a single Character, the standard SystemFont will be used.
You should change it via NSMutableAttributedString's attributes.
I.e. use something like:
NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc] initWithAttributedString:textView.text];
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
[mutableSrting addAttributes:attrs range:NSMakeRange(0, [mutableString length])];
textView.attributedText = mutableString;
[mutableString release];