UITextView Text size is auto resized while using the NSAttributedString - ios

In the UITextView text,
before highlight the text, its shown the text size as 16. (in the first image)
after the using the NSAttributedString to highlight. highlighted text only show that same size(16) remaining strings show lesser than that the original size (second .
this problem only in the iPod and iPhone deceive . iPad is shown correct format.
code to highlight the UITextView Text
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIFont fontWithName:#"Arial" size:currentTextSize] forKey:NSFontAttributeName];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:txtview4disp.text];
int startind=[[dict objectForKey:#"BeginIndexPos"]integerValue];
int endind=[[dict objectForKey:#"EndIndexPos"]integerValue];
NSRange rang;
if (startind>=STARTINDEX&&startind<=ENDINDEX) {
if (endind>ENDINDEX) {
int endind2=endind-ENDINDEX;
rang=NSMakeRange(startind-STARTINDEX, (endind-startind)-endind2);
}
else
rang=NSMakeRange(startind-STARTINDEX, endind-startind);
[attrString addAttributes:attrsDictionary range:rang];
[attrString addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:rang];
txtview4disp.attributedText=attrString;

It looks like you skipped the original attributes of your UITextView.
Try replacing this line:
NSMutableAttributedString *attrString =
[[NSMutableAttributedString alloc] initWithString:txtview4disp.text];
with following:
NSMutableAttributedString *attrString =
[[NSMutableAttributedString alloc] initWithAttributedString:txtview4disp.attributedText];

Related

Only first NSParagraphStyle applied in NSAttributedString

Goal is to have a single NSAttributedString with a larger line height between paragraphs than within a paragraph, a fairly simple and common use case it seems to me. Here's my code:
NSMutableParagraphStyle *firstParagraphStyle = [[NSMutableParagraphStyle alloc] init];
firstParagraphStyle.lineHeightMultiple = 3.0;
NSMutableParagraphStyle *secondParagraphStyle = [[NSMutableParagraphStyle alloc] init];
secondParagraphStyle.lineHeightMultiple = 1.0;
NSAttributedString *title = [[NSAttributedString alloc] initWithString:#"Title"
attributes:#{NSParagraphStyleAttributeName: firstParagraphStyle}];
NSAttributedString *bodyTop = [[NSAttributedString alloc] initWithString:#"\u2029Body 1"
attributes:#{NSParagraphStyleAttributeName: secondParagraphStyle}];
NSAttributedString *bodyBottom = [[NSAttributedString alloc] initWithString:#"\u2029Body 2 line 1\u2028Body 2 line 2"
attributes:#{NSParagraphStyleAttributeName: secondParagraphStyle}];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
[attributedString appendAttributedString:title];
[attributedString appendAttributedString:bodyTop];
[attributedString appendAttributedString:bodyBottom];
All four lines end up with the same line spacing of 3.0. In fact, when I remove the attributes dictionary entirely and simply do:
NSAttributedString *title = [[NSAttributedString alloc] initWithString:#"Title"];
NSAttributedString *bodyTop = [[NSAttributedString alloc] initWithString:#"\u2029Body 1"];
NSAttributedString *bodyBottom = [[NSAttributedString alloc] initWithString:#"\u2029Body 2 line 1\u2028Body 2 line 2"];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
[attributedString appendAttributedString:title];
[attributedString appendAttributedString:bodyTop];
[attributedString appendAttributedString:bodyBottom];
[attributedString addAttribute:NSParagraphStyleAttributeName
value:firstParagraphStyle
range:NSMakeRange(0, 1)];
[attributedString addAttribute:NSParagraphStyleAttributeName
value:secondParagraphStyle
range:NSMakeRange(1, attributedString.length - 1)];
it still renders all three paragraphs using line height multiple of 3.0. It seems that whatever the first paragraph style I apply to the string is, that's the one that applies to all subsequent lines and paragraphs!
Why doesn't using the special paragraph separator character \u2029 as Apple suggests here allow for more than one paragraph style within a single NSAttributedString? I'd prefer not to break into multiple UILabels.
Thanks in advance to anyone with deep Core Text knowledge on this subject.
Ended up getting this working. Turns out when I set alignment on the subsequent UILabel to .textAlignment = NSTextAlignmentCenter that messed up the paragraph style for the entire attributedText.
So the lesson is: If you're using multiple paragraph styles, don't set any corresponding properties on the UILabel or your behavior will be clobbered by the first paragraph style the label sees, even when the properties aren't related (e.g. line height and text alignment).

Chinese characters interline spacing in UITextView

I have translated an app into Simplified Chinese.
The app is using a UITextView and the contents of it is Simplified Chinese text.
I have noticed that (sometimes not always) the interline spacing is wrong, the lines are practically touching each other :
I am setting the contents of the UITextview via an attributed string that I create based on a required pointsize like this:
-(NSMutableAttributedString*) textWithPointSize:(CGFloat)pointSize
{
UIFont * myFontDescr = [UIFont systemFontOfSize:pointSize];
NSMutableAttributedString * description = [[NSMutableAttributedString alloc] initWithString:self.exercise.descr attributes:#{NSFontAttributeName:myFontDescr}];
UIFont * myFontTips = [UIFont italicSystemFontOfSize:pointSize];
NSMutableAttributedString * tips = [[NSMutableAttributedString alloc] initWithString:self.exercise.tips attributes:#{NSFontAttributeName:myFontTips}];
NSMutableAttributedString * text = [[NSMutableAttributedString alloc] initWithAttributedString:description];
[text appendAttributedString:[[NSAttributedString alloc] initWithString:#"\n\n"]];
[text appendAttributedString:tips];
return text;
}
I am using the systemFontOfSize: function, I guess this should handle Chinese characters correctly? Any ideas why this could be happening?
I did not find out why but I implemented a work around by explicitly setting the interline spacing (only when language is Chinese) like this:
//for chinese language we have detected a problem with the interline spacing
//it does happen randomly, and so to workaround this we set the linespacing explicitly
if ( [NSLocalizedString(#"__CurrentLanguage",#"zh-Hans") isEqualToString:#"zh-Hans"] ) {
//make paragraph styl with interline spacing
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 0.25 * pointSize;
//font for description
UIFont * myFontDescr = [UIFont systemFontOfSize:pointSize];
NSMutableAttributedString * description = [[NSMutableAttributedString alloc] initWithString:self.exercise.descr attributes:#{NSFontAttributeName:myFontDescr, NSParagraphStyleAttributeName:paragraphStyle}];
//font for tips
UIFont * myFontTips = [UIFont italicSystemFontOfSize:pointSize];
NSMutableAttributedString * tips = [[NSMutableAttributedString alloc] initWithString:self.exercise.tips attributes:#{NSFontAttributeName:myFontTips,NSParagraphStyleAttributeName:paragraphStyle}];
//add description then...
NSMutableAttributedString * text = [[NSMutableAttributedString alloc] initWithAttributedString:description];
//concat the tips (with 2x newline in between)
[text appendAttributedString:[[NSAttributedString alloc] initWithString:#"\n\n"]];
[text appendAttributedString:tips];
//return value
return text;
}
//its not chinese, so do "normal" stuff

WatchKit, AttributedString formatting not working

I am building my first WatchKit App and am having troubles with NSAttributedString formatting as it does not seem to work as I'd expect ;)
This is my code:
UIFont *textFont = [UIFont fontWithName:#"Menlo" size:30];
UIFont *hlFont = [UIFont fontWithName:#"Menlo-Bold" size:30];
NSMutableAttributedString *information = [[NSMutableAttributedString alloc]initWithString:#"ADDED AN ENTRY OF " attributes:#{NSFontAttributeName : textFont}];
NSString *amountString = [NSString stringWithFormat:#"%.2f",(-1)*handler.amount.floatValue];
NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle];
NSAttributedString *amount = [[NSAttributedString alloc]initWithString:amountString attributes:#{NSFontAttributeName : hlFont, NSUnderlineStyleAttributeName : underline }];
NSAttributedString *to = [[NSMutableAttributedString alloc]initWithString:#" TO " attributes:#{NSFontAttributeName : textFont}];
NSString *categoryString = handler.category;
NSAttributedString *category = [[NSAttributedString alloc]initWithString:categoryString attributes:#{NSFontAttributeName : hlFont, NSUnderlineStyleAttributeName : underline }];
[information appendAttributedString:amount];
[information appendAttributedString:to];
[information appendAttributedString:category];
[_informationLabel setAttributedText:information];
and this the result:
Expectation
10.00 and Stuff should be underlined and in boldface.
Is there something fundamentally different to how attributed strings work on the watch than on iOS? What am I missing?
read through this: https://developer.apple.com/library/ios/documentation/General/Conceptual/WatchKitProgrammingGuide/TextandLabels.html
Solved it, the problem were the fonts #"Menlo".
By using
UIFont *textFont = [UIFont systemFontOfSize:20];
UIFont *hlFont = [UIFont systemFontOfSize:20];
the formatting with underlines works fine.
I don't believe you can programmatically set a label to be bold, italic, underlined....this has to be done through the actual interface in the storyboard.
According to this link
The attributes you can configure are
text
text color
font
min scale
alignment
lines
a potential workaround is to incorporate multiple labels, and set the ones you need to the right format (bold, underlined)

Setting attributedText of UILabel causing issue with Lengthier content

In my project I want to add an attributed text in UILabel placed on the xib.
It's working perfectly, but if large text appears it shows some issues.
My current implementation:
- (void)viewDidLoad
{
[super viewDidLoad];
_demoLabel.numberOfLines = 0;
_demoLabel.lineBreakMode = NSLineBreakByWordWrapping;
_demoLabel.attributedText = [self demoNameWithFontSize:21 andColor:[UIColor redColor]];
}
- (NSMutableAttributedString *)demoNameWithFontSize:(CGFloat)fontSize andColor:(UIColor *)color
{
NSMutableAttributedString *attributedText = nil;
NSString *demoName = #"Blah blah blah";
UIFont *demoFont = [UIFont fontWithName:#"Zapfino" size:fontSize];
attributedText = [[NSMutableAttributedString alloc] initWithString:demoName];
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.lineBreakMode = NSLineBreakByWordWrapping;
[attributedText addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, [demoName length])];
[attributedText addAttribute:NSFontAttributeName value:demoFont range:NSMakeRange(0, [demoName length])];
[attributedText addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, [demoName length])];
return attributedText;
}
Output:
Issue:
It is not displaying the whole text, even if I applied the NSMutableParagraphStyle.
How can I solve this ?
Alternative I found:
If I change
UIFont *demoFont = [UIFont fontWithName:#"Zapfino" size:fontSize];
to
UIFont *demoFont = [UIFont systemFontOfSize:fontSize];
It'll work and gives output like:
But the issue is I need to use custom font, can't use default font. Also cannot change the font size.
I checked UILabel class reference and googled, but couldn't find a solution. Please help me.
Is there anyway to span this text into multiple lines ?
You need to resize the UILabel to fit the text.
You can calculate the size with the boundingRectWithSize:options:context: NSAttributedString class method, which takes an attributed string and calculates the size within a set rect based on all the attributes of the string.

Set NSAttributedString's background color in custom view's drawRect function

I want to draw text with background color in my custom view, but I found that if
I add NSBackgroundColorAttributeName attribute to my NSAttributedString , it doesn't appear.
when I remove background color attribute, it draws normaly :
-(void) drawRect : (CGRect)rect
{
NSAttributedString *test = [[NSAttributedString alloc]initWithString:#"test"
attributes: #{NSForegroundColorAttributedName:[UIColor whiteColor],
NSBackgroundColorAttributeName:[UIColor blackColor]}];
[test drawAtPoint:CGPointMake(0,0)];
}
But if I add a label on my custom view, and modify its attributed text, It will work:
NSMutableAttributedString *attributedText = [self.testLabel.attributedText mutableCopy];
NSString *str = [attributedText string];
NSRange r = [str rangeOfString:str];
[attributedText addAttributes: #{NSForegroundColorAttributedName:[UIColor whiteColor],
NSBackgroundColorAttributeName:[UIColor blackColor]} range:r];
self.testLabel.attributedText = arrtibutedText;
why is that ?

Resources