我在这个论坛上发帖是为了获得帮助,我正在 Swift 上为 iPhone 编写一个应用程序,我创建了一个文本视图来向最终用户展示一些信息,但我已经将图片作为附件包含在内,这里的问题是我不'不知道如何圆化该附件图片的所有角。下面是我的代码:
// Outlet created:
@IBOutlet weak var informacionTextView: UITextView!
// Image Creation.
// center Paragraph.
let imageStyle = NSMutableParagraphStyle()
imageStyle.alignment = .center
// create attachment attrStringWithImage
let textImage = NSTextAttachment()
textImage.image = UIImage(named: "iconapp.png")!
let attrStringWithImage = NSAttributedString(attachment: textImage).mutableCopy() as! NSMutableAttributedString
// Resize
textImage.bounds = CGRectMake(0, 0, 111, 111)
// Adding Center.
let length2 = attrStringWithImage.length
attrStringWithImage.addAttribute(NSAttributedString.Key.paragraphStyle, value: imageStyle, range: NSRange(location: 0, length: length2))
// Var creation to display.
let informacionCompleta = NSMutableAttributedString()
informacionCompleta.append(attrStringWithImage)
informacionTextView.attributedText = informacionCompleta
您能分享一下让图标应用程序以圆角显示在文本视图上的想法吗?
我尝试使用一些代码,但总是失败。
我期待得到帮助。
这是一个圆角 UIImages 的扩展。
extension UIImage {
func roundedImage(withCornerRadius cornerRadius: CGFloat) -> UIImage? {
let rect = CGRect(origin: .zero, size: self.size)
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius).addClip()
self.draw(in: rect)
let roundedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return roundedImage
}
}
使用:
textImage.image = UIImage(named: "iconapp.png")!.roundedImage(withCornerRadius: 10)