【问题标题】:How Can I indent only first line of multiline UILabel in iOS?如何在 iOS 中仅缩进多行 UILabel 的第一行?
【发布时间】:2014-01-29 12:08:17
【问题描述】:

我想在 UILabel 的开头添加一个图像。标签是多行的。如果我使用 contentInset,它会缩进整个标签,但我只想缩进第一行。

到目前为止我已经尝试过了,这对我不起作用。

    UIEdgeInsets titleInsets = UIEdgeInsetsMake(0.0, 40.0, 0.0, 0.0);
    valueLabel.contentInset = titleInsets;

它应该看起来像这样。

【问题讨论】:

    标签: ios objective-c ios7 uilabel


    【解决方案1】:

    @DavidCaunt 的建议对我有用。我在这里分享代码。

    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.firstLineHeadIndent = 50;
    
    [attributedText addAttribute:NSParagraphStyleAttributeName value:style range:range];
    
    [valueLabel setAttributedText:attributedText];
    

    【讨论】:

    • 我知道这是正确的方法,但您是否尝试过使用自定义字体。对我来说它不起作用。
    【解决方案2】:

    正如用户 716216 指出的那样,另外 - 我们可以使用具有定义缩进值的制表符:

    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.headIndent = 50;
    
    label.attributedText = [[NSAttributedString alloc] initWithString:
        @"\tHow can i add image like this in start of UILabel? Label is multiline.........."
        attributes:@{NSParagraphStyleAttributeName: paragraphStyle}];
    

    【讨论】:

    • 我不得不在一个 UILabel 中缩进 2 行,所以我使用了:paragraphStyle.firstLineHeadIndent = 7;段落样式.headIndent = 7;
    【解决方案3】:

    您可以在 Interface Builder 中执行此操作:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 2021-12-03
      • 1970-01-01
      相关资源
      最近更新 更多