简体   繁体   中英

NSAttributedString adjusting baseline of range doesn't work in UILabel as expected

I am trying to adjust the baseline of a portion of an attributed string

UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(cell.contentView.bounds.size.width-100, 0, 75, cell.contentView.bounds.size.height)];
[cell.contentView addSubview:detailLabel];
detailLabel.textAlignment = NSTextAlignmentRight;

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"5" attributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Thin" size:18]}];
NSAttributedString *subScript = [[NSAttributedString alloc] initWithString:@"b" attributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Thin" size:18],
                                                                                                (NSString *)kCTSuperscriptAttributeName: @(-2)}];
[attributedString appendAttributedString:subScript];


cell.attributedText = attributedString;

But all of the text is adjusted and not only the portion that was attributed

I've tried playing with the UILabel's baselineAdjustment property but didn't see any difference

Here is the attributed string description

Printing description of attributedString:
5{
    NSFont = "<UICTFont: 0x7fbde0d548a0> font-family: \"HelveticaNeue-Thin\"; font-weight: normal; font-style: normal; font-size: 18.00pt";
}b{
    NSFont = "<UICTFont: 0x7fbde0d548a0> font-family: \"HelveticaNeue-Thin\"; font-weight: normal; font-style: normal; font-size: 18.00pt";
    NSSuperScript = "-2";
}
(lldb) 

This is the visible result

从模拟器打印屏幕

Image from Debug View Hierarchy

调试打印屏幕

It can be done with this typographic trick:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"5" attributes:@{
    NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Thin" size:18],
}];
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@" " attributes:@{
    NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Thin" size:18],
    (NSString *)kCTSuperscriptAttributeName: @2,
    NSKernAttributeName: @-5,
}]];
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"b" attributes:@{
    NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Thin" size:18],
    (NSString *)kCTSuperscriptAttributeName: @-2,
}]];

That's what's happening here:

  1. Adding a space character between "5" and "b" with the same font as "b".
  2. Shifting that space in opposite direction from "b" shift, and with the same value. Now bounds of "5 b" string are incremented by "space" up for equal amount as incremented by "b" down. UILabel will align resulting attributed string and "5" will be dead center.
  3. Visually shifting "b" to "5" by applying negative kerning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM