2012-11-21 133 views
0

朋友, 我創建了一個帶邊框的UILabel(就像下圖所示)。標籤邊界內的間距

我想在離開一行/兩行後再開始我的標籤,然後在標籤的最後一行再次下一行/兩行之後開始標籤。

有沒有辦法讓標籤的邊界內部有空格?

This is the UILabel with 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]; 

回答

2

使當前標籤(commentLabel)顏色爲白色。創建另一個具有相同內容和較小尺寸的標籤,將其放置在邊界內。使用填充作爲您的願望

0
Have you tried this- 

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

感謝rahul ...它爲第一行空間工作 但是在文本之後,這不起作用 我意思是在最後一行之後沒有空間低於 –

+0

我試過了,它適用於我。結果只有一行是空白的。嘗試在下面提供更多空行,例如: NSString * text = [NSString stringWithFormat:@「\ n \ n \ n」,@「」,[[self.DtlArray objectAtIndex: indexPath.row] objectForKey:@「comment」],@「」]; –

0

您可以創建自定義視圖和

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

希望幫助