中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

iOS-自適應(yīng)總結(jié)

2018-07-20    來源:編程學(xué)習(xí)網(wǎng)

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用

UIView方法之SizeToFit

作用: 計算出最優(yōu)size, 并且改變UIView的size

Demo1: 高度不變,寬度隨文本大小變化而變化

設(shè)置字號為13,使用SizeToFit自適應(yīng)結(jié)果為

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 100, 40)];
    label.backgroundColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:13];
    label.text = @"天青色等煙雨\n而我在等你\n炊煙裊裊升起\n隔江千萬里";
    [self.view addSubview:label];
    [label sizeToFit];
    NSLog(@"frame = %@", NSStringFromCGRect(label.frame));

UILabel打印結(jié)果為:

frame = {{20, 100}, {307, 16}}

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 100, 100, 40)];
    btn.backgroundColor = [UIColor whiteColor];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    [btn setTitle:@"天青色等煙雨\n而我在等你\n炊煙裊裊升起\n隔江千萬里" forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn sizeToFit];
    NSLog(@"frame = %@", NSStringFromCGRect(btn.frame));

UIButton打印結(jié)果為:

frame = {{20, 100}, {540, 28}}

UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 100, 40)];
    field.backgroundColor = [UIColor whiteColor];
    field.text = @"天青色等煙雨\n而我在等你\n炊煙裊裊升起\n隔江千萬里";
    field.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:field];
    [field sizeToFit];
    NSLog(@"frame = %@", NSStringFromCGRect(field.frame));

UITextField打印結(jié)果為:

frame = {{20, 100}, {544, 16}}

此時三個控件都是單行模式下,高度不變,寬度隨文本大小變化而變化.

但UILabel是有設(shè)置多行功能的,所以UILabel和UIButton均可設(shè)置多行,請看Demo2

Demo2: 寬度不變,高度隨文本大小變化而變化

設(shè)置寬度為100,UILabel的numberOfLines大于1行,假如為3行

若自適應(yīng)后的寬度小于100,則寬度小于100,高為一行

若是自適應(yīng)后的寬度大于100,則寬度為100,若高度大于3行,則高度為3行,剩余內(nèi)容不顯示,若高度小于三行,則高度為計算出的高度

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 100, 40)];
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:13];
    label.numberOfLines = 3;
    label.text = @"天青色等煙雨\n而我在等你\n炊煙裊裊升起\n隔江千萬里";
    [self.view addSubview:label];
    [label sizeToFit];
    NSLog(@"frame = %@", NSStringFromCGRect(label.frame));

UIlabel打印結(jié)果為:

frame = {{20, 100}, {97, 47}}

Demo3: 寬度不變,高度隨文本大小變化而變化

設(shè)置寬度為100,UILabel的numberOfLines為0行

若自適應(yīng)后的寬度小于100,則寬度小于100,高為一行

若是自適應(yīng)后的寬度大于100,則寬度為100,高度為自適應(yīng)后的高度

注意:高度自適應(yīng)時,寬度不可設(shè)置為0,若為0,則變成了單行寬度自適應(yīng)

但剛剛自適應(yīng)算出來的坐標(biāo),上下左右均緊貼著Label的邊,不美觀,可做如下修飾

label.width += 10;
label.height += 10;

UIView方法之sizeThatFits

作用: 計算出最優(yōu)size, 但是不會改變UIView的size

用法: 將sizeThatFits的寬高設(shè)置較大點,會在這個范圍內(nèi)自動計算出最匹配寬高

若計算的寬度小于設(shè)置的寬度,則以計算出來的高度為準(zhǔn)

若計算的寬度大于設(shè)置的寬度,則以設(shè)置的寬度去進行高度自適應(yīng)

注意:若要自適應(yīng),要重設(shè)Label坐標(biāo)

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 100, 20)];
    label.backgroundColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:13];
    label.numberOfLines = 0;
    label.text = @"天青色等煙雨\n而我在等你\n炊煙裊裊升起\n隔江千萬里";
    [self.view addSubview:label];
    CGSize sizeThat = [label sizeThatFits:CGSizeMake(1000, 1000)];
    label.frame = CGRectMake(20, 100, sizeThat.width, sizeThat.height);
    NSLog(@"frame = %@", NSStringFromCGSize(sizeThat));

UIlabel打印結(jié)果為:(因為有換行,所以計算出)

frame = {80, 62.5}

NSString方法之boundingRectWithSize

_boundingRectWithSizeLabel.numberOfLines = 0;
    _boundingRectWithSizeLabel.font = [UIFont systemFontOfSize:18];

    NSString *strText = @"這是一段很長的文字,你需要計算這個高度到底是多少";
    _boundingRectWithSizeLabel.text = strText;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObject:[UIFont systemFontOfSize:18] forKey:NSFontAttributeName];
    CGSize size = [strText boundingRectWithSize:CGSizeMake(100, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    _boundingRectWithSizeLabel.frame = CGRectMake(15, _h5TextView.bottom + 30, size.width, size.height);

NSAttributedString方法之boundingRectWithSize

_boundingRectWithSizeAttributedLabel.numberOfLines = 0;
    _boundingRectWithSizeAttributedLabel.font = [UIFont systemFontOfSize:13];

    NSString *h5boundingRectWithSizeString = @"<p>天青色等煙雨,而我在等你,炊煙裊裊升起,隔江千萬里,在瓶底書漢隸仿前朝的飄逸,就當(dāng)我為遇見你伏筆,天青色等煙雨,而我在等你,月色被打撈起,暈開了結(jié)局,如傳世的青花瓷自顧自美麗,你眼帶笑意,色白花青的錦鯉躍然於碗底,臨摹宋體落款時卻惦記著你,你隱藏在窯燒里千年的秘密,極細(xì)膩猶如繡花針落地,簾外芭蕉惹驟雨,門環(huán)惹銅綠,而我路過那江南小鎮(zhèn)惹了你,在潑墨山水畫里,你從墨色深處被隱去</p>";
    NSMutableAttributedString *h5boundingRectWithSizeAttributedString = [[NSMutableAttributedString alloc] initWithData:[h5boundingRectWithSizeString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    _boundingRectWithSizeAttributedLabel.attributedText = h5boundingRectWithSizeAttributedString;

    CGSize size1 = [h5boundingRectWithSizeAttributedString boundingRectWithSize:CGSizeMake(100, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil].size;
    _boundingRectWithSizeAttributedLabel.frame = CGRectMake(15, _boundingRectWithSizeLabel.bottom + 30, size1.width, size1.height);

 

來自:http://www.jianshu.com/p/ce26f05cd7cc

 

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:Python 異步調(diào)用命令行工具

下一篇:深入了解正則表達式