UILabel

作者: aaa000 | 来源:发表于2016-09-01 11:23 被阅读78次
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, self.view.bounds.size.width - 40, 40)];
        lab.text = @"label中的文字";//文本内容
        lab.textColor = [UIColor whiteColor];//文字颜色
        lab.backgroundColor = [UIColor orangeColor];//背景颜色
        lab.font = [UIFont systemFontOfSize:14];//字体大小 字样
        lab.textAlignment = NSTextAlignmentCenter;//文本对其方式
        lab.numberOfLines = 0;//设置显示多少行 0表示不限制
        lab.lineBreakMode = NSLineBreakByCharWrapping;//设置换行模式
        
        [self.view addSubview:lab];
        
        //设置圆角
        lab.layer.borderColor = [[UIColor greenColor] CGColor];
        lab.layer.cornerRadius = 8.0f;
        lab.clipsToBounds = YES;
        lab.layer.masksToBounds = YES;
    

    通过label中的 文字内容 字体大小 等属性来计算 label的宽高

    -(CGSize)autoSizeWithFontSize:(CGFloat)aSize text:(NSString *)theString{
        
        NSDictionary *attrs = @{NSFontAttributeName : [UIFont systemFontOfSize:aSize]};
        
        CGRect rect = [theString boundingRectWithSize:CGSizeMake(200, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil];
        
        return rect.size;
    }
    

    label 富文本实现各种各样的文字

        //label中出现多种颜色的和不同字体的文字
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String"];
        
        //给自定range 内的字符 设置属性
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,12)];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)];
        
        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:14.0] range:NSMakeRange(0, 5)];
        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:24.0] range:NSMakeRange(6, 12)];
        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:14.0] range:NSMakeRange(19, 6)];
        
        UILabel *label1 = [UILabel new];
        label1.frame = CGRectMake(10, 100, self.view.bounds.size.width - 20, 40);
        [self.view addSubview:label1];
        label1.backgroundColor = [UIColor lightGrayColor];
        label1.textAlignment = NSTextAlignmentCenter;
        
        //label 设置富文本
        label1.attributedText = str;
    

    eg: 电话号码

        UILabel *label1 = [UILabel new];
        label1.frame = CGRectMake(10, 180, self.view.bounds.size.width - 20, 40);
        [self.view addSubview:label1];
        label1.textAlignment = NSTextAlignmentCenter;
    
        //label中出现多种颜色的和不同字体的文字
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"xxx电话:18866669999"];
        //给自定range 内的字符 设置属性
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(6, 11)];
        [str addAttribute:NSUnderlineStyleAttributeName value:@1 range:NSMakeRange(6, 11)];
        [str addAttribute:NSUnderlineColorAttributeName value:[UIColor blueColor] range:NSMakeRange(6, 11)];
        [str addAttribute:NSObliquenessAttributeName value:@0.5 range:NSMakeRange(6, 11)];
        
        //label 设置富文本
        label1.attributedText = str;
    

    效果图


    屏幕快照 2016-09-01 上午11.03.08.png

    eg: 删除线

        UILabel *label1 = [UILabel new];
        label1.frame = CGRectMake(10, 180, self.view.bounds.size.width - 20, 80);
        [self.view addSubview:label1];
        label1.numberOfLines = 0;
        label1.textAlignment = NSTextAlignmentCenter;
    
         NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"hjkdf电话加咖啡的大家肯定会看见按快点交罚款了卡里的"];
        
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6, 5)];
        [str addAttribute:NSStrikethroughStyleAttributeName value:@1 range:NSMakeRange(6, 5)];
        [str addAttribute:NSStrikethroughColorAttributeName value:[UIColor redColor] range:NSMakeRange(6, 11)];
        //字体倾斜
        [str addAttribute:NSObliquenessAttributeName value:@0.2 range:NSMakeRange(6, 5)];
        
        //label 设置富文本
        label1.attributedText = str;
    

    效果图:

    屏幕快照 2016-09-01 上午11.09.46.png

    eg:图文混排

        UILabel *label1 = [UILabel new];
        label1.frame = CGRectMake(10, 180, self.view.bounds.size.width - 20, 80);
        [self.view addSubview:label1];
        label1.numberOfLines = 0;
        label1.textAlignment = NSTextAlignmentCenter;
    
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"先加一些文字"];
        // 创建图片图片附件
        NSTextAttachment *attach = [[NSTextAttachment alloc] init];
        attach.image = [UIImage imageNamed:@"fireFox"];
        attach.bounds = CGRectMake(0, -4, 20, 20);
        NSAttributedString *attachString = [NSAttributedString attributedStringWithAttachment:attach];
        [string appendAttributedString:attachString];
        
        [string appendAttributedString:[[NSAttributedString alloc] initWithString:@"火狐浏览器"]];
        //label 设置富文本
        label1.attributedText = string;
    

    效果图:


    屏幕快照 2016-09-01 上午11.22.38.png

    富文本的其他属性设置

    NSFontAttributeName                         设置字体属性,默认值:字体:Helvetica(Neue) 字号:12
    NSForegroundColorAttributeNam       设置字体颜色,取值为 UIColor对象,默认值为黑色
    NSBackgroundColorAttributeName     设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色
    NSLigatureAttributeName                   设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符
    NSKernAttributeName                        设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
    NSStrikethroughStyleAttributeName   设置删除线,取值为 NSNumber 对象(整数)
    NSStrikethroughColorAttributeName  设置删除线颜色,取值为 UIColor 对象,默认值为黑色
    NSUnderlineStyleAttributeName         设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似
    NSUnderlineColorAttributeName        设置下划线颜色,取值为 UIColor 对象,默认值为黑色
    NSStrokeWidthAttributeName           设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
    NSStrokeColorAttributeName           填充部分颜色,不是字体颜色,取值为 UIColor 对象NSShadowAttributeName 设置阴影属性,取值为 NSShadow 对象
    NSTextEffectAttributeName             设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用:
    NSBaselineOffsetAttributeName       设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
    NSObliquenessAttributeName         设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
    NSExpansionAttributeName            设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
    NSWritingDirectionAttributeName   设置文字书写方向,从左向右书写或者从右向左书写
    NSVerticalGlyphFormAttributeName  设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
    NSLinkAttributeName                       设置链接属性,点击后调用浏览器打开指定URL地址
    NSAttachmentAttributeName            设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
    NSParagraphStyleAttributeName       设置文本段落排版格式,取值为 NSParagraphStyle 对象
    

    相关文章

      网友评论

          本文标题:UILabel

          本文链接:https://www.haomeiwen.com/subject/bepcettx.html