UILabel不会改变

时间:2011-04-27 05:56:59

标签: iphone uilabel

我的代码:

- (void)viewDidLoad 
 {
NSString *text = @"fjkldsjfkladsjfkljsdaklfjadsklfjdskjfdskjflkdsahfahfdkdfdsfasdfasdfafjkldsjfkladsjfkljsdaklfjadsklfjdskjfdskjflkdsahfahfdkdfdsfasdfasdfass"
                "fjkldsjfkladsjfkljsdaklfjadsklfjdskjfdskjflkdsahfahfdkdfdsfasdfasdfas"
                "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;
}

我有一个返回格式化标签的方法。 myLabel是通过IB推出的。问题是返回标签不会影响myLabel。

代码有什么问题? myLabel不会更改文本,框架等。

3 个答案:

答案 0 :(得分:1)

试试这个,

将您的 mylabel 传递给... ...

- (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;
}

答案 1 :(得分:0)

您没有修改 myLabel 的属性。这就是问题所在。

解决这个问题的方法是,将 mylabel 作为参数传递给函数,并使用该引用修改属性,让函数不返回任何内容。

答案 2 :(得分:0)

我认为基本问题是根据文字设置标签的正确高度。 试试这个代码: -

它更简单,你不需要写一些额外的方法

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

设置你自己的constraintTosize和frame x和y cordinate。