Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 设置UILabel的最大行数_Iphone_Objective C_Cocoa Touch - Fatal编程技术网

Iphone 设置UILabel的最大行数

Iphone 设置UILabel的最大行数,iphone,objective-c,cocoa-touch,Iphone,Objective C,Cocoa Touch,我需要用UILabel做一些特殊的对齐 我希望它有1到3行,无论它有什么内容 目前我正在做这样的事情: label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0f]; label.backgroundColor = [UIColor clearColor]; label.numberOfLines = 0; label.lineBreakMode = UILineBreakModeWordWrap; label.fram

我需要用UILabel做一些特殊的对齐

我希望它有1到3行,无论它有什么内容

目前我正在做这样的事情:

label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0f];
label.backgroundColor = [UIColor clearColor];
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;

label.frame = CGRectMake(114.0f, 88.0f, 187.0f, 0.0f);
[label sizeToFit];
如果文本不太长,这很好用。如果字符串类似于
@“Hello World”
,则UILabel仅为14点高,如果它长一些,则会展开

现在我希望文本应该添加默认的
三个点,如果文本对于三行来说太长,但是设置在顶部,它会扩展到第四行


如何实现这一点?

只需设置
label.numberOfLines=3


它的标签有点错误,因为它实际上包含了要显示的最大行数。

调用
getStringSize
。它将返回类型为
StringSize
的变量,并动态决定行数

label.numberOfLines = 3;
CGSize size = [label.text sizeWithFont:label.font
                     constrainedToSize:CGSizeMake(SOME_WIDTH,
                                                  3 * label.font.lineHeight)
                         lineBreakMode:UILineBreakModeWordWrap];
label.bounds = CGRectMake(0, 0,
                          size.width,
                          size.height);
struct StringSize {

    int numberOfLine;
    float height;
    float width;

};

typedef struct StringSize StringSize;



+(StringSize)getStringSize:(NSString *)string ofWidth:(float)width  FontSize:(id)font {

    StringSize sizeForString;

    if(string){
        CGSize size = CGSizeMake(width, 400.0f);
        CGSize appStringSize = [string sizeWithFont:(UIFont *)font constrainedToSize:size     lineBreakMode:UILineBreakModeTailTruncation];
        CGSize sizeString = [string sizeWithFont:font];    

        sizeForString.width = appStringSize.width;
        sizeForString.height = appStringSize.height;
        sizeForString.numberOfLine = (int)(sizeForString.height/sizeString.height);
    }else {
        sizeForString.width = 0;
        sizeForString.height = 0;
        sizeForString.numberOfLine = 0;
    }

    return sizeForString;
}

我想我会尝试一下:

func heightForStringDrawing(myString: String, myFont:UIFont, myWidth:CGFloat) -> (CGFloat, Int) {
    var tc = NSTextContainer(size: CGSize(width: myWidth, height:CGFloat(FLT_MAX)))
    var ts = NSTextStorage(string: myString)
    var lm = NSLayoutManager()
    lm.addTextContainer(tc)
    ts.addLayoutManager(lm)
    ts.addAttribute(NSFontAttributeName, value: myFont, range: NSMakeRange(0, ts.length))
    tc.lineFragmentPadding = 0.0
    lm.glyphRangeForTextContainer(tc)
    let height = lm.usedRectForTextContainer(tc).height
    let lines = Int(Float(height / myFont.lineHeight))
    return (height, lines)
}
欢迎评论!我从以下方面采纳了这一点:

Interface Builder中的“行”设置控制最大行数

struct StringSize {

    int numberOfLine;
    float height;
    float width;

};

typedef struct StringSize StringSize;



+(StringSize)getStringSize:(NSString *)string ofWidth:(float)width  FontSize:(id)font {

    StringSize sizeForString;

    if(string){
        CGSize size = CGSizeMake(width, 400.0f);
        CGSize appStringSize = [string sizeWithFont:(UIFont *)font constrainedToSize:size     lineBreakMode:UILineBreakModeTailTruncation];
        CGSize sizeString = [string sizeWithFont:font];    

        sizeForString.width = appStringSize.width;
        sizeForString.height = appStringSize.height;
        sizeForString.numberOfLine = (int)(sizeForString.height/sizeString.height);
    }else {
        sizeForString.width = 0;
        sizeForString.height = 0;
        sizeForString.numberOfLine = 0;
    }

    return sizeForString;
}
检查Xcode界面生成器中Lines选项的工具提示:

行数-UILabel
numberOfLines

用于呈现文本的最大行数

这似乎对我很有效(使用自动布局):


这样做的目的是通过
较小的要求关系来限制高度。如果文本足够小,那么高度将由标签的
sizeToFit
自动定义。

如果numberOfLines不为零,那么sizeToFit将不起作用,我需要sizeToFit行为,因为如果不需要,它不应该总是三行高。我误解了你的问题,很抱歉在我的测试中,当numberOfLines=3时,sizeToFit工作正常。这可能是因为在我的应用程序中,每次文本更改时,我都会创建一个新标签,所以它不必缩小它。但是在这种情况下,它确实可以将标签扩展到与文本匹配所需的程度(最多三行),效果如预期。将边界更改为帧,并更改了线路制动模式。将其作为一个类别,请检查:
uilinebreakmodewrap
已被弃用。改用
NSLineBreakByWordWrapping
。嗨@debleek63请告诉我如何使用swift 4Oh。。。我意识到这也许不能具体回答他的问题,但它对任何遇到它的人都是有用的。