我需要在UIImage上使用圆形和文本,我已经使用文本实现了方形,但是无法使其成为圆形

时间:2017-07-12 06:25:08

标签: ios objective-c uiimage

我需要像这张图片一样在UIImage上进行圆形和文字 Sample Image here

这是我的代码,我已经实现但是方形不在圆形,我需要围绕文本。

+(UIImage*)drawText:(NSString*)text inImage:(UIImage*)image atPoint:(CGPoint)point
{

    UIFont *font = [UIFont boldSystemFontOfSize:50];
    UIGraphicsBeginImageContext(image.size);
    [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
    CGRect rect = CGRectMake(point.x - 10, point.y - 10, image.size.width, image.size.height);
    [text drawInRect:CGRectIntegral(rect) withAttributes:@{NSFontAttributeName:font,NSBackgroundColorAttributeName:[UIColor colorWithRed:0.26 green:0.32 blue:0.59 alpha:1.0],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

提前致谢。

3 个答案:

答案 0 :(得分:0)

做类似的事情:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.imgview.image = [ViewController drawText:@"iOS" inImage:[UIImage imageNamed:@"eye"] atPoint:CGPointMake(30 , 30)];
    self.imgview.layer.cornerRadius = self.imgview.frame.size.height/2;
    self.imgview.clipsToBounds = YES;

}


+ (UIImage*)drawText:(NSString*)text inImage:(UIImage*)image atPoint:(CGPoint)point
{
    UIFont *font = [UIFont boldSystemFontOfSize:20];
    UIGraphicsBeginImageContext(image.size);
    [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
    CGRect rect = CGRectMake(point.x - 10, point.y - 10, image.size.width, image.size.height);
    [text drawInRect:CGRectIntegral(rect) withAttributes:@{NSFontAttributeName:font,NSBackgroundColorAttributeName:[UIColor colorWithRed:0.26 green:0.32 blue:0.59 alpha:1.0],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

enter image description here

<强>更新

为此,您必须添加更多逻辑,如下所示。

- (void)viewDidLoad {
    [super viewDidLoad];
    self.imgview.image = [ViewController drawText:@"iOS Dev" inImage:[UIImage imageNamed:@"Lion"] bgCircleRect:CGRectMake(15, 15, 100, 100)];
}

+ (UIImage*)drawText:(NSString*)text inImage:(UIImage*)image bgCircleRect:(CGRect) bgCircleRect
{
    UIFont *font = [UIFont boldSystemFontOfSize:20];
    UIGraphicsBeginImageContext(image.size);
    [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];

    CGRect rect = CGRectMake(bgCircleRect.origin.x+10, bgCircleRect.origin.y+35, bgCircleRect.size.width, bgCircleRect.size.height);

    UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect: bgCircleRect cornerRadius: 50];
    [UIColor.redColor setFill];
    [path fill];

    [text drawInRect:CGRectIntegral(rect) withAttributes:@{NSFontAttributeName:font,NSBackgroundColorAttributeName:[UIColor redColor],NSForegroundColorAttributeName:[UIColor whiteColor]}];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

输出

enter image description here

答案 1 :(得分:0)

我认为你正在寻找角落半径:

+(void)drawText:(NSString*)text inImage:(UIImage*)image atPoint:(CGPoint)point{
UIImageView *imageView = [[UIImageView alloc] init];
UIFont *font = [UIFont boldSystemFontOfSize:50];
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
CGRect rect = CGRectMake(point.x - 10, point.y - 10, image.size.width, image.size.height);
[text drawInRect:CGRectIntegral(rect) withAttributes:@{NSFontAttributeName:font,NSBackgroundColorAttributeName:[UIColor colorWithRed:0.26 green:0.32 blue:0.59 alpha:1.0],NSForegroundColorAttributeName:[UIColor whiteColor]}];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageView.center = point;
imageView.image = newImage; }

将图像添加到imageView并将其作为subView添加到主视图

答案 2 :(得分:0)

为了使UIImageView成为圆形,首先检查长度和宽度是否相同,然后在viewDidLoad()中应用以下代码

yourpic.layer.cornerRadius = yourpic.frame.size.width / 2;
yourpic.clipsToBounds = YES;

我认为这样做。