Vertically top align the part of text in UILabel

How can we subclass a label so that it displays text as follows prefix (aligned to top), text and, suffix (aligned to bottom).

I have used following method currently on subclass of UILabel

-(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;
    prefixTextAttributes[(NSString*)kCTSuperscriptAttributeName] = [NSNumber numberWithUnsignedInteger:_prefixTextVerticalPositionning];

    NSMutableDictionary * suffixTextAttributes = [@{} mutableCopy];
    suffixTextAttributes[NSFontAttributeName]  = _suffixTextFont? _suffixTextFont:self.font;
    suffixTextAttributes[NSForegroundColorAttributeName] = _suffixTextColour ?_suffixTextColour:self.textColor;
    suffixTextAttributes[(NSString*)kCTSuperscriptAttributeName] = [NSNumber numberWithUnsignedInteger:_suffixTextVerticalPostioning];

    if(prefixText)
        [attributedCompoundText addAttributes:prefixTextAttributes  range:[compondText rangeOfString:prefixText]];
    if(suffixText)
        [attributedCompoundText addAttributes:suffixTextAttributes  range:[compondText rangeOfString:suffixText]];

    self.attributedText = attributedCompoundText;
}

But this sets the text as superscript which doesn't solve the problem. The top of prefix text and text should be same,But prefix text should be vertically top aligned with smaller font size!


Using NSBaselineOffsetAttributeName solved my problem,Using this we can specify the offset from the bottom.+ve for superscript and -ve for subscript

   -(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;

}

链接地址: http://www.djcxy.com/p/4846.html

上一篇: 垂直居中对齐UIlabel中的文本

下一篇: 垂直顶部对齐UILabel中的部分文本