標籤邊框內的間距 (spacing inside label's border)


問題描述

標籤邊框內的間距 (spacing inside label's border)

Friends, i have created a UILabel with border (just like the image below).

I want to start my label after one/two lines left and then after finishing last line of label again one/two lines below.

Is there any way to get spacing inside label's border?

    UILabel *cmntBoxlbl = [[UILabel alloc]initWithFrame:CGRectMake(58, 23, 250, 60)];
     cmntBoxlbl.font=[UIFont fontWithName:@"Arial" size:12];
     cmntBoxlbl.layer.borderColor = [UIColor darkGrayColor].CGColor;
     cmntBoxlbl.layer.borderWidth = 1.0;
      NSString *text = [NSString stringWithFormat:@"%@%@%@",@"  ",[[self.DtlArray  objectAtIndex:indexPath.row] objectForKey:@"comment"],@" "];
      cmntBoxlbl.text = text;

      cmntBoxlbl.textAlignment = UITextAlignmentCenter;
      cmntBoxlbl.lineBreakMode = UILineBreakModeWordWrap;
      [cmntBoxlbl setTextColor:[UIColor darkGrayColor]];

      CGSize expectedLabelSize = [text sizeWithFont:cmntBoxlbl.font
                            constrainedToSize:cmntBoxlbl.frame.size
                                lineBreakMode:UILineBreakModeWordWrap];

      CGRect newFrame = cmntBoxlbl.frame;
      newFrame.size.height = expectedLabelSize.height;
      cmntBoxlbl.frame = newFrame;
      cmntBoxlbl.numberOfLines = 0;
      [cmntBoxlbl sizeToFit];
      [cell addSubview:cmntBoxlbl];

‑‑‑‑‑

參考解法

方法 1:

Make current label(commentLabel) color is white. create another label with same content and a little smaller size,place it inside the boarder.Make padding as you wish

方法 2:

Have you tried this‑

      NSString *text = [NSString stringWithFormat:@"\n\n%@%@%@\n\n",@"  ",[[self.DtlArray  objectAtIndex:indexPath.row] objectForKey:@"comment"],@" "];

方法 3:

You may create a custom view and 

‑ (void)drawRect:(CGRect)rect {
      ......;
      [string drawInRect:rect withFont:font lineBreakMode:mode alignment:ali];
      ......;
}

hope that helps

(by Nithin M Kelothuser1824262Rahul WakadeDaniel Gao)

參考文件

  1. spacing inside label's border (CC BY‑SA 3.0/4.0)

#uilabel #objective-c #iphone #iOS #XCode






相關問題

標籤邊框內的間距 (spacing inside label's border)

awakeFromNib 無法從 UILabel 獲取值,也無法禁用 UIButton (awakeFromNib can't get value from UILabel and can't disable UIButton)

如何在 UILabel 中製作陰影 (How to make shadow in UILabel)

如何計算 UILabel 的字體大小 (How can I calculate font size of UILabel)

如何在情節提要中為uilabel設置大於300的字體大小? (How to set font size greater than 300 for uilabel in storyboard?)

隨著 UILabel 尺寸的增加,在我的 UILabel 周圍獲得額外的空間 (Getting extra space around my UILabel as it increases in size)

UILabel 不顯示所有內容 (UILabel not displaying everything)

在 UIView 中將標籤居中 (Centering a label in a UIView)

UITableViewCell 未在其中顯示 UILabel (UITableViewCell not showing UILabel within)

帶有 UIMenucontroller 的 UILabel 不會通過外部觸摸退出第一響應者 (UILabel with UIMenucontroller not resigning first responder with touch outside)

UILabel 動態高度問題 (UILabel Dynamic height problem)

是否可以將標籤內的文本與“大”框架垂直對齊 (Is it possible to vertically align text inside labels with a "large" frame)







留言討論