0
我們怎樣才能繼承的標籤,以便它顯示的文字如下 前綴(對齊頂部),文本和,後綴(對齊底部)。頂部垂直文本部分對齊的UILabel
我已經目前使用上的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;
}
的子類下面的方法,但這種設置文本爲上標不解決問題。前綴文本和文本的頂部應該相同,但前綴文本應該垂直頂部與較小的字體對齊!
NSMutableAttributedString是好的,所有複雜的同時, 。 –
這個鍵值線幫助我在同一中心線垂直放置* part-with-smaller-font-size *,例如* part-with-larger-font-size *:'NSBaselineOffsetAttributeName:@(1.0f)' – sig