UILabel自適應高度和自動換行- OC swift

        加粗;swift

        [UILabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];app

        加粗而且傾斜測試

        [UILabel setFont:[UIFont fontWithName:@"Helvetica-BoldOblique" size:20]];字體

        guessLikeLabel.font = UIFont(name: "Helvetica-Bold", size: 14)spa


//初始化labelcode

    UILabel *label = [[UILabel alloc] init]; orm

    NSString *text = @"這是一個測試!!!adsfsaf時發生發勿忘我勿忘我勿忘我勿忘我勿忘我阿阿阿阿阿阿阿阿阿阿阿阿阿啊00000000阿什頓。。。"ci

    label.text = text;string

    [label setNumberOfLines:0];  it

    UIFont *font = [UIFont fontWithName:@"Arial" size:14];

    //設置字體

    label.font = font;

    CGSize constraint = CGSizeMake(30020000.0f);

    CGSize size = [text sizeWithFont:font constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

    [label setFrame:CGRectMake(100, size.width, size.height)];

    [self.view addSubview:label];




iOS7 UILabel 內容自適應 - OBJC

2014年08月29日 


//iOS7放棄使用「 CGSize actualsize = [tstring sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByCharWrapping]; 」
    
    //基本設置
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
    NSString *text = @"測試:阿啊阿啊阿啊阿啊阿啊阿啊阿啊阿啊阿啊阿啊阿啊阿啊阿啊阿asdafdddddaaaaaaaaajhkjkgghghghghghghghghghgghghhghghghghghghg啊阿啊阿啊阿啊阿啊阿啊";
    label.text = text;
    UIFont *textFont = [UIFont systemFontOfSize:14.f];
    label.font = textFont;
    
    //折行
    label.lineBreakMode = NSLineBreakByWordWrapping;
    
    //必須寫,不然只顯示一行
    [label setNumberOfLines:0];
    [self.view addSubview:label];
    
    //最大尺寸
    // MAXFLOAT 爲可設置的最大高度
    CGSize size = CGSizeMake(300, MAXFLOAT);
    
    //獲取當前那本屬性
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:textFont,NSFontAttributeName, nil];
    //實際尺寸
    CGSize actualSize = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    
    label.frame = CGRectMake(10, 70, actualSize.width,actualSize.height);






//下面是本身在項目中寫的, 當時出錯了,由於在cellForItemAtIndexPath是先初使化cell,而後在賦值,所在初使化(cell的init中是拿不到_detailText,因此_detailTextLabel.text也是沒有值的,不能計算frame

        UIFont *textFont = [UIFont systemFontOfSize:12.f];

        _detailTextLabel.numberOfLines = 0;//設置無限換行

        _detailTextLabel.lineBreakMode = NSLineBreakByCharWrapping;  //自動折行

        //CGSize size  = [_detailTextLabel.text boundingRectWithSize:_detailTextLabel.frame options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:nil context:nil];

        //設置最大尺寸 10000.0( MAXFLOAT )爲可設置的最大高度

        CGSize constraint = CGSizeMake([[UIScreen mainScreen] bounds].size.width-20, 10000.0);

        //獲取當前文本屬性

        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:textFont,NSFontAttributeName, nil];

        //實際尺寸

        CGSize actualSize = [_detailTextLabel.text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;

        _detailTextLabel.frame = CGRectMake(10,  _topicImageView.frame.size.height+60+1+15, actualSize.width, actualSize.height);



 UILabel 內容自適應 - swift

        var detailTextLabel = UILabel()

        dtext = detailTextLabel.text

        detailTextLabel.font= UIFont.systemFontOfSize(12)

       
        //numberOfLines //標籤最多顯示行數,若是爲0則表示多行 即設置無限換行

        detailTextLabel.numberOfLines = 0         

        detailTextLabel.lineBreakMode = NSLineBreakMode.ByCharWrapping  //自動折行


        //根據detailText文字長度調節topView高度

        let constraint = CGSize(width: topView.frame.size.width,      height: 10000.0)

        var size =dtext.boundingRectWithSize(constraint, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: NSDictionary(object:UIFont.systemFontOfSize(12), forKey: NSFontAttributeName) as [NSObject : AnyObject] ,context: nil)

        detailTextLabel.frame = CGRectMake(10,  topicImageView.frame.size.height+60+1+15, size.width, size.height)

相關文章
相關標籤/搜索