gpt4 book ai didi

ios - 垂直顶部对齐 UILabel 中的文本部分

转载 作者:行者123 更新时间:2023-11-29 03:04:33 25 4
gpt4 key购买 nike

我们如何对标签进行子类化,使其显示文本如下前缀(顶部对齐)、文本后缀(底部对齐)。

我目前在 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;
}

但这将文本设置为上标并不能解决问题。前缀文本和正文的顶部应相同,但前缀文本应垂直顶部对齐且字体较小!

最佳答案

使用 NSBaselineOffsetAttributeName 解决了我的问题,使用它我们可以指定距底部的偏移量。+ve 用于上标,-ve 用于下标

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

}

关于ios - 垂直顶部对齐 UILabel 中的文本部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22932991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com