美文网首页
iOS绘制字符串

iOS绘制字符串

作者: 我不白先生 | 来源:发表于2020-11-15 15:19 被阅读0次

DrawView.m

@implementation DrawView

- (void)drawRect:(CGRect)rect {
    NSString *str = @"今天是第二阶段最后一天,明天我们仍需努力,加油!!!!!!!!!!!";
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    //字是黄色的
    dic[NSForegroundColorAttributeName]  =[UIColor yellowColor];
    //背景是蓝色的
    dic[NSBackgroundColorAttributeName] =[UIColor blueColor];
    //字号是20号
    dic[NSFontAttributeName] = [UIFont systemFontOfSize:20];
   //[str drawAtPoint:CGPointMake(100, 100) withAttributes:dic];
    //动态的知道字符串所需要的高度(确定宽度文本会自动换行,返回的矩形中的高度就是字符串实际的高度)
      CGRect textFrame = [str boundingRectWithSize:CGSizeMake(200, 999) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 200, textFrame.size.height)];
    [[UIColor greenColor]setFill];
    [path fill];
    [str drawInRect:CGRectMake(50, 50, 200, textFrame.size.height) withAttributes:dic];
    
}

相关文章

网友评论

      本文标题:iOS绘制字符串

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