我们如何设置ImageView Like B形状

时间:2017-03-09 12:20:52

标签: ios iphone swift swift3

我想像这样创建一个imageView。请帮忙 。我是iOS新手。任何帮助将不胜感激。

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以将UIlabel中的Big B转换为Image

投票结束 公认 From:从UITextView或UILabel中提取UIImage会产生白色图像。

// iOS

- (UIImage *)grabImage {
    // Create a "canvas" (image context) to draw in.
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0.0);  // high res
    // Make the CALayer to draw in our "canvas".
    [[self layer] renderInContext: UIGraphicsGetCurrentContext()];

    // Fetch an UIImage of our "canvas".
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    // Stop the "canvas" from accepting any input.
    UIGraphicsEndImageContext();

    // Return the image.
    return image;
}

// Swift扩展w /用法。

extension UIImage {
    class func imageWithLabel(label: UILabel) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0.0)
        label.layer.renderInContext(UIGraphicsGetCurrentContext()!)
        let img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return img
    }
}

用法:

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 30, height: 22))
label.text = @"B"
let image = UIImage.imageWithLabel(label)