iOS-图像上的文字叠加

我们如何在iOS的令人惊叹的图像上覆盖文本?

这基本上是所有基于图形的应用程序所必需的,例如海报制作,创建徽标,在图像上生成水印,并且还有助于重叠两个图像。

以下是一个演示,通过该演示,您可以更好地了解iOS中图像上的文本覆盖:

步骤1建立XCode专案

为此,您必须创建一个xCode项目,将其命名为Sketch_demo并将其保存到特定项目。 该项目将包含一个View Controller,这是我们的主要View Controller。

步骤2 DesignUI

要在图像上重叠文本,您需要在主视图控制器中使用一个UIImageView并将其设置为任意图像并将其模式设置为Aspect Fit。 因此,您的图像不会拉伸。

第3步创建标签并添加手势

在主视图控制器的viewDidLoad()中编写以下方法。

void )viewDidLoad 
  { 
  [ super viewDidLoad]; 
  [imageview setImage:[UIImage imageNamed:@” darken.png”]]; 
  UITextView * sktechLabel = [[UITextView alloc] initWithFrame: CGRectMake50,50,250,40 )]; 
  sktechLabel.text = @“让我喜欢一个新的方式,这是我拥有的一个好方法”; 
  sktechLabel.textColor = [UIColor whiteColor]; 
  sktechLabel.delegate =自我; 
  sktechLabel.font = [UIFont fontWithName:@“ Marker Felt” size:35]; 
  [imageview addSubview:sktechLabel]; 
  [imageview setUserInteractionEnabled:YES]; 
  [sktechLabel setUserInteractionEnabled:YES]; 
  UIPanGestureRecognizer * panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@ 选择器 (handlePanGesture :)]; 
  [panGesture setMinimumNumberOfTouches:1]; 
  [panGesture setMaximumNumberOfTouches:1]; 
  [sktechLabel addGestureRecognizer:panGesture]; 
  panGesture =零; 
  } 

在上面的代码中,我们向UIImageView添加了一个标签,该标签已经在我们的主视图控制器中可用,向其添加了平移手势,并为此定义了适当的选择器方法。

您必须将用户交互设置为同时定义的UIImageView和UILabel都启用。

步骤4处理手势

平移手势的选择器方法按以下定义,它允许您在图像周围移动标签。

void )handlePanGesture:(id)sender 
  { 
  CGPoint translationPoint = [(UIPanGestureRecognizer *)发送者translationInView:self.view]; 
  如果 ([[UIPanGestureRecognizer *)发件人状态] == UIGestureRecognizerStateBegan) 
  { 
  firstX = [[发件人视图]中心] .x; 
  firstY = [[发件人视图]中心] .y; 
  } 
  translationPoint = CGPointMake (firstX + translatedPoint.x,firstY + translatedPoint.y); 
  [[发件人视图] setCenter:translatedPoint]; 
  } 

步骤5在手势上创建图像

向主视图控制器添加一个按钮,以处理与图像重叠的文本的捕获。 当单击按钮时,带有重叠文本的图像将另存为相册中的单个图像。

以下方法将为此处理按钮单击事件:

  (IBAction)btnCaptureImageClicked:(id)sender 
  { 
  [self createImage:imageView]; 
  } 
  -(UIImage *)createImage:(UIImageView *)imageView 
  { 
  UIGraphicsBeginImageContextWithOptions (imageView.bounds.size,NO,0); 
  CGContextRef context = UIGraphicsGetCurrentContext (); 
  [imageView.layer renderInContext:context]; 
  UIImage * images = UIGraphicsGetImageFromCurrentImageContext (); 
  UIGraphicsEndImageContext (); 
  UIImageWriteToSavedPhotosAlbum (images,nil,nil,nil); 
  返回图像; 
  } 

如果您有任何与iOS中图像叠加文字相关的查询,请在下面对其进行评论,我们将尝试解决它们。