1

我有一个 UILable,它在我按下按钮时显示文本。文本来自属性字符串。其中一个按钮需要一个上标属性:

string = [[NSMutableAttributedString alloc]initWithString:@"A"];
[string addAttribute:NSFontAttributeName value:(font) range:NSMakeRange(string.length-1, 1)];
[string addAttribute:(NSString*)kCTSuperscriptAttributeName value:@"1" range:NSMakeRange(string.length-1, 1)];
[string2 appendSttributedString: string];
label.attributedText = string2;

只要 string2 适合 UILable 中的一行,此代码就可以工作。当文本开始跨越两行时,它会按原样显示。但是,当添加 kCTSuperscriptAttributeName 上标属性时,标签的第二行会消失并被截断。我不确定发生了什么。有人有想法吗?

4

2 回答 2

0

您是否为 NSAttributedString 尝试了“NSBaselineOffsetAttributeName”?

     -(void)setText:(id)text withPrefixText:(id)prefixText andSuffixText:(id)suffixText
 {
NSString * compondText = [self textByCompoundingText:text withPrefixText:prefixText WithsuffixText:suffixText];
NSMutableAttributedString * attributedCompoundText = [[NSMutableAttributedString alloc] initWithString:compondText];

NSMutableDictionary * prefixTextAttributes = [@{} mutableCopy];
prefixTextAttributes[NSFontAttributeName]  = _prefixTextFont? _prefixTextFont:self.font;
prefixTextAttributes[NSForegroundColorAttributeName] = _prefixTextColour? _prefixTextColour:self.textColor;
NSNumber *baselineOffSet =[NSNumber numberWithUnsignedInteger:_prefixTextVerticalPositionning];
prefixTextAttributes[(NSString*)NSBaselineOffsetAttributeName] = baselineOffSet;

NSMutableDictionary * suffixTextAttributes = [@{} mutableCopy];
suffixTextAttributes[NSFontAttributeName]  = _suffixTextFont? _suffixTextFont:self.font;
suffixTextAttributes[NSForegroundColorAttributeName] = _suffixTextColour ?_suffixTextColour:self.textColor;
baselineOffSet = [NSNumber numberWithUnsignedInteger:_suffixTextVerticalPostioning];
suffixTextAttributes[(NSString*)NSBaselineOffsetAttributeName] = baselineOffSet;
if(prefixText)
    [attributedCompoundText addAttributes:prefixTextAttributes  range:[compondText rangeOfString:prefixText]];
if(suffixText)
    [attributedCompoundText addAttributes:suffixTextAttributes  range:[compondText rangeOfString:suffixText]];
self.attributedText = attributedCompoundText;
}
于 2014-04-08T12:39:09.017 回答
0

确保numberOfLines将 UILabel 上的属性设置为 2 或其他值,以便标签不会截断超出第一行。希望这可以帮助!

于 2013-09-11T00:50:55.553 回答