【问题标题】:How we set ImageView Like B shape我们如何设置 ImageView Like B 形状
【发布时间】:2017-03-09 12:20:52
【问题描述】:

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

【问题讨论】:

  • 在代码中绘制?或者使用绘图软件(如illustrator)并导入到项目中
  • 是的,我想通过代码创建
  • 您应该使用核心图形绘制路径。绘制它的轮廓,并将上下文导出到 uiimage。那里有很多教程
  • @Yitschak 先生,我找不到。请建议一些链接

标签: ios iphone swift swift3


【解决方案1】:

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

否决票 公认 来自:从 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 扩展。

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-15
    • 2014-05-01
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 2014-01-23
    • 1970-01-01
    • 2014-02-14
    相关资源
    最近更新 更多