iOS绘制字符串和图片

绘制文字可以用 drawAtPoint,drawInRect方法
这两个方法的调用一定写在drawRect方法内,因为方法内部使用 CGContextRef

- (void)drawRect:(CGRect)rect
{
    NSString *str = @"这是一段聊天记录,内容长度不一定,也许很长,也许很短,这怎么定高度呢?聊天内容又变长了,这次高度是否够用呢?这是一段聊天记录,内容长度不一定,也许很长,也许很短,这怎么定高度呢?聊天内容又变长了,这次高度是否够用呢?";

    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:18];
    attrs[NSForegroundColorAttributeName] = [UIColor redColor];

    //[str drawAtPoint:CGPointMake(30, 100) withAttributes:attrs];

    // 能够根据字符串的内容,在指定宽度的情况下,算出文字宽高
    // 刚好能够装下所有文字的高度
    CGRect strRect = [str boundingRectWithSize:CGSizeMake(200, 998) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil];

    // 画在指定的矩形区域内
    [str drawInRect:CGRectMake(30, 100,strRect.size.width,strRect.size.height) withAttributes:attrs];

}

效果

绘制聊天气泡

- (void)drawRect:(CGRect)rect
{
    self.message = @"这是一段聊天记录,文字尽量不要太多阿卡就是的房间按税法就是了回复大幅度;卡了;是否放假;卢卡斯房间卡上发动机";
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
    attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];

    // 1.计算出文字的宽高
    CGSize textSize = [self.message boundingRectWithSize:CGSizeMake(200, 998) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;

    // 2.画底色--灰色的气泡
    CGRect popRect = CGRectZero;
    popRect.origin.x = self.bounds.size.width - 40 - textSize.width;
    popRect.origin.y = 10;
    popRect.size.width = textSize.width + 20;
    popRect.size.height = textSize.height + 20;

    UIBezierPath *rectPath = [UIBezierPath bezierPathWithRoundedRect:popRect cornerRadius:10];
    [[UIColor lightGrayColor] setFill];
    [rectPath fill];

    // 3.绘制三角形
    CGPoint startP = CGPointMake(self.bounds.size.width - 20, popRect.size.height);

    UIBezierPath *triPath = [UIBezierPath bezierPath];
    [triPath moveToPoint:startP];
    [triPath addLineToPoint:CGPointMake(startP.x+10, startP.y+10)];
    [triPath addLineToPoint:CGPointMake(startP.x-10, startP.y+10)];
    [triPath closePath];
    [triPath fill];

    // 4.绘制字符串
    [self.message drawInRect:CGRectMake(popRect.origin.x+10, popRect.origin.y+10, textSize.width, textSize.height) withAttributes:attrs];
}

绘制图片

- (void)drawRect:(CGRect)rect
{
    // 记录一个圆形的位置
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 120, 120)];
    // 沿着此路径,设置圆之外的部分为绘图无效区
    [path addClip];

    UIImage *image = [UIImage imageNamed:@"aaa"];
    [image drawAtPoint:CGPointMake(100, 100)];

    // 打水印
    NSString *s = @"X";

    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = [UIColor redColor];
    attrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:35];

    [s drawAtPoint:CGPointMake(147, 158) withAttributes:attrs];

    //UIImage *image2 = [UIImage imageNamed:@"icon60"];
    //[image2 drawAtPoint:CGPointMake(100, 220)];

    //[image2 drawInRect:CGRectMake(50, 50, 300, 100)];
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值