【发布时间】:2017-11-14 22:22:30
【问题描述】:
我想将图像居中到 UILabel 第一行文本的中心 Y 位置。我使用砌体来设置这样的自动布局约束:
[_haveReadIndicatorImgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.equalTo(self.contentView).offset(SMALL_OFFSET);
make.height.width.equalTo(@(8));
}];
[_topTxtlbl mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_haveReadIndicatorImgView.mas_right).offset(TINY_OFFSET);
make.top.equalTo(_haveReadIndicatorImgView.mas_top);
make.right.equalTo(self.arrowImgView.mas_left).offset(-SMALL_OFFSET);
make.bottom.equalTo(_dateTxtLbl.mas_top).offset(-SMALL_OFFSET);
}];
它应该非常简单。我只是将 UIImageView 的顶部附加到标签的顶部。
但是看看屏幕。
UIImageView 的上边缘(灰点)和标签是相等的,但是如何让 UIImageView 像这样居中于第一行文本呢?
谢谢。
【问题讨论】:
-
可能更容易使用
NSAttributedString,NSParagraphStyle,firstLineHeadIndent设置为 0,headIndent设置为 10(对于文本的其余部分),以及使用NSTextAttachment(项目符号的图像,宽度将是headIndent值)或直接使用项目符号点字符•(或字符列表中的另一个)。 -
@Larme 你能举个例子吗?
-
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:@"• MyLongTextHere"]; NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; [style setHeadIndent:15]; [style setFirstLineHeadIndent:0]; [attr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, [attr length])];? -
@Larme 谢谢你,但我用不同的方法达到目的,将作为答案发布。
标签: ios objective-c autolayout