2015-04-15 29 views
0

我想調整的屬性串NSAttributedString調整的範圍基線的UILabel不能按預期

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; 

的部分基線,但所有的文本進行調整,不僅是部分歸因

我已經試過與UILabel's baselineAdjustment物業玩,但沒有看到任何區別

這裏是屬性串描述

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) 

這是顯着的成果

Print screen from the simulator

圖片來自調試視圖層次

Debug print screen

回答

0

它可以用這個排版技巧來完成:

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, 
}]]; 

這這裏發生了什麼:

  1. 在「5」和「b」之間用與「b」相同的字體添加空格字符。
  2. 從「b」檔位向相反方向移動該空間,並使用相同的值。現在「5b」字符串的範圍增加了「空間」等量增加「b」向下。 UILabel將對齊所產生的屬性字符串,並且「5」將成爲死點。
  3. 通過應用負的字距視覺地將「b」移動到「5」。