Problem with text alignment in NSAttributedString

Hi all,

I have problems with text alignment.

I like to right align some text on an image.


My code looks like this:

// my text string

NSString* myString = @"Some text";

// create a dictionary

NSMutableDictionary *userAttributes = [[NSMutableDictionary alloc] init];

// fill my dictionary with text properties

[userAttributes setObject:[NSFont fontWithName:mySelectedFont size:myFontSize] forKey:NSFontAttributeName];

[userAttributes setObject:myFontColor forKey:NSForegroundColorAttributeName];

// create a paragraph style to define my text alignment

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

paragraphStyle.alignment = NSTextAlignmentRight;

// create an NSAttributedString and add text properties to it

NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:myString attributes:userAttributes];

// draw the "attributed string" at a given point

[attrString drawAtPoint:NSMakePoint(myXTLval,myYTLval)];


The text is placed exactly where I want it,

the font is applied

except the alignment, this is not working.

What am I doing wrong here?


Can someone help me further?

Try adding the paragrapgh style

https://developer.apple.com/documentation/uikit/nsparagraphstyleattributename?language=objc



to the attributes:

[userAttributes setObject:pragraphStyle forKey:NSParagraphStyleAttributeName];

You added 2 userAttributes, (they work)

[userAttributes setObject:[NSFont fontWithName:mySelectedFont size:myFontSize] forKey:NSFontAttributeName];
[userAttributes setObject:myFontColor forKey:NSForegroundColorAttributeName];

But

you nowhere add the paragraphStyle to userAttributes

No surprise you don't get them applied.


The line proposed by PBK will do it.

Sorry I forgot a line of code in my explanation:

As PBK mentionned too.


// create a paragraph style to define my text alignment

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

paragraphStyle.alignment = NSTextAlignmentRight;


[userAttributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];


// create an NSAttributedString and add text properties to it

NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:myString attributes:userAttributes];

// draw the "attributed string" at a given point

[attrString drawAtPoint:NSMakePoint(myXTLval,myYTLval)];


Thank you both for feedback.

But this is how it still not work.

Try this:


NSAttributedString *attString = ????
CGFloat stringWidth = [attString size].width;
CGFloat stringY = borderSize.height - 48;
CGFloat stringX = borderSize.width - [thePrintInfo rightMargin] - stringWidth;
[attString drawAtPoint:NSMakePoint(stringX, stringY)];


I use this in a print routine to draw a footer on the bottom right of the page. You could adapt it for your needs.

Since I am calculating from the right side of the page, I get the right margin X value and then subtract the width of the string, stringwidth, to get the starting position of X to draw. In this case, borderSize.width is the overall width, then subtract the width of the margin and the width of the attString. Setting the alignment doesn't have any effect when drawing with the drawAtPoint--yes, your string is right aligned but it is a string of only the length needed for the text so it really doesn't do anything.

Thank you janabanana,

but I have to deal also with multiline strings, so this is not a real solution for me.

As I said, "Setting the alignment doesn't have any effect when drawing with the drawAtPoint--yes, your string is right aligned but it is a string of only the length needed for the text so it really doesn't do anything."

Your code is drawing the text right where you would expect it to draw--at the X. If you have multiple lines, then either draw each line with the code I suggested or think about another approach--maybe putting the text into a textField or textView with the text right aligned and then placing the field/textView at the location you desire (set the background color of the field/textView to clearColor so it doesn't appear as a box on top of your image--only the text will show. I would go with the first approach and draw each line separately.

In fact you just right justify, you define nowere that you align with any image.


BTW, we don't see any reference to an image in your code.


You don't tell where this code is.

But did you consider doing it in IB ?

Thank you janabanana I will try your suggestions.

Problem with text alignment in NSAttributedString
 
 
Q