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:
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.