调整UILabel以适应? [英] Sizing a UILabel to fit?

查看:153
本文介绍了调整UILabel以适应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何修改以下片段(在tableView:cellForRowAtIndexPath:UITableViewController方法)从iPhone开发人员菜谱第6章的09a - PrefsTable食谱:

  if(row == 1){
//创建一个大字包裹的UILabel
cell = [tableView dequeueReusableCellWithIdentifier:@libertyCell];
if(!cell){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@libertyCell] autorelease];
[cell addSubview:[[UILabel alloc] initWithFrame:CGRectMake(20.0f,10.0f,280.0f,330.0f)]];
}
UILabel * sv = [[cell subviews] lastObject];
sv.text = @当在人类事件的过程中,一个人变得有必要解散将它们与另一个联系起来的政治乐队,并在地球的力量中承担独立和平等的自然法则和上帝赋予他们的权利,对人类的意见的正当尊重,要求他们宣布促使他们分离的原因。
sv.textAlignment = UITextAlignmentCenter;
sv.lineBreakMode = UILineBreakModeWordWrap;
sv.numberOfLines = 9999;
return cell; UILabel子视图和单元格大小$
}

UITableViewCell的大小只是足够大,以适应文本(和工作与更多或更少的文本,和其他类型的文本对齐)?我看了UILabel textRectForBounds:limitedToNumberOfLines:方法,但是文档声明它不应该被直接调用(并且应该被覆盖)。我尝试使用UIView sizeToFit方法,但没有成功。



更新: 我问了一个新的问题关于我的问题与NSString -sizeWithFont:forWidth:lineBreakMode:方法

解决方案

我必须这样做,我扩展UILabel为我做这个:

  @interface UILabel(BPExtensions)
- (void)sizeToFitFixedWidth:(CGFloat)fixedWidth;
@end

@implementation UILabel(BPExtensions)


- (void)sizeToFitFixedWidth:(CGFloat)fixedWidth
{
self.frame = CGRectMake(self.frame.origin.x,self.frame.origin.y,fixedWidth,0);
self.lineBreakMode = NSLineBreakByWordWrapping;
self.numberOfLines = 0;
[self sizeToFit];
}
@end

How would one modify the following snippet (in a tableView:cellForRowAtIndexPath: UITableViewController method) from the "09a - PrefsTable" recipe from Chapter 6 of The iPhone Developer's Cookbook:

if (row == 1) { 
    // Create a big word-wrapped UILabel 
    cell = [tableView dequeueReusableCellWithIdentifier:@"libertyCell"]; 
    if (!cell) { 
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"libertyCell"] autorelease]; 
        [cell addSubview:[[UILabel alloc] initWithFrame:CGRectMake(20.0f, 10.0f, 280.0f, 330.0f)]]; 
    } 
    UILabel *sv = [[cell subviews] lastObject]; 
    sv.text =  @"When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation."; 
    sv.textAlignment = UITextAlignmentCenter; 
    sv.lineBreakMode = UILineBreakModeWordWrap; 
    sv.numberOfLines = 9999; 
    return cell; 
} 

...to size the "sv" UILabel subview and the "cell" UITableViewCell to be sized just big enough to fit the text (and work with more or less text, and other types of text alignment)?  I looked at the UILabel textRectForBounds:limitedToNumberOfLines: method, but the documentation states that it should not be called directly (and should only be overridden).  I experimented with the UIView sizeToFit method, without success.

Update: I asked a new question about my problem with the NSString -sizeWithFont:forWidth:lineBreakMode: method.

解决方案

I had to do this enough that I extended UILabel to do it for me:

@interface UILabel (BPExtensions)
- (void)sizeToFitFixedWidth:(CGFloat)fixedWidth;
@end

@implementation UILabel (BPExtensions)


- (void)sizeToFitFixedWidth:(CGFloat)fixedWidth
{
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, fixedWidth, 0);
    self.lineBreakMode = NSLineBreakByWordWrapping;
    self.numberOfLines = 0;
    [self sizeToFit];
}
@end

then to have a label to have a variable multiline height but a fixed width just:

[myLabel sizeToFitFixedWidth:kSomeFixedWidth];

这篇关于调整UILabel以适应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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