美文网首页
Swift--Label高效切圆

Swift--Label高效切圆

作者: 嗯哼丶傻大个是你 | 来源:发表于2017-03-04 16:52 被阅读0次

    上代码:

    func circleImageWithString(string: String, frame: CGRect, textFont: CGFloat, backgroundColor: UIColor, textColor: UIColor) -> UIImage {
      let titleLabel = UILabel(frame: CGRectMake(0,0,frame.size.width * 3,frame.size.height * 3))
      titleLabel.backgroundColor = UIColor.blueColor()
      titleLabel.text = string
      titleLabel.font = UIFont.systemFontOfSize(textFont * 3)
      titleLabel.backgroundColor = backgroundColor
      titleLabel.textAlignment = .Center
      titleLabel.textColor = textColor
      UIGraphicsBeginImageContext(titleLabel.frame.size)
      let context: CGContextRef = UIGraphicsGetCurrentContext()!
      CGContextSetLineWidth(context, 0)
      CGContextSetStrokeColorWithColor(context, UIColor.redColor().CGColor)
      let rect = CGRectMake(0, 0, titleLabel.frame.size.width, titleLabel.frame.size.height)
      CGContextAddEllipseInRect(context, rect)
      CGContextClip(context)
      titleLabel.layer.drawInContext(context)
      CGContextAddEllipseInRect(context, rect)
      CGContextStrokePath(context)
      let newImage = UIGraphicsGetImageFromCurrentImageContext()
      UIGraphicsEndImageContext()
      return newImage
    }
    

    相关文章

      网友评论

          本文标题:Swift--Label高效切圆

          本文链接:https://www.haomeiwen.com/subject/wytoittx.html