What's the best way to add the left margin to an NSAttributedString?

I am trying to add a left hand marker to an NSAttributedString so that when I combine it with another NSAS there is a bit of space between the two frame frames.

All I have so far:

NSMutableAttributedString *issn = [[NSMutableAttributedString alloc] initWithString:jm.issn attributes:nil];

NSRange range = NSMakeRange(0, [issn length]);
[issn addAttribute:NSFontAttributeName
             value:[UIFont fontWithName:@"AvenirNext-Medium" size:8]

NSMutableAttributedString *textLabel = [[NSMutableAttributedString alloc] initWithAttributedString:title];
[textLabel  appendAttributedString:issn];


I want a margin on the left side of the second row.


Edit: image upload

enter image description here


source to share

3 answers

Why not just use a tab character between the two lines?

This can be done by changing the first line:

NSMutableAttributedString *issn = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"\t%@", jm.issn] attributes:nil];


This should output something like what you want. However, you can add 2 \t

characters instead of one, because depending on the length of the line, you might not need a tab character to align it (for example, on this exact line you posted, it didn't add anything to my output).

1 tab with your line:

One tab

2 tabs with your string:

Two tabs



You can not. If you are concatenating attributed strings, then there is no "margin" around the specified range in the final string. How will this work with multiple lines or text wrapping?

If you need free space in the attribute string, use spaces - spaces or tabs. You can define the position of tab stops using paragraph styles.



All you can do is add the necessary spaces (or whitespace) before the original string and then add them to your NSMutableAttributedString.

 NSString *newString = [NSString stringWithFormat:@"  %@", jm.issn] <- Have given two spaces here.





All Articles