Swift
来源:互联网 发布:应用更新软件 编辑:程序博客网 时间:2024/05/16 14:34
项目需求,文字要有描边效果,在这里记录下来。 核心是使用NSAttributedString进行绘制。 建议将此方法写在扩展中。另外还看到一些博主写的描边效果,其方式是继承UILabel,然后重载drawTextInRect方法,我改成了Swift,在下面也一并附上,有兴趣的可以看看
overridefunc viewDidLoad() { super.viewDidLoad() let lab =UILabel(frame:view.bounds) lab.attributedText =drawOutlineAttributedString(string:"哎呦,不错哦", fontFloat:30, alignment: .center, textColor:UIColor.white, widthColor:UIColor.red) view.addSubview(lab) } func drawOutlineAttributedString(string:String? =nil, fontFloat:CGFloat, alignment: NSTextAlignment, textColor:UIColor, widthColor: UIColor) ->NSAttributedString { let paragraph =NSMutableParagraphStyle() paragraph.alignment = alignment let dic = [NSFontAttributeName:UIFont.systemFont(ofSize: fontFloat), NSParagraphStyleAttributeName: paragraph, NSForegroundColorAttributeName: textColor, NSStrokeWidthAttributeName: (-1.5), NSStrokeColorAttributeName: widthColor]as [String :Any] var attributedText:NSMutableAttributedString! if string !=nil{ attributedText = NSMutableAttributedString.init(string: string!, attributes: dic) } else { attributedText = NSMutableAttributedString.init(string:"", attributes: dic) } return attributedText }
重载drawTextInRect方法
overridefunc drawText(in rect: CGRect) { let shadowOffset =self.shadowOffset let textColor =self.textColor let c =UIGraphicsGetCurrentContext() c!.setLineWidth(1) c!.setLineJoin(.round) c!.setTextDrawingMode(.stroke) self.textColor =UIColor.white super.drawText(in: rect) c!.setTextDrawingMode(.fill) self.textColor = textColor self.shadowOffset =CGSize(width: CGFloat(0), height:CGFloat(0)) super.drawText(in: rect) self.shadowOffset = shadowOffset }
效果如下:
阅读全文
0 0
- SWIFT
- swift
- Swift
- Swift
- swift
- Swift
- swift
- Swift
- swift
- swift
- swift
- Swift
- swift
- swift
- Swift
- swift
- swift
- swift ??
- PHP7.0新增加的特性
- 常用参考基类写法之Fragment
- 我与CSDN
- 获取文件相关消息
- Flask:Nginx + uWSGI + Supervisor 服务器部署
- Swift
- SpringBoot系列—基础配置
- 在phper中如何成为崛起最快的1%
- JS模块化工具requirejs教程(一):初识requirejs
- Android4.2到5.0 Phone的变化
- UnicodeDecodeError: 'gbk' codec can't decode byte 0xb7 in position 2069解决方法
- Unique Paths II
- 一道有趣的伪博弈题——三叉树
- Python Scrapy No module named _sqlite3