NSLineBreakByWordWrapping不工作ios7

时间:2014-02-12 13:10:54

标签: objective-c ios7 xcode5 line-breaks word-wrap

我有一个从数据库填充的UILabel .. UILabel中的文本可能很长而且可能很短..问题是文本末尾被删除了.. 例如它显示如下: “www.sample.com/h1/h2/h3 ......” 它应该在哪里:“www.sample.com/h1/h2/h3/h4/h5/h6.html”

这是我正在使用的代码:

CGFloat constraintWidth = 180.0f;

CGSize labelsize = [details.web sizeWithFont:self.webTextLabel.font constrainedToSize:CGSizeMake(constraintWidth, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];

CGRect webFrame = CGRectMake(20, self.webTitleLabel.frame.origin.y + self.WebTitleLabel.frame.size.height + 10, constraintWidth, labelsize.height);

[self.webLabel setFrame:webFrame];

[self.webLabel setText:details.web]; // details.web is where I get it from the database

self.webLabel .numberOfLines=0;

我正在使用Xcode 5和iOS 7

解决: 我设法使用这种方法解决它:

UILabel *instructions = [[UILabel alloc]initWithFrame:CGRectMake(10, 225, 300, 180)];
   NSString *text = @"First take clear picture and then try to zoom in to fit the ";
   instructions.text = text;
   instructions.textAlignment = UITextAlignmentCenter;
   instructions.lineBreakMode = UILineBreakModeWordWrap;
   [instructions setTextColor:[UIColor grayColor]];

   CGSize expectedLabelSize = [text sizeWithFont:instructions.font 
                                constrainedToSize:instructions.frame.size
                                    lineBreakMode:UILineBreakModeWordWrap];

    CGRect newFrame = instructions.frame;
    newFrame.size.height = expectedLabelSize.height;
    instructions.frame = newFrame;
    instructions.numberOfLines = 0;
    [instructions sizeToFit];
    [self addSubview:instructions];

1 个答案:

答案 0 :(得分:-1)

使用以下内容获取文字或标签的动态尺寸

 labelHeight = [details.web boundingRectWithSize:CGSizeMake(constraintWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSLineBreakByWordWrapping attributes:@{NSFontAttributeName:self.webTextLabel.font} context:nil];