ios - 两个不同字体大小的自动布局UILabel : Text Top alignment

标签 ios autolayout uilabel

有没有办法设置两个UILabel文本顶部对齐?两者的字体大小不同。 enter image description here

最佳答案

我可以向您展示这张图片的示例:

enter image description here

代码:

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

关于ios - 两个不同字体大小的自动布局UILabel : Text Top alignment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38863255/

相关文章:

ios - 如何在 Autolayout 中滑动 tableview 单元格?

swift - 在 UIStackView 中调整 View 的大小以在运行时填充剩余的屏幕大小

iOS 自动布局 : How to show/hide a view including its margins?

ios - 如何让以# 或@ 字符串开头的单词像 UIButton 一样?

ios - 核心数据不同计数

ios - 从具有多个部分的 TableView 继续

ios - `willPresent notification` 是 't called & local notifications aren' t 当应用程序在 iOS 10 中处于前台时不显示

ios - 在 Swift 中对 Big Int 值进行排序

ios - 将 UILabel 文本与特定字符对齐

swift - 属性字符串切割并在 UILabel 上将单个单词显示为两行