亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

UILabel文字邊距

UILabel文字邊距

iOS
隔江千里 2019-08-09 11:11:43
UILabel文字邊距我正在設置a的左邊插入/邊距,UILabel但找不到這樣做的方法。標簽有一個背景設置,所以只是改變它的起源不會有效。10px左手邊左右插入文本是理想的。
查看完整描述

3 回答

?
POPMUISE

TA貢獻1765條經驗 獲得超5個贊

我通過子類化UILabel和覆蓋解決了這個問題drawTextInRect:

- (void)drawTextInRect:(CGRect)rect {
    UIEdgeInsets insets = {0, 5, 0, 5};
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];}

Swift 3.1:

override func drawText(in rect: CGRect) {
    let insets = UIEdgeInsets.init(top: 0, left: 5, bottom: 0, right: 5)
    super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))}

Swift 4.2.1:

override func drawText(in rect: CGRect) {
    let insets = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
    super.drawText(in: rect.inset(by: insets))}

正如你可能已經聚集的那樣,這是對tc。答案的改編。它有兩個優點:

  1. 沒有必要通過發送sizeToFit消息來觸發它

  2. 它會單獨留下標簽框 - 如果您的標簽有背景并且您不希望它縮小,則很方便


查看完整回答
反對 回復 2019-08-09
?
慕后森

TA貢獻1802條經驗 獲得超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;


查看完整回答
反對 回復 2019-08-09
  • 3 回答
  • 0 關注
  • 916 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號