1. 程式人生 > >iOS 設定label動態高度

iOS 設定label動態高度

1、方法一


- (void)viewDidLoad {
    [super viewDidLoad];

    NSString * content = @"今年2月,中央政治局決定,黨的十八屆六中全會專題研究全面從嚴治黨問題,制定新形勢下黨內政治生活的若干準則,修訂《中國共產黨黨內監督條例(試行)》,成立檔案起草組,由我擔任組長,劉雲山、王岐山同志任副組長,有關部門和地方負責同志參加,在中央政治局常委會領導下進行工作。";

    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, self.view.frame
.size.width-20, 200)]; label.numberOfLines = 0; label.font = [UIFont systemFontOfSize:17]; label.textColor = [UIColor grayColor]; [label setAttributedText:[self setContent:content]]; [label sizeToFit]; [self.view addSubview:label]; } - (NSMutableAttributedString *)setContent:(NSString
*)content { NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:content]; NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init]; [paragraphStyle setLineSpacing:10]; //設定行間距 [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0
, [content length])]; return attributedString; }

2、方法二

這種適合設定列表cell中label的動態高度

#import "BusinessessCell.h"

@implementation BusinessessCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        _tilte = [UICommonCtrl commonLabelWithFrame:CGRectMake(10, 10, SCREEN_WIDTH-10, 20)
                                               text:@"張家界國家森林公園"
                                              color:kColor_Black
                                               font:FONTBOLD(17)
                                      textAlignment:NSTextAlignmentLeft];
        [self addSubview:_tilte];

        _content = [UICommonCtrl commonLabelWithFrame:CGRectMake(10, _tilte.bottom+10, SCREEN_WIDTH-20, 100)
                                                 text:@""
                                              color:kColor_LightGray
                                               font:kFont_Middle
                                      textAlignment:NSTextAlignmentLeft];
        _content.numberOfLines = 0;
        [self addSubview:_content];

        _bgImageView = [UICommonCtrl commonImageViewWithFrame:CGRectMake(10, _content.bottom+5, SCREEN_WIDTH-20, 230) image:nil];
        _bgImageView.contentMode = UIViewContentModeScaleAspectFill;
        _bgImageView.layer.masksToBounds = YES;
        [self addSubview:_bgImageView];


    }
    return self;
}

- (void)setDictInfo:(NSDictionary *)dictInfo
{
    _tilte.text = STR_OBJ(dictInfo[@"title"]);
    _content.text = STR_OBJ(dictInfo[@"content"]);
    _bgImageView.image = [UIImage imageNamed:STR_OBJ(dictInfo[@"image"])];

    CGFloat h = [BusinessessCell cellHeightWithInfo:dictInfo];

    _content.height = h-290;
    _bgImageView.top = _content.bottom+10;

    //設定行間距
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineSpacing = 5;
    NSDictionary *attributes = @{ NSFontAttributeName:kFont_Middle, NSParagraphStyleAttributeName:paragraphStyle};
    _content.attributedText = [[NSAttributedString alloc]initWithString:_content.text attributes:attributes];

}

+ (CGFloat)cellHeightWithInfo:(NSDictionary *)dictInfo
{
    NSString *contents = STR_OBJ([dictInfo objectForKey:@"content"]);

    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5]; //設定行間距

    NSDictionary *attributes = @{NSFontAttributeName:kFont_Middle, NSParagraphStyleAttributeName:paragraphStyle};

    CGRect rect = [contents boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-20, MAXFLOAT)
                                         options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                      attributes:attributes
                                         context:nil];

    return 290+rect.size.height;
}

@end