UILabel设置背景色/阴影/描边

作者: Michael_NO1 | 来源:发表于2017-11-22 11:40 被阅读1104次

效果图:


QQ20171122-113317.png

通过NSMutableAttributedString设置UILabel中文字的背景色:

//设置字体背景颜色
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]init];
    NSString *str1 = @"背景色";
    NSDictionary *dictAttr1 = @{NSBackgroundColorAttributeName:[UIColor cyanColor]};
    NSAttributedString *attr1 = [[NSAttributedString alloc]initWithString:str1 attributes:dictAttr1];
    [attributedString appendAttributedString:attr1];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 50)];
    label.textColor = [UIColor blueColor];
    label.attributedText = attributedString;
    [self.view addSubview:label];

设置阴影有两种方法:
方法一:

// 通过layer层设置阴影
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(100, 150, 200, 50)];
    label2.text = @"layer层设置阴影";
    label2.textColor = [UIColor blueColor];
    
    label2.layer.shadowColor = [UIColor blackColor].CGColor;
    label2.layer.shadowOffset = CGSizeMake(0, 0);
    label2.layer.shadowOpacity = 1;
    [self.view addSubview:label2];

方法二:

// 通过富文本设置阴影
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowBlurRadius = 10.0;
    shadow.shadowOffset = CGSizeMake(0, 0);
    shadow.shadowColor = [UIColor blackColor];
    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:@"富文本设置阴影" attributes:@{NSShadowAttributeName:shadow}];
    
    UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 200, 50)];
    label3.textColor = [UIColor blueColor];
    label3.attributedText = attributedText;
    [self.view addSubview:label3];

描边需要继承自UILabel以后重载- (void)drawRect:(CGRect)rect方法:

- (void)drawRect:(CGRect)rect {
    
    CGSize shadowOffset = self.shadowOffset;
    UIColor *textColor = self.textColor;
    
    CGContextRef ref = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(ref, 1);
    CGContextSetLineJoin(ref, kCGLineJoinRound);
    
    CGContextSetTextDrawingMode(ref, kCGTextStroke);
    self.textColor = [UIColor redColor];
    [super drawRect:rect];
    
    CGContextSetTextDrawingMode(ref, kCGTextFill);
    self.textColor = textColor;
    self.shadowOffset = CGSizeMake(0,0);
    [super drawRect:rect];
    
    self.shadowOffset = shadowOffset;
}

描边的使用:

// 描边
    HYZLabel *label4 = [[HYZLabel alloc] initWithFrame:CGRectMake(100, 250, 200, 50)];
    label4.text = @"描边啊描边";
    label4.textColor = [UIColor blueColor];
    label4.shadowOffset = CGSizeMake(0, 0);
    [self.view addSubview:label4];

相关文章

  • UILabel设置背景色/阴影/描边

    效果图: 通过NSMutableAttributedString设置UILabel中文字的背景色: 设置阴影有两种...

  • 一定要学的ps小技巧(持续更新)

    小技巧: 1:裁剪时,跟随背景色。(需选中删除裁剪的像素) 2:fx在做边框的时候可以描边与阴影 3:编辑---描...

  • iOS 基础之UIlabel

    标签UILabel UILabel常见的属性 1.shadowColor属性:设置阴影颜色 。2.shadowOf...

  • UILabel 文字描边

    titleLabel.shadowColor= [UIColor blackColor]; titleLabel....

  • 1、基本控件

    0、创建HelloWorld工程,在ViewController.m文件 文本控件UILabel 在//设置背景色...

  • Canvas入门到高级详解(中)

    三、 canvas 进阶 3.1 Canvas 颜色样式和阴影 3.1.1 设置填充和描边的颜色(掌握) fill...

  • swift:view的阴影的实现

    阴影:先设置背景色,不能maskToBounds bgView.backgroundColor = UIColor...

  • box-shadow 效果大全

    box-shadow 效果大全(内阴影,外阴影,三边阴影,双边阴影,单边阴影,细线描边…)CSS3 box-sha...

  • ios 开发速成版

    UIView 效果图 UILabel 继承UIView 一样可以设置背景色、圆角等等和UIView相比UILabe...

  • NGUI字体描边

    NGUI的UILabel中实现字体的描边是通过以方形的方式对字体网格顶点偏移一定位置后作为其描边网格。以这种方式描...

网友评论

    本文标题:UILabel设置背景色/阴影/描边

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