如果您可以通过界面构建器添加标签,它将节省大量的标签属性代码,尽管从您所描述的内容来看,如果您使用下面的代码(替换所需的位置,大小和文本值),那么这个标签会根据你填充的内容而改变。我一直使用它,完美地工作。
UILabel *label = [[UILabel alloc] initWithFrame:GCRectMake(0,0,300, 50)]; //these values to be changed to reflect where you want the label to appear, initial position and size, width etc.
//setting up the label attributes etc
label.numberOfLines = 0; //This means there's no limit to lines of text.
label.font = [UIFont systemFontOfSize:13];
label.textColor = [UIColor blackColor];
NSString *content = YOUR_TEXT;// your example @"jklljk sdkhfdjkdsfjhkfk fhs fdh fk dksdks dfss s dfs dfs fsdkdfks dfks dfks df k dfh";
CGSize maximumLabelSize = CGSizeMake(300, 1000); //For example - the height can be changed to any maximum value.
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:13] forKey: NSFontAttributeName]; //This allows a calculation to be made of the space taken up, so if you're using a custom or large font it will calculate accordingly.
CGSize newExpectedLabelSize = [content boundingRectWithSize:maximumLabelSize options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil].size;
CGRect frame = label.frame;
frame.size.height = newExpectedLabelSize.height;
label.frame = frame; //This last line should change the height of your label according to what it needs to be to have all the text visible and over multiple lines.
我希望这可以帮助你找到你要找的东西。
(这取代了需要任何约束的编码,你有太多。)
干杯,吉姆
,如果你希望它有多行,你必须做出'numberOfLines'大于0 – user2277872
我已将其设置为零,因为我没有多少准确的数据,并且我在某处将其设置为零将会处理它 – Haagenti
http://stackoverflow.com/questions/18315441/with-what-should -i-replace-the-deprecated-sizewithfontcontrainedtosizelinebrea ?? –