如题
使用UILabel时候想要文字靠右显示,但是不想顶着UILabel的尾部显示,类似于设置UIButton 的edge。 UILabel好像不可以直接设置edge,所以选择使用UILabel的attributedText。
以下是代码把想要的Label和想要表示的文字作为参数传入方法就可以了。
- (void)setAttributeStringForPriceLabel:(UILabel *)label text:(NSString *)text
{
NSMutableAttributedString *attrString = [[NSMutableAttributedString
alloc] initWithString:text];
NSUInteger length = [text length];
NSMutableParagraphStyle *
style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.tailIndent = -10;
style.alignment = NSTextAlignmentRight;
[attrString addAttribute:NSParagraphStyleAttributeName value:style
range:NSMakeRange(0, length)];
label.attributedText = attrString;
}
PS:同理设置左对齐的时候 还要为首行多设置一个属性:
style.firstLineHeadIndent = 10;
通过对UILabel的Attribute的使用,多行显示的时候还可以设置相邻两行文字的间距等。 总之花式UILabel的时候不要忘了 attributeString !