alertas alerta ios iphone uiimageview

ios - alerta - uialertcontroller swift 4



¿Cómo escribir texto en una imagen en Objective-C(iOS)? (13)

¡Hice algo como esto! Después de navegar y combinar algunos ejemplos.

Coloca el texto en la mitad de la imagen, cambiando el tamaño de la fuente si es necesario.

UIImage *myImage = [UIImage imageNamed:@"promoicon.png"]; UIGraphicsBeginImageContext(myImage.size); [myImage drawInRect:CGRectMake(0,0,myImage.size.width,myImage.size.height)]; UITextView *myText = [[UITextView alloc] init]; myText.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:15.0f]; myText.textColor = [UIColor whiteColor]; myText.text = NSLocalizedString(@"promotionImageText", @""); myText.backgroundColor = [UIColor clearColor]; CGSize maximumLabelSize = CGSizeMake(myImage.size.width,myImage.size.height); CGSize expectedLabelSize = [myText.text sizeWithFont:myText.font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap]; myText.frame = CGRectMake((myImage.size.width / 2) - (expectedLabelSize.width / 2), (myImage.size.height / 2) - (expectedLabelSize.height / 2), myImage.size.width, myImage.size.height); [[UIColor whiteColor] set]; [myText.text drawInRect:myText.frame withFont:myText.font]; UIImage *myNewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();

Quiero hacer una imagen como esta programáticamente:

Tengo la imagen superior y el texto conmigo. ¿Debo escribir texto en la imagen?

Quiero convertirlo en una imagen .png completa (imagen + etiqueta) y establecerlo como fondo del botón.


Aquí está la versión Swift.

func textToImage(drawText: NSString, inImage: UIImage, atPoint:CGPoint)->UIImage{ // Setup the font specific variables var textColor: UIColor = UIColor.whiteColor() var textFont: UIFont = UIFont(name: "Helvetica Bold", size: 12)! //Setup the image context using the passed image. UIGraphicsBeginImageContext(inImage.size) //Setups up the font attributes that will be later used to dictate how the text should be drawn let textFontAttributes = [ NSFontAttributeName: textFont, NSForegroundColorAttributeName: textColor, ] //Put the image into a rectangle as large as the original image. inImage.drawInRect(CGRectMake(0, 0, inImage.size.width, inImage.size.height)) // Creating a point within the space that is as bit as the image. var rect: CGRect = CGRectMake(atPoint.x, atPoint.y, inImage.size.width, inImage.size.height) //Now Draw the text into an image. drawText.drawInRect(rect, withAttributes: textFontAttributes) // Create a new image out of the images we have created var newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() // End the context now that we have the image we need UIGraphicsEndImageContext() //And pass it back up to the caller. return newImage }

Para llamarlo, solo debes pasar una imagen.

textToImage("000", inImage: UIImage(named:"thisImage.png")!, atPoint: CGPointMake(20, 20))

Los siguientes enlaces me ayudaron a entenderlo.

Swift - Dibujando texto con drawInRect: withAttributes:

¿Cómo escribir texto en una imagen en Objective-C (iOS)?

El objetivo original era crear una imagen dinámica que pudiera usar en un AnnotaionView, como poner un precio en una ubicación determinada en un mapa, y esto funcionó muy bien. Espero que esto ayude a alguien a tratar de hacer lo mismo.


Aquí hay una versión de Swift que centra correctamente el texto en la imagen. Esto funciona con texto de varios tamaños.

func addTextToImage(text: NSString, inImage: UIImage, atPoint:CGPoint) -> UIImage{ // Setup the font specific variables let textColor = YOURCOLOR let textFont = YOUR SIZE //Setups up the font attributes that will be later used to dictate how the text should be drawn let textFontAttributes = [ NSFontAttributeName: textFont, NSForegroundColorAttributeName: textColor, ] // Create bitmap based graphics context UIGraphicsBeginImageContextWithOptions(inImage.size, false, 0.0) //Put the image into a rectangle as large as the original image. inImage.drawInRect(CGRectMake(0, 0, inImage.size.width, inImage.size.height)) // Our drawing bounds let drawingBounds = CGRectMake(0.0, 0.0, inImage.size.width, inImage.size.height) let textSize = text.sizeWithAttributes([NSFontAttributeName:textFont]) let textRect = CGRectMake(drawingBounds.size.width/2 - textSize.width/2, drawingBounds.size.height/2 - textSize.height/2, textSize.width, textSize.height) text.drawInRect(textRect, withAttributes: textFontAttributes) // Get the image from the graphics context let newImag = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImag }


Dibujar texto dentro de una imagen y devolver la imagen resultante:

+(UIImage*) drawText:(NSString*) text inImage:(UIImage*) image atPoint:(CGPoint) point { UIFont *font = [UIFont boldSystemFontOfSize:12]; UIGraphicsBeginImageContext(image.size); [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)]; CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height); [[UIColor whiteColor] set]; [text drawInRect:CGRectIntegral(rect) withFont:font]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; }

Uso:

// note: replace "ImageUtils" with the class where you pasted the method above UIImage *img = [ImageUtils drawText:@"Some text" inImage:img atPoint:CGPointMake(0, 0)];

Cambie el origen del texto dentro de la imagen de 0,0 al punto que desee.

Para pintar un rectángulo de color sólido detrás del texto, agregue lo siguiente antes de la línea [[UIColor whiteColor] set]; :

[[UIColor brownColor] set]; CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, (image.size.height-[text sizeWithFont:font].height), image.size.width, image.size.height));

Estoy usando el tamaño del texto para calcular el origen del rectángulo de color sólido, pero puedes reemplazarlo por cualquier número.


En Swift-3 de @Jano Respuesta: -

func drawText(text:NSString ,image:UIImage ,point:CGPoint ) -> UIImage { let font = UIFont.boldSystemFont(ofSize: 12) UIGraphicsBeginImageContext(image.size) image.draw(in:CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height) ) let rect = CGRect(x: point.x, y: point.y, width:image.size.width, height: image.size.height ) UIColor.white.set() text.draw(in: rect.integral, withAttributes: [NSFontAttributeName : font]) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image! }


He creado extensiones totalmente personalizadas para UIImage para dibujar marcas de agua en Swift:

extension UIImage{ enum WaterMarkCorner{ case TopLeft case TopRight case BottomLeft case BottomRight } func waterMarkedImage(#waterMarkText:String, corner:WaterMarkCorner = .BottomRight, margin:CGPoint = CGPoint(x: 20, y: 20), waterMarkTextColor:UIColor = UIColor.whiteColor(), waterMarkTextFont:UIFont = UIFont.systemFontOfSize(20), backgroundColor:UIColor = UIColor.clearColor()) -> UIImage{ let textAttributes = [NSForegroundColorAttributeName:waterMarkTextColor, NSFontAttributeName:waterMarkTextFont] let textSize = NSString(string: waterMarkText).sizeWithAttributes(textAttributes) var textFrame = CGRectMake(0, 0, textSize.width, textSize.height) let imageSize = self.size switch corner{ case .TopLeft: textFrame.origin = margin case .TopRight: textFrame.origin = CGPoint(x: imageSize.width - textSize.width - margin.x, y: margin.y) case .BottomLeft: textFrame.origin = CGPoint(x: margin.x, y: imageSize.height - textSize.height - margin.y) case .BottomRight: textFrame.origin = CGPoint(x: imageSize.width - textSize.width - margin.x, y: imageSize.height - textSize.height - margin.y) } /// Start creating the image with water mark UIGraphicsBeginImageContext(imageSize) self.drawInRect(CGRectMake(0, 0, imageSize.width, imageSize.height)) NSString(string: waterMarkText).drawInRect(textFrame, withAttributes: textAttributes) let waterMarkedImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return waterMarkedImage } }

Como puede ver, he agregado algunos valores predeterminados para los atributos para que pueda ignorarlos si no necesita cambiarlos. Aquí algunos ejemplos de cómo usarlo:

let watermark1 = image.waterMarkedImage(waterMarkText: "@yourapp") let watermark2 = image.waterMarkedImage(waterMarkText: "your app name", corner: .TopRight, margin: CGPoint(x: 5, y: 5), waterMarkTextColor: UIColor.greenColor()) let watermark3 = image.waterMarkedImage(waterMarkText: "appName", waterMarkTextColor: UIColor.blackColor(), waterMarkTextFont: UIFont(name: "Helvatica", size: 25)!)


La versión Swift 3 de @Hossam Ghareebs responde y agregó la integración faltante del parámetro backgroundColor:

enum WaterMarkCorner{ case TopLeft case TopRight case BottomLeft case BottomRight } extension UIImage{ func waterMarkedImage(_ waterMarkText:String, corner:WaterMarkCorner = .TopRight, margin:CGPoint = CGPoint(x: 20, y: 20), waterMarkTextColor:UIColor = UIColor.black, waterMarkTextFont:UIFont = UIFont.systemFont(ofSize: 40), backgroundColor:UIColor = UIColor(white: 1.0, alpha: 0.5)) -> UIImage?{ let textAttributes = [NSForegroundColorAttributeName:waterMarkTextColor, NSFontAttributeName:waterMarkTextFont, NSBackgroundColorAttributeName: backgroundColor] let textSize = NSString(string: waterMarkText).size(attributes: textAttributes) var textFrame = CGRect(x:0, y:0, width:textSize.width, height:textSize.height) let imageSize = self.size switch corner{ case .TopLeft: textFrame.origin = margin case .TopRight: textFrame.origin = CGPoint(x: imageSize.width - textSize.width - margin.x, y: margin.y) case .BottomLeft: textFrame.origin = CGPoint(x: margin.x, y: imageSize.height - textSize.height - margin.y) case .BottomRight: textFrame.origin = CGPoint(x: imageSize.width - textSize.width - margin.x, y: imageSize.height - textSize.height - margin.y) } /// Start creating the image with water mark UIGraphicsBeginImageContext(imageSize) self.draw(in: CGRect(x:0, y:0, width:imageSize.width, height:imageSize.height)) NSString(string: waterMarkText).draw(in: textFrame, withAttributes: textAttributes) let waterMarkedImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return waterMarkedImage } }


Mi contribución a la primera respuesta con soporte para iOS 7:

+(UIImage*) drawText:(NSString*) text inImage:(UIImage*) image atPoint:(CGPoint) point { UIGraphicsBeginImageContextWithOptions(image.size, YES, 0.0f); [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)]; CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height); [[UIColor whiteColor] set]; UIFont *font = [UIFont boldSystemFontOfSize:12]; if([text respondsToSelector:@selector(drawInRect:withAttributes:)]) { //iOS 7 NSDictionary *att = @{NSFontAttributeName:font}; [text drawInRect:rect withAttributes:att]; } else { //legacy support [text drawInRect:CGRectIntegral(rect) withFont:font]; } UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; }

Espero que esto ayude

EDITAR: modificó el UIGraphicsBeginImageContextWithOptions para manejar la escala de pantalla. Thk @SoftDesigner


Swift 3

extension UIImage { func textToImage(drawText: NSString, atPoint:CGPoint) -> UIImage? { // Setup the font specific variables let textColor: UIColor = UIColor.white let textFont: UIFont = UIFont(name: "Helvetica Bold", size: 12)! //Setup the image context using the passed image. UIGraphicsBeginImageContext(self.size) //Setups up the font attributes that will be later used to dictate how the text should be drawn let textFontAttributes = [ NSFontAttributeName: textFont, NSForegroundColorAttributeName: textColor, ] as [String : Any] //Put the image into a rectangle as large as the original image. self.draw(in: CGRect(x:0, y:0, width:self.size.width, height: self.size.height)) // Creating a point within the space that is as bit as the image. let rect: CGRect = CGRect(x:atPoint.x, y:atPoint.y, width:self.size.width, height:self.size.height) //Now Draw the text into an image. drawText.draw(in: rect, withAttributes: textFontAttributes) // Create a new image out of the images we have created let newImage: UIImage? = UIGraphicsGetImageFromCurrentImageContext() // End the context now that we have the image we need UIGraphicsEndImageContext() //And pass it back up to the caller. return newImage } }


Teniendo en cuenta el rendimiento, debe evitar tener frecuentes llamadas a -drawRect :. Cada UIView está respaldado con un CALayer e imágenes, ya que el contenido de la capa permanece en la memoria siempre que el CALayer permanezca en la jerarquía. Esto significa que la mayoría de las operaciones que ve en una aplicación incluyen movimiento, rotación y escalado de la vista / capa , no necesita un redibujado. Esto significa que puede usar agregar un CATextLayer en UIImageView, si no necesita una imagen con marca de agua. https://developer.apple.com/library/ios/qa/qa1708/_index.html

- (void)addText:(NSString *)text inImageView:(UIImageView *)imageView { CATextLayer *textLayer = [CATextLayer layer]; UIFont *font = [UIFont systemFontOfSize:14.0f]; CGSize textSize = [text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0f]}]; textLayer.frame = CGRectMake((imageView.size.width - textSize.width)/2, (imageView.size.height - textSize.height)/2, textSize.width, textSize.height);; textLayer.string = text; textLayer.fontSize = font.pointSize; [imageView.layer addSublayer:textLayer]; }


Use este método para agregar su campo de texto en la imagen con la fuente, el color y el tamaño seleccionados

//Method to add - (UIImage *) addText:(UIImage *)img text:(NSString *)text { CGRect rect = CGRectMake(0,0, img.size.width, img.size.height); // create a context according to image size UIGraphicsBeginImageContext(rect.size); // draw image [img drawInRect:rect]; float fontSize = _txtvwEdit.font.pointSize*2; NSLog(@"Original %f new %f",_txtvwEdit.font.pointSize,fontSize); UIFont* font = [UIFont fontWithName:_txtvwEdit.font.fontName size:fontSize]; CGRect textRect = CGRectMake((_txtvwEdit.frame.origin.x*2)-5,_txtvwEdit.frame.origin.y*2,_txtvwEdit.frame.size.width*2,_txtvwEdit.frame.size.height*2); if ([temparyGifframes count]>0) { font = [UIFont fontWithName:_txtvwEdit.font.fontName size:_txtvwEdit.font.pointSize]; textRect = CGRectMake(_txtvwEdit.frame.origin.x,_txtvwEdit.frame.origin.y ,_txtvwEdit.frame.size.width,_txtvwEdit.frame.size.height); } /// Make a copy of the default paragraph style NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping; paragraphStyle.alignment = NSTextAlignmentLeft; NSDictionary *attributes = @{ NSFontAttributeName: font, NSForegroundColorAttributeName: _txtvwEdit.textColor,NSParagraphStyleAttributeName: paragraphStyle }; // draw text [text drawInRect:textRect withAttributes:attributes]; // get as image UIImage * image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }


iOS7 solo.

Marca de agua en la esquina inferior derecha.

@interface UIImage (DrawWatermarkText) -(UIImage*)drawWatermarkText:(NSString*)text; @end @implementation UIImage (DrawWatermarkText) -(UIImage*)drawWatermarkText:(NSString*)text { UIColor *textColor = [UIColor colorWithWhite:0.5 alpha:1.0]; UIFont *font = [UIFont systemFontOfSize:50]; CGFloat paddingX = 20.f; CGFloat paddingY = 20.f; // Compute rect to draw the text inside CGSize imageSize = self.size; NSDictionary *attr = @{NSForegroundColorAttributeName: textColor, NSFontAttributeName: font}; CGSize textSize = [text sizeWithAttributes:attr]; CGRect textRect = CGRectMake(imageSize.width - textSize.width - paddingX, imageSize.height - textSize.height - paddingY, textSize.width, textSize.height); // Create the image UIGraphicsBeginImageContext(imageSize); [self drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)]; [text drawInRect:CGRectIntegral(textRect) withAttributes:attr]; UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return resultImage; } @end

Uso:

UIImage *image = [UIImage imageNamed:@"mona_lisa"]; image = [image drawWatermarkText:@"Leonardo da Vinci"];


UIImageView *imageView = [UIImageView alloc]; imageView.image = [UIImage imageNamed:@"img.png"]; UILabel *label = [UILabel alloc]; label.text = @"Your text"; [imageView addsubview:label];

Establezca el marco de la etiqueta donde desea que se muestre la etiqueta.