2017-08-25 15 views
1

基本的には、任意のテキストを含む領域を画像に追加したいと考えています。その後、画像のサイズが大きくなります。画像に複数行のテキスト領域を追加する

  • テキストがトリミングされる(例えばfontSize = 160;):

    public partial class ViewController : UIViewController 
    { 
        public override void ViewDidLoad() 
        { 
         base.ViewDidLoad(); 
    
         string filename = "TestImage.jpg"; 
         string bundlePath = NSBundle.MainBundle.BundlePath; 
         string sourcePath = Path.Combine(bundlePath, filename); 
         string docPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); 
         string destinationPath = Path.Combine(docPath, filename); 
         File.Copy(sourcePath, destinationPath, true); 
    
         string testString = "Lorem ipsum dolor sit amet"; 
         this.AddTextToImage(testString, destinationPath); 
    
         var imageView = new UIImageView(new CGRect(0, 0, 500, 500)); 
         imageView.ContentMode = UIViewContentMode.ScaleAspectFit; 
         imageView.Image = UIImage.FromFile(destinationPath); 
         this.View.AddSubview(imageView); 
         this.View.BackgroundColor = UIColor.Red; 
        } 
    
    
        public void AddTextToImage(string texttoadd, string filepath) 
        { 
         UIImage image = UIImage.FromFile(filepath); 
         nfloat fontSize = 16; 
    
         nfloat fWidth = image.Size.Width; 
         nfloat fHeight = image.Size.Height; 
         nfloat textWidth; 
         nfloat textHeight; 
    
         CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB(); 
    
         UIFont font = UIFont.FromName("Helvetica", fontSize); 
         NSParagraphStyle style = new NSMutableParagraphStyle(); 
         style.LineBreakMode = UILineBreakMode.WordWrap; 
         NSAttributedString attributedString = new NSAttributedString(texttoadd, font: font, foregroundColor: UIColor.Blue, paragraphStyle: style); 
         CGRect stringSize = attributedString.GetBoundingRect(new CGSize(fWidth, double.MaxValue), NSStringDrawingOptions.UsesLineFragmentOrigin | NSStringDrawingOptions.UsesFontLeading, null); 
         textWidth = (nfloat)Math.Ceiling(stringSize.Width); 
         textHeight = (nfloat)Math.Ceiling(stringSize.Height); 
    
         nfloat fullWidth = fWidth; 
         nfloat fullHeight = fHeight + textHeight; 
         UIImage composition; 
    
         using (CGBitmapContext ctx = new CGBitmapContext(IntPtr.Zero, (nint)fullWidth, (nint)fullHeight, 8, 4 * (nint)fullWidth, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst)) 
         { 
          CGRect frameRect = new CGRect(0, 0, fullWidth, fullHeight); 
    
          ctx.SetFillColor(UIColor.Yellow.CGColor); 
          ctx.FillRect(frameRect); 
    
          CGRect imageRect = new CGRect(0, textHeight, (double)fWidth, (double)fHeight); 
          ctx.DrawImage(imageRect, image.CGImage); 
    
          CGPath stringPath = new CGPath(); 
          stringPath.AddRect(new CGRect(0, 0, textWidth, textHeight)); 
          CTFramesetter framesetter = new CTFramesetter(attributedString); 
          CTFrame frame = framesetter.GetFrame(new NSRange(0, attributedString.Length), stringPath, null); 
          frame.Draw(ctx); 
    
          using (var imageRef = ctx.ToImage()) 
           composition = new UIImage(imageRef); 
         } 
    
         NSData data = composition.AsJPEG(); 
         NSError error; 
         data.Save(filepath, NSDataWritingOptions.FileProtectionNone, out error); 
        } 
    } 
    

    現在、私は次の問題を持っている:私が思い付くたものです。複数行のテキストが機能していないようです。

  • テキストがまったく表示されません(例:fontSize = 16;)。

Objective-C、Swift、またはC#で回答を提供できます。翻訳しようとします。

答えて

1

フォントが問題だったようです。使用:

UIFont font = UIFont.SystemFontOfSize(fontSize); 

は、テキストを切り捨てずにジョブを実行します。残りの唯一の質問は、なぜですか?

関連する問題