修复UILabel的边框

时间:2012-11-21 09:17:27

标签: objective-c ios xcode uitableview uilabel

我正在将UILabel内的文字带到UItableViewCell内(标签文字来自webData,因此大小不一)。

我想为我的标签设置一个边框,它应该符合文字的宽度和高度。我创造了一个,但看起来并不好 帮助我改进我的代码。

**还有什么方法可以获得圆角边框? **

嘿我这样在边框内得到文字,角落不是那么圆润:

enter image description here

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 个答案:

答案 0 :(得分:3)

*还有什么方法可以获得圆角边框? *

#import <QuartzCore/QuartzCore.h>
label.layer.borderWidth = 3;
label.layer.borderColor = [[UIColor blackColor] CGColor];
label.layer.cornerRadius = 5;

答案 1 :(得分:2)

圆角设置。

[cmntBoxlbl.layer setCornerRadius:15];

同时添加QuartzCore框架并导入标题:

#import <QuartzCore/QuartzCore.h>