Nithin M Keloth
Nithin M Keloth

Reputation: 1595

Fixing border to UILabel

I am taking text to UILabel which is inside a UItableViewCell(label text is coming from webData-so it varies in size).

I want to give a border for my label, which should fit the text width and height. I have created one, but it's not looking good.
Help me to improve my code.

**Also is there any way to get border with rounded corners ? **

Hey I am getting text inside the border like this, and the corners are not so rounded:

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];

Upvotes: 1

Views: 1208

Answers (2)

Midhun MP
Midhun MP

Reputation: 107231

For rounded corner set.

[cmntBoxlbl.layer setCornerRadius:15];

Also add the QuartzCore framework and import the header:

#import <QuartzCore/QuartzCore.h>

Upvotes: 2

Andrey Chernukha
Andrey Chernukha

Reputation: 21818

*also is there any way to get border with rounded corners ?? *

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

Upvotes: 3

Related Questions