my code:
- (void)viewDidLoad
{
NSString *text = @"fjkldsjfkladsjfkljsdaklfjadsklfjdskjfdskjflkdsahfahfdkdfdsfasdfasdfafjkldsjfkladsjfkljsdaklfjadsklfjdskjfdskjflkdsahfahfdkdfdsfasdfasdfass"
"fjkldsjfkladsjfkljsdaklfjadsklfjdskjfdskjflkdsahfahfdkdfdsfasdfasdfa开发者_如何转开发s"
"fjkldsjfkladsjfkljsdaklfjadsklfjdskjfdskjflkdsahfahfdkdfdsfasdfasdfas";
myLabel = [self getTheHeight:text NameofFont:FONT_NAME fontSize:FONT_SIZE LineMode:UILineBreakModeWordWrap];
[super viewDidLoad];
}
- (UILabel *)getTheHeight:(NSString *)text NameofFont:(NSString *)font_name fontSize:(float)font_size LineMode:(UILineBreakMode)lineBreakMode
{
CGSize size = [text sizeWithFont:[UIFont fontWithName:FONT_NAME size:FONT_SIZE]];
UILabel *temp = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, size.height)];
[temp setFont:[UIFont fontWithName:FONT_NAME size:FONT_SIZE]];
[temp setLineBreakMode:UILineBreakModeWordWrap];
[temp setNumberOfLines:0];
temp.text = text;
return temp;
}
I have a method that returns the formatted label. the myLabel is putted through IB . The problem is that the return label does not affect to myLabel.
Whats wrong in code? myLabel does not change text , frame etc.
Try this,
Pass your mylabel to function like...
- (void)viewDidLoad
{
NSString *text = @"fjkldsjfkladsjfkljsdaklfjadsklfjdskjfdskjflkdsahfahfdkdfdsfasdfasdfafjkldsjfkladsjfkljsdaklfjadsklfjdskjfdskjflkdsahfahfdkdfdsfasdfasdfass"
"fjkldsjfkladsjfkljsdaklfjadsklfjdskjfdskjflkdsahfahfdkdfdsfasdfasdfas"
"fjkldsjfkladsjfkljsdaklfjadsklfjdskjfdskjflkdsahfahfdkdfdsfasdfasdfas";
[self getTheHeight:text NameofFont:FONT_NAME fontSize:FONT_SIZE LineMode:UILineBreakModeWordWrap withLabel:myLabel];
[super viewDidLoad];
}
- (void)getTheHeight:(NSString *)text NameofFont:(NSString *)font_name fontSize:(float)font_size LineMode:(UILineBreakMode)lineBreakMode withLabel:(UILabel*)temp
{
CGSize size = [text sizeWithFont:[UIFont fontWithName:FONT_NAME size:FONT_SIZE]];
temp.frame = CGRectMake(0, 0, 320, size.height);
[temp setFont:[UIFont fontWithName:FONT_NAME size:FONT_SIZE]];
[temp setLineBreakMode:UILineBreakModeWordWrap];
[temp setNumberOfLines:1];
temp.text = text;
}
You are not modifying the properties of myLabel. Thats the problem here.
A solution for this problem is, Pass the mylabel as a parameter to the function and modify the properties with that reference and let the function returns nothing.
I think the basic problem is to set the proper height of label according to the text. Try this code:-
It is simpler and you need not to write some extra methods
CGSize labelsize;
UILabel *commentsTextLabel = [[UILabel alloc] init];;
[commentsTextLabel setNumberOfLines:0];
[commentsTextLabel setBackgroundColor:[UIColor clearColor]];
NSString *text = @"fjkldsjfkladsjfkljsdaklfjadsklfjdskjfdskjflkdsahfahfdkdfdsfasdfasdfafjkldsjfkladsjfkljsdaklfjadsklfjdskjfdskjflkdsahfahfdkdfdsfasdfasdfassfjkldsjfkladsjfkljsdakljadsklfjdskjfdskjflkdsahfahf dkdfdsfasdfasdfasfjkldsjfkladsjfkljsdakladsklfjdskjfdskjflkdsahfahfdkdfdsfasdfasdfas";
[commentsTextLabel setFont:[UIFont fontWithName:@"Helvetica"size:14]];
labelsize=[text sizeWithFont:commentsTextLabel.font constrainedToSize:CGSizeMake(268, 2000.0) lineBreakMode:UILineBreakModeWordWrap];
commentsTextLabel.frame=CGRectMake(10, 24, 268, labelsize.height);
[commentsTextLabel release];
set your own constraintTosize and frame x and y cordinate.
精彩评论