3 回答

TA貢獻1827條經驗 獲得超9個贊
UILabel
drawTextInRect:
- (void)drawTextInRect:(CGRect)rect { UIEdgeInsets insets = {0, 5, 0, 5}; [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];}
override func drawText(in rect: CGRect) { let insets = UIEdgeInsets.init(top: 0, left: 5, bottom: 0, right: 5) super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))}
沒有必要通過發送 sizeToFit
訊息 如果你的標簽有背景,而且你不想讓它縮小,它就會讓標簽框單獨使用。

TA貢獻1772條經驗 獲得超5個贊
對于多行文本,可以使用NSAttributedString設置左右邊距。
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.alignment = NSTextAlignmentJustified;
style.firstLineHeadIndent = 10.0f;
style.headIndent = 10.0f;
style.tailIndent = -10.0f;
NSAttributedString *attrText = [[NSAttributedString alloc] initWithString:title attributes:@{ NSParagraphStyleAttributeName : style}];
UILabel * label = [[UILabel alloc] initWithFrame:someFrame];
label.numberOfLines = 0;
label.attributedText = attrText;
- 3 回答
- 0 關注
- 1336 瀏覽
添加回答
舉報