Swift4之NSAttributedString的使用
来源:互联网 发布:免费外汇交易软件 编辑:程序博客网 时间:2024/06/01 09:03
Swift4富文本使用
NSAttributeString的优点
1.主要用于控制控件的文本属性。
2.有些文本属性并没有暴露出来。
3.一条语句代码(属性字典)可以设置多个属性。
关于NSMutableAttributedString,可以用来设置分段文本属性,比如一段文本有10个字,可以前5个字设置为一个属性样式,后5个字又设置为其他的样式
swift3使用NSAtrributedString
let attributedString = NSAttributedString(string: "富文本测试", attributes: [ NSBackgroundColorAttributeName : UIColor.red,NSForegroundColorAttributeName : UIColor.green])
Swift4版本
let attributeString = NSAttributedString(string: "富文本测试", attributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 13.5)])
//注意:Swift使用NSAttributedStringKey去掉相关属性。所以在使用的使用需要有点小变化,主要属性关键字还是差不多。
文本属性关键字如下:
swift3版本示例
//设置字体let NSFontAttributeName: String // UIFont, default Helvetica(Neue) 12//设置段落样式let NSParagraphStyleAttributeName: String // NSParagraphStyle, default defaultParagraphStyle//设置前景色(字体色),如果设置字体线条色,则无效,字体内部为字体背景色let NSForegroundColorAttributeName: String // UIColor, default blackColor
Swift4使用NSAttributedStringKey.点关键字示例
extension NSAttributedStringKey { /************************ Attributes ************************/ @available(iOS 6.0, *) public static let font: NSAttributedStringKey @available(iOS 6.0, *) public static let paragraphStyle: NSAttributedStringKey // NSParagraphStyle, default defaultParagraphStyle @available(iOS 6.0, *) public static let foregroundColor: NSAttributedStringKey // UIColor, default blackColor
注意:如果设置字体线条色,且描边宽度为正值(中空形式)则无效,显示为字体背景色
这里虽然我设置的字体色为绿色,但是设置了描边色,且宽度为正值,并没有起效果,
同样的,如果描边宽度设置为负值(-3),即正常显示了。
//字体背景色,与控件背景色不同public let NSBackgroundColorAttributeName: String // UIColor, default nil: no background
//连字符,反正我不懂干嘛的
public let NSLigatureAttributeName: String // NSNumber containing integer, default 1: default ligatures, 0: no ligatures
//字间距,正值加宽,负值变窄public let NSKernAttributeName: String // NSNumber containing floating point value, in points; amount to modify default kerning. 0 means kerning is disabled.//删除线 ,值可以使用整数(0-7:单线,值越大,线越粗,7-15:双线,同理)或者枚举(参考下面枚举定义)public let NSStrikethroughStyleAttributeName: String // NSNumber containing integer, default 0: no strikethrough//下划线, 值同删除线public let NSUnderlineStyleAttributeName: String // NSNumber containing integer, default 0: no underline//字体边框颜色,设置此颜色public let NSStrokeColorAttributeName: String // UIColor, default nil: same as foreground color//字体变框宽度,如果设置了边框色,需要设置这个颜色,否则看不到,【重点:值可以为正值(中空形式,字体色为字体背景色)和负值(填充形式:设置字体颜色可以显示)】public let NSStrokeWidthAttributeName: String // NSNumber containing floating point value, in percent of font point size, default 0: no stroke; positive for stroke alone, negative for stroke and fill (a typical value for outlined text would be 3.0)//字体阴影,public let NSShadowAttributeName: String // NSShadow, default nil: no shadow//文本特殊效果,只有图文排版印刷使用public let NSTextEffectAttributeName: String // NSString, default nil: no text effect//图文混排public let NSAttachmentAttributeName: String // NSTextAttachment, default nilpublic let NSLinkAttributeName: String // NSURL (preferred) or NSString//基线偏移,正值向上,负值向下public let NSBaselineOffsetAttributeName: String // NSNumber containing floating point value, in points; offset from baseline, default 0//底线的颜色public let NSUnderlineColorAttributeName: String // UIColor, default nil: same as foreground color//删除线颜色public let NSStrikethroughColorAttributeName: String // UIColor, default nil: same as foreground color//字体倾斜度,正值左倾,负值右倾public let NSObliquenessAttributeName: String // NSNumber containing floating point value; skew to be applied to glyphs, default 0: no skew//文本横线拉伸,正值拉伸,负值压缩public let NSExpansionAttributeName: String // NSNumber containing floating point value;
//字体书写方向,从左到右,与从右到左,值参考下面public let NSWritingDirectionAttributeName: String // NSArray of NSNumbers representing the nested levels of writing direction overrides as defined by Unicode LRE, RLE, LRO, and RLO characters.字体书写方向值:
The control characters can be obtained by masking NSWritingDirection and NSWritingDirectionFormatType values.
LRE: NSWritingDirectionLeftToRight|NSWritingDirectionEmbedding,
RLE: NSWritingDirectionRightToLeft|NSWritingDirectionEmbedding,
LRO: NSWritingDirectionLeftToRight|NSWritingDirectionOverride,
RLO: NSWritingDirectionRightToLeft|NSWritingDirectionOverride,
//文字横竖排版 值为0或1,0横排,1竖排,目前ios只支持横排。public let NSVerticalGlyphFormAttributeName: String
如何实现多种复合文本组合
1.可以使用设置range
2.通过NSAttributedMutableString 的append方法组合具体使用参考:OC[传送门] 传送门2
阅读全文
0 0
- Swift4之NSAttributedString的使用
- NSAttributedString的使用
- NSAttributedString的使用
- <iOS开发>之 NSAttributedString使用
- NSAttributedString和NSMutableAttributedString的使用
- 初探NSAttributedString和NSMutableAttributedString的使用
- 文本属性的简单使用--NSAttributedString
- NSAttributedString文本属性设置的使用
- 苹果faceid使用swift4
- Swift4
- Swift4
- Swift4
- Swift4
- swift4
- IOS之NSAttributedString
- NSAttributedString使用介绍
- swift4初探之网络请求
- TextKit/富文本/图层混排模式的使用(一)NSAttributedString
- 1039. Course List for Student (25)
- 图片预加载的方法
- vb.net 教程 12-3 HtmlElement类 3
- 深度理解Jquery 中 offset() 方法
- 【程序】Marvell 88W8782/88W8801 WiFi模块的固件下载代码
- Swift4之NSAttributedString的使用
- Python初入门(四)(Head First Python 第四章 数据保存到文件)
- 11:图像旋转
- 方法重写和方法重载
- Linux shell工作原理详细分析
- 在Pypi上发布自己的Python包
- 4.HTML表格
- (转)非极大抑制(Non-Maximum Suppression)
- H5+css+js一个简单的图书管理系统