NSLineBreakByWordWrapping不工作ios7 [英] NSLineBreakByWordWrapping not working ios7

查看:167
本文介绍了NSLineBreakByWordWrapping不工作ios7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UILabel which is populated from the database .. the text in the UILabel can be long and can be short .. the problem is that the text is being cut at the end .. for example its shown like this: "www.sample.com/h1/h2/h3..." where it should be: "www.sample.com/h1/h2/h3/h4/h5/h6.html"

这是我正在使用的代码:

This is the code I am using:

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

I am using Xcode 5 and iOS 7

已解决:
我设法使用此方法解决了这个问题:

SOLVED: I managed to solve it using this method:

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


推荐答案

使用以下方法获取文字或标签的动态大小

Use following to get dynamic size of your text or label

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

这篇关于NSLineBreakByWordWrapping不工作ios7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆