ios學習day3

來源:互聯網
上載者:User

標籤:

UIlabel

UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(10,30, CGRectGetWidth(self.view.frame) -20, 200)];

    //設定文字

    label.text = @"歡迎收看灌籃高手,我是安溪教練";

    //設定文字顏色

    label.textColor = [UIColor grayColor];

    //預設17號字型

    label.font = [UIFont systemFontOfSize:34];

    //對齊

    label.textAlignment = NSTextAlignmentCenter;

    //設定陰影

    label.shadowColor = [UIColor redColor];

    //設定陰影映射大小,預設CGSizeMake(0, -1)

    label.shadowOffset = CGSizeMake(-2, -2);

    //斷句,展示縮減

    label.lineBreakMode = NSLineBreakByTruncatingTail;

    //行數0為自動匹配行數

    label.numberOfLines = 1;

    //字型是否適應寬度(Size無效)

    label.adjustsFontSizeToFitWidth = YES;

    //最小字型比例

//    label.minimumScaleFactor = 0.9;

UIfont

輸出編譯器所有的字型

for (NSString * familyName in [UIFont familyNames]) {

        NSLog(@"%@",familyName);

        NSArray * familys = [UIFont fontNamesForFamilyName:familyName];

        for (NSString * fontName in familys) {

            NSLog(@"%@",fontName);

        }

    }

具體實現方法

//斜體

        //[UIFont italicSystemFontOfSize:50];

        //字型加粗

        //[UIFont boldSystemFontOfSize:50];

        //設定系統字型

        //[UIFont systemFontOfSize:10];

        //[UIFont fontWithName:@"Heiti TC" size:25];

富文本

段落設定

NSMutableParagraphStyle * paragraph = [[NSMutableParagraphStyle alloc] init];

    paragraph.lineSpacing = 10;//行間距

    paragraph.paragraphSpacing = 50;//段間距

    paragraph.firstLineHeadIndent = 50;//第一段空幾格

富文本實現

 NSDictionary * dictA = @{NSFontAttributeName:[UIFont systemFontOfSize:20],

                             NSForegroundColorAttributeName:[UIColor greenColor],//前景色彩

                             NSBackgroundColorAttributeName:[UIColor grayColor],//後景色

                             NSParagraphStyleAttributeName:paragraph

//                             NSObliquenessAttributeName:@0.5 //斜體

//                             NSStrokeColorAttributeName:[UIColor whiteColor],

//                             NSStrokeWidthAttributeName:@2,//描邊

//                             NSKernAttributeName:@20,//字間距

//                             NSStrikethroughStyleAttributeName:@2,//刪除線

//                             NSUnderlineStyleAttributeName:@1,//底線

                             };

    

    NSAttributedString * attribute = [[NSAttributedString alloc] initWithString:exampleString attributes:dictA];

可變富文本

NSMutableAttributedString * astring = [[NSMutableAttributedString alloc] initWithString:string];

 

 

 

 

//可變富文本

    NSMutableAttributedString * mutableAttribute = [[NSMutableAttributedString alloc] initWithString:exmapleString];

    

    NSRange range1 = {0,3};

    NSRange range2 = {4,5};

    [mutableAttribute addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:50] range:range1];

    

    NSDictionary * dict = @{NSForegroundColorAttributeName:[UIColor magentaColor],

                            NSBackgroundColorAttributeName:[UIColor cyanColor]};

    

    [mutableAttribute addAttributes:dict range:range2];

 

追加子符串

NSAttributedString * string2 = [[NSAttributedString alloc] initWithString:@"好噠!" attributes:dict2];

    

    [astring appendAttributedString:string2];

計算label的高度

//一定要先確定寬度,再根據寬度和字型計算size—多行顯示

    CGSize size = [string boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;

單行顯示

NSString * string = @"黑貓警長";

    NSDictionary * dict = @{NSFontAttributeName:[UIFont systemFontOfSize:50]};

    CGSize size = [string sizeWithAttributes:dict];

label的自適應-多行

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

    label.text = string;//先給字串,再計算它的size

    label.numberOfLines = 0;

    label.backgroundColor = [UIColor greenColor];

    CGSize size = [label sizeThatFits:CGSizeMake(self.view.frame.size.width - 40, MAXFLOAT)];

    label.frame = CGRectMake(20, 50, size.width, size.height);

    [self.view addSubview:label];

 

 

UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, self.view.frame.size.width - 40, 0)];

    label.text = string;

    label.numberOfLines = 0;

    label.backgroundColor = [UIColor greenColor];

    //使用sizeToFit一定要設定寬度

    [label sizeToFit];

    [self.view addSubview:label];

 

ios學習day3

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.