English 中文(简体)
难以总结案文
原标题:Having difficulty wrapping text

我使用以下法典在海洋观测系统中添加案文。

//Set up label frame
UILabel *tempLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, 210, 80)];
self.answer_text_label = tempLabel;
[tempLabel release];    
[self.preview_answer_container addSubview:self.answer_text_label];

//Populate label with text
self.answer_text_label.text = self.answer.text;
self.answer_text_label.numberOfLines = 4;
self.answer_text_label.lineBreakMode = UILineBreakModeWordWrap;
[self.answer_text_label sizeToFit];

然而,我所得到的结果是,案文似乎超越了权利,而不是停留在我所贴的CGRectMake(100、10、210、80)标签所规定的框架之内。

“entergraph

如果我改为<条码>,则包装工程。 但是,这不利于我,因为我需要在我规定的标签框架内限制案文。

我能以什么方式总结案文,只保留4条线?

EDIT:

2. 询问所建议的法典

self.answer_text_label.text = self.answer.text;
[self.answer_text_label sizeToFit];
CGRect labelRect = self.answer_text_label.frame;
labelRect.origin.y = CGRectGetMaxY(labelRect);
labelRect.size = self.answer_text_label.frame.size;
self.answer_text_label.frame = labelRect;

结果如下。 似乎没有解决我的问题。

“entergraph

最佳回答

1. 明确设定框架

[self.answer_text_label sizeToFit];
CGRect labelRect = self.answer_text_label.frame;
labelRect.origin.y = CGRectGetMaxY(labelRect);
labelRect.size = self.answer_text_label.frame.size;
self.answer_text_label.frame = labelRect;

EDIT - Don t need to use this, just use following - remove these of code just use below, no other property of frame, remove sizeToFit as well -

self.answer_text_label.numberOfLines = 4;
self.answer_text_label.lineBreakMode = UILineBreakModeWordWrap;

纵向调整——(高于法典,也使用此法,不使用体积)

    CGSize textSize = [self.answer_text_label.text sizeWithFont:self.answer_text_label.font 
                                constrainedToSize:CGSizeMake(self.answer_text_label.frame.size.width, MAXFLOAT)
                                    lineBreakMode:self.answer_text_label.lineBreakMode];
self.answer_text_label.frame = CGRectMake(20.0f, 20.0f, textSize.width, textSize.height);
问题回答

在OS 6和以后,使用<代码>NSLineBreakByWordWrapping,而不是 UILineBreakModeWordWrap。

self.answer_text_label.lineBreakMode = NSLineBreakByWordWrapping;




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签