2

I create an NSMutableAttributedString with alignment as follows:

UIFont * font =  [UIFont systemFontOfSize:[UIFont labelFontSize]];
/// Make a copy of the default paragraph style
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
/// Set line break mode
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
/// Set text alignment
paragraphStyle.alignment = alignmentForString(textString) ;//NSTextAlignmentLeft;//NSTextAlignmentRight;// NSTextAlignmentNatural;//  NSTextAlignmentLeft;

NSDictionary *messageDrawRectAttributesDictionary = @{
                        NSFontAttributeName: font,
                        NSParagraphStyleAttributeName: paragraphStyle,
                        NSForegroundColorAttributeName:[UIColor blackColor]
                        };

NSMutableAttributedString * mutalbleAttributedString = [[NSMutableAttributedString alloc] initWithString:textString attributes:messageDrawRectAttributesDictionary];

I want to know (for later use in the app) how to get what alignment I set in this string.

I tried this code, without understanding exactly what I'm doing:

NSRange range = NSMakeRange(0,mutalbleAttributedString.length-1);
NSDictionary * paraDic =[mutalbleAttributedString attribute:NSParagraphStyleAttributeName atIndex:0 longestEffectiveRange:&range inRange:range];

Now, I search for a lot of sample code and I read the Apple docs. But no luck. I get empty dictionary.

The only help I need is if someone can just write the right code that will return me the data of the alignment.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user1105951
  • 2,259
  • 2
  • 34
  • 55

3 Answers3

4

When you call attribute:atIndex:longestEffectiveRange:inRange:, the return value is the value of the requested attribute, not a dictionary. Also, do not pass in range for both range parameters.

Your code should be something like this:

NSRange range = NSMakeRange(0, mutalbleAttributedString.length);
NSParagraphStyle *paraStyle = [mutalbleAttributedString attribute:NSParagraphStyleAttributeName atIndex:0 longestEffectiveRange:NULL inRange:range];
NSTextAlignment alignment = paraStyle.alignment;

Note the following changes:

  • The proper return value for the attribute being requested
  • Settings the proper length for range
  • Passing NULL for the longestEffectiveRange: since you don't care about that value.
rmaddy
  • 314,917
  • 42
  • 532
  • 579
1

You can get the alignment from the NSMutableAttributedString like this:

 NSRange range = NSMakeRange(0, mutableAttributedString.length);
 NSDictionary *attributes = [mutableAttributedString attributesAtIndex:0 effectiveRange:&range];
 NSMutableParagraphStyle *paragraphStyle = attributes[NSParagraphStyleAttributeName];
 NSTextAlignment alignment = paragraphStyle.alignment;
joern
  • 27,354
  • 7
  • 90
  • 105
  • 1
    1. `range` is incorrect. 2. Do not hardcode the key to get the attribute. Use the proper constant provided by the API. – rmaddy Oct 31 '15 at 16:12
  • Thanks for your comment! You are right, I edited my answer. – joern Oct 31 '15 at 16:14
0

Swift 5 example:

var effectiveRange = NSMakeRange(0, attributedString.length)
let currentParagraphStyle: NSParagraphStyle = attributedText?.attribute(NSAttributedString.Key.paragraphStyle, at: 0, effectiveRange: &effectiveRange) as! NSParagraphStyle
let align = currentParagraphStyle.alignment
let lineSpacing = currentParagraphStyle.lineSpacing
let newParagraphStyle = currentParagraphStyle.lineHeightMultiple
coldembrace
  • 549
  • 8
  • 19