iOS绘制字符串

2020-11-15  本文已影响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];
    
}
上一篇 下一篇

猜你喜欢

热点阅读