Question

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!

Was it helpful?

Solution

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;

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top