如何创建以用户姓名首字母为中心的圆圈图像

时间:2018-09-26 17:34:13

标签: xamarin.ios

需要创建一个圆形图像,该图像的用户姓名缩写必须位于圆形的中心。类似于圈子图标在Gmail中的显示方式。任何第三方组件建议都很棒。

2 个答案:

答案 0 :(得分:1)

found some Swift code正是我所需要的。只需将其全部转换为C#。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using CoreGraphics;
using Foundation;
using UIKit;

public static class Extensions
{
    public static NSString ToNSString(this string text)
    {
        return new NSString(text);
    }

    public static void ToCircle(this UIImageView imageView, UIColor color, string text = null, double fontSize = 15.0, NSMutableDictionary<NSString, NSObject> customTextAttributes = null)
    {
        imageView.ToCustomFormat(color, true, text, fontSize, customTextAttributes);
    }

    public static void ToCustomFormat(this UIImageView imageView, UIColor color, bool isCircle, string text = null, double fontSize = 15.0, NSMutableDictionary<NSString, NSObject> customTextAttributes = null)
    {
        var scale = UIScreen.MainScreen.Scale;
        var imageSize = imageView.Bounds.Size;

        if (imageView.ContentMode == UIViewContentMode.ScaleToFill ||
            imageView.ContentMode == UIViewContentMode.ScaleAspectFill ||
            imageView.ContentMode == UIViewContentMode.ScaleAspectFit ||
            imageView.ContentMode == UIViewContentMode.Redraw)
        {
            imageSize.Width =  (nfloat) Math.Floor((imageSize.Width * scale) / scale);
            imageSize.Height = (nfloat) Math.Floor((imageSize.Height * scale) / scale);
        }

        UIGraphics.BeginImageContextWithOptions(imageSize, false, scale);
        var context = UIGraphics.GetCurrentContext();

        if (isCircle)
        {
            var path = CGPath.EllipseFromRect(imageView.Bounds);
            context?.AddPath(path);
            context?.Clip();
        }

        context?.SetFillColor(color.CGColor);
        var rect = new CGRect(x: 0, y: 0, width: imageSize.Width, height: imageSize.Height);
        context?.FillRect(rect);

        if (!string.IsNullOrEmpty(text))
        {                
            NSString nsText = text.ToNSString();
            UIStringAttributes textAttributes = customTextAttributes == null
                                                ? getDefaultTextAttributes(fontSize)
                                                : new UIStringAttributes(customTextAttributes);

            CGSize textSize = nsText.GetSizeUsingAttributes(textAttributes);
            var bounds = imageView.Bounds;
            var textRect = new CGRect(x: bounds.Size.Width / 2 - textSize.Width / 2, 
                                      y: bounds.Size.Height / 2 - textSize.Height / 2, 
                                      width: textSize.Width, 
                                      height: textSize.Height);

            nsText.DrawString(textRect, textAttributes);
        }

        var newImage = UIGraphics.GetImageFromCurrentImageContext();
        UIGraphics.EndImageContext();

        if (newImage != null)
        {
            imageView.Image = newImage;    
        }
    }

    private static UIStringAttributes getDefaultTextAttributes(double fontSize = 15.0)
    {
        var defaultAttrs = new NSMutableDictionary<NSString, NSObject>
                            {
                                {UIStringAttributeKey.ForegroundColor, UIColor.Black},
                                {UIStringAttributeKey.Font, UIFont.SystemFontOfSize((nfloat) fontSize)}
                            };

        return new UIStringAttributes(defaultAttrs);
    }
}

// Copyright (c) 2017 Paul-Anatole CLAUDOT - author of Swift code

答案 1 :(得分:0)

这真的很简单,我给了你一些而不是完整的代码。

  

UIView yourview = new UIView(new CGRect(0,0,100,100));

     

yourview.Layer.CornerRadius = 50f;

这将创建一个四舍五入的视图,然后您必须在框中居中添加首字母缩写

  

UILabel yourlabel = new UILabel(new CGRect(0,(100-40)/ 2,100,   40));

     

yourlabel.TextAlignment = UITextAlignment.Center;

     

yourlabel.Text =“ PG”;

yourview.add(您的标签);

这应该像您预期的那样工作。

对不起,您没有看到想要的图像,所以这是添加图像的部分

  

var img = new UIImageView(new CGRect(0,0,100,100));

     

img.ContentMode = UIViewContentMode.ScaleAspectFill;

     

img.ClipsToBounds = true;

     

img.Image = UIImage.FromBundle(“ image”);

     

yourview.Add(img);

您应该在标签之前添加图片