NSLineBreakByWordWrapping不工作ios7(NSLineBreakByWord

2019-10-19 08:17发布

我有从数据库..在的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];

Answer 1:

使用以下让你的文字或标签的大小动态

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


文章来源: NSLineBreakByWordWrapping not working ios7