在UILabel中对齐不同大小的文本

时间:2013-03-20 23:55:17

标签: ios ios6 uilabel nsattributedstring

如何在UILabel中对齐不同大小的文本?一个例子是在价格横幅中将较小尺寸的分金额与较大尺寸的金额进行顶部对齐。

Sample Image

iOS6中的UILabel支持NSAttributedString,这允许我在同一个UILabel中包含不同大小的文本。但是它似乎没有顶部对齐文本的属性。有什么选择来实现这个?在我看来,根据自定义属性字符串键提供自定义绘图逻辑来进行顶部对齐可能是最好的,但我不知道如何去做。

4 个答案:

答案 0 :(得分:47)

我能够使用单个标签获得您想要的结果。

使用一点数学,你可以偏移较小文本的基线,以达到理想的效果。

目标-C

- (NSMutableAttributedString *)styleSalePriceLabel:(NSString *)salePrice withFont:(UIFont *)font
{
    if ([salePrice rangeOfString:@"."].location == NSNotFound) {
        return [[NSMutableAttributedString alloc] initWithString:salePrice];
    } else {
        NSRange range = [salePrice rangeOfString:@"."];
        range.length = (salePrice.length - range.location);
        NSMutableAttributedString *stylizedPriceLabel = [[NSMutableAttributedString alloc] initWithString:salePrice];
        UIFont *smallFont = [UIFont fontWithName:font.fontName size:(font.pointSize / 2)];
        NSNumber *offsetAmount = @(font.capHeight - smallFont.capHeight);
        [stylizedPriceLabel addAttribute:NSFontAttributeName value:smallFont range:range];
        [stylizedPriceLabel addAttribute:NSBaselineOffsetAttributeName value:offsetAmount range:range];
        return stylizedPriceLabel;
    }
}

夫特

extension Range where Bound == String.Index {
    func asNSRange() -> NSRange {
        let location = self.lowerBound.encodedOffset
        let length = self.lowerBound.encodedOffset - self.upperBound.encodedOffset
        return NSRange(location: location, length: length)
    }
}

extension String {
    func asStylizedPrice(using font: UIFont) -> NSMutableAttributedString {
        let stylizedPrice = NSMutableAttributedString(string: self, attributes: [.font: font])

        guard var changeRange = self.range(of: ".")?.asNSRange() else {
            return stylizedPrice
        }

        changeRange.length = self.count - changeRange.location
        // forgive the force unwrapping
        let changeFont = UIFont(name: font.fontName, size: (font.pointSize / 2))!
        let offset = font.capHeight - changeFont.capHeight
        stylizedPrice.addAttribute(.font, value: changeFont, range: changeRange)
        stylizedPrice.addAttribute(.baselineOffset, value: offset, range: changeRange)
        return stylizedPrice
    }
}

这产生以下结果:

答案 1 :(得分:9)

试图通过对齐框架的起源来做到这一点的问题是“正常”字符通常最终会在它们周围添加一些额外的填充,因为标签必须容纳字体字符的所有,包括那些高大的上升者和长长的下降者。您会注意到,在您发布的图片中,如果较小的“99”是与较大文本设置为相同原点的单独标签,则由于美元符号的最高点而会过高。

幸运的是,UIFont为我们提供了正确执行此操作所需的所有信息。我们需要测量标签使用的空的上升空间,并调整相对位置来考虑它,如下所示:

//Make sure the labels hug their contents
[self.bigTextLabel sizeToFit];
[self.smallTextLabel sizeToFit];

//Figure out the "blank" space above normal character height for the big text
UIFont *bigFont = self.bigTextLabel.font;
CGFloat bigAscenderSpace = (bigFont.ascender - bigFont.capHeight);

//Move the small text down by that ammount
CGFloat smallTextOrigin = CGRectGetMinY(self.bigTextLabel.frame) + bigAscenderSpace;

//Figure out the "blank" space above normal character height for the little text
UIFont *smallFont = self.smallTextLabel.font;
CGFloat smallAscenderSpace = smallFont.ascender - smallFont.capHeight;

//Move the small text back up by that ammount
smallTextOrigin -= smallAscenderSpace;

//Actually assign the frames
CGRect smallTextFrame = self.smallTextLabel.frame;
smallTextFrame.origin.y = smallTextOrigin;
self.smallTextLabel.frame = smallTextFrame;

(此代码假设您有两个分别名为bigTextLabelsmallTextLabel的标签属性


编辑:

没有两个标签这样做非常相似。您可以使用NSAttributedStrings方法创建自定义UIView子类并在其中绘制-drawInRect:options:context:(确保在您的选项中使用NSStringDrawingUsesLineFragmentOrigin)。计算顶部对齐的数学运算应该是相同的,唯一的区别是您通过NSFontAttributeName属性从属性字符串获取字体,而不是标签。关于归因字符串绘制的2012 WWDC视频是一个很好的参考。

答案 2 :(得分:2)

那太糟糕了。我没有足够的声誉来评论我在稍作修改后使用得很好的答案。 我只想指出使用smallfont代替美分而不是font,这可能是一个错字。

以下是修改后的代码

- (NSMutableAttributedString *)styleSalePriceLabel:(NSString *)salePrice withFont:(UIFont *)font
{
    if ([salePrice rangeOfString:@"."].location == NSNotFound) {
        return [[NSMutableAttributedString alloc]  
                   initWithString:salePrice];
    } else {
        NSRange range = [salePrice rangeOfString:@"."];
        range.length = (salePrice.length - range.location);
        NSMutableAttributedString *stylizedPriceLabel =
            [[NSMutableAttributedString alloc] initWithString:salePrice];
        UIFont *smallfont = [UIFont fontWithName:font.fontName 
                                            size:(font.pointSize / 2)];
        NSNumber *offsetAmount = @(font.capHeight - smallfont.capHeight);
        [stylizedPriceLabel addAttribute:NSFontAttributeName 
                                   value:smallfont 
                                   range:range];
        [stylizedPriceLabel addAttribute:NSBaselineOffsetAttributeName 
                                   value:offsetAmount 
                                   range:range];
        return stylizedPriceLabel;
    }
}

答案 3 :(得分:0)

一种可能的方法是使标签与您放入的文本的大小完全相同。

CGRect labelFrame;
//Set the origin of the label to whatever you want
labelFrame.size = [label.text sizeWithFont:label.font];  //If using NSString
labelFrame.size = [label.text size];                     //If using NSAttributedString
label.frame = labelFrame;

请参阅此similar SO post for more details

相关问题