最近整理了一下CoreText里面的相关知识点,现在刚看到NSAttributedString,也从网上查到了一些资料,不过总感觉有很多文章只是对于Apple Document的翻译,对于初学者来说,可能更想要知道落实到代码是什么?每一个属性又是做什么的。废话不多少,发车了!
一:源代码
github地址:NSAttributedString。喜欢的麻烦给颗星🙃
二:属性列表
NSAttributedString属性列表如下:
属性名称 | 意义 |
---|---|
font | 字体 |
paragraphStyle | 段落设置,默认是:defaultParagraphStyle |
foregroundColor | 文字颜色,默认是黑色的 |
backgroundColor | 背景颜色,默认是没有背景颜色 |
ligature | 网上说是连字功能,默认是关闭的,我试了一下,没啥用,可能没有看到可以连字的字母吧 |
kern | 字符间距,0表示字符间距不可用 |
strikethroughStyle | 删除线的类型,只有0(无删除线)1(有删除线)两种样式 |
underlineStyle | 下划线样式0(没有下划线),1(有下划线) |
strokeColor | 文字的描边颜色,默认是没有描边颜色,也就是背景颜色 |
strokeWidth | 文字的描边宽度,默认是0,没有描边 |
shadow | 阴影,默认是没有阴影的 |
textEffect | 文字效果,默认没有文字效果 |
attachment | 附件 |
link | 链接 |
baselineOffset | 基线的偏移量 |
underlineColor | 下划线颜色,默认是nil,背景颜色 |
strikethroughColor | 删除线颜色,默认是nil,背景颜色 |
obliqueness | 字体倾斜度,默认是没有倾斜度 |
expansion | 字体的伸缩,小于零是缩小,大于零是扩张 |
writingDirection | 字体的方向,有LRE, RLE, LRO, 和RLO几种模式,现在还不知道怎么赋值。。。。。( ˇˍˇ ) |
verticalGlyphForm | 文字的排版方向 |
三:效果示意图和代码实现
再说一遍,先把上面的demo下载下来:NSAttributedString,下面的代码都在demo的代码里面
3.1:font
字体设定具体实现代码如下:
//代码位置:BoldSystemFontViewController.swift
func refreshLabel(font:UIFont){
let dict = [NSAttributedStringKey.font:font]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.2:paragraphStyle
paragraphStyle//代码位置:ParagraphStyleViewController.swift
func refreshLabel(index:Int){
let style = NSMutableParagraphStyle.init()
switch index {
case 0:
style.lineSpacing = 20.0
break
case 1:
style.paragraphSpacing = 20.0
break
case 2:
style.alignment = .right
break
case 3:
style.firstLineHeadIndent = 20.0
break
case 4:
style.headIndent = 20.0
break
case 5:
style.tailIndent = 20.0
break
case 6:
style.lineBreakMode = .byWordWrapping
break
case 7:
style.minimumLineHeight = 10
break
case 8:
style.maximumLineHeight = 20
break
case 9:
style.baseWritingDirection = .rightToLeft
break
case 10:
style.lineHeightMultiple = 3.0
break
case 11:
style.paragraphSpacingBefore = 20.0
break
case 12:
style.hyphenationFactor = 20.0
break
case 13:
let decimalTab = NSTextTab.init(textAlignment: .right, location: 5, options: [:])
let percentTab = NSTextTab.init(textAlignment: .left, location: 20, options: [:])
style.tabStops = [decimalTab,percentTab]
break
case 14:
style.defaultTabInterval = .infinity*2
break
case 15:
style.allowsDefaultTighteningForTruncation = true
break
case 16:
let decimalTab = NSTextTab.init(textAlignment: .right, location: 5, options: [:])
style.addTabStop(decimalTab)
break
case 17:
let decimalTab = NSTextTab.init(textAlignment: .right, location: 5, options: [:])
style.removeTabStop(decimalTab)
break
case 18:
let newStyle = NSMutableParagraphStyle.init()
newStyle.lineSpacing = 30
style.setParagraphStyle(newStyle)
break
default:
break
}
let dict = [NSAttributedStringKey.paragraphStyle:style]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.3:ForeGroundColor
ForeGroundColor//代码位置:ForegroundColorViewController.swift
func refreshLabel(color:UIColor){
let dict =
[NSAttributedStringKey.foregroundColor:color]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.4:backgroundColor
backgroundColor//BackgroundColorViewController.swift
func refreshLabel(color:UIColor){
let dict = [NSAttributedStringKey.backgroundColor:color]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.5:ligature
ligature//代码位置:LigatureViewController.swift
func refreshLabel(index:Int){
let dict = [NSAttributedStringKey.ligature:index]
let attrStr = NSMutableAttributedString.init(string: self.titleStr + pinyin, attributes: dict)
label.attributedText = attrStr
}
3.6:kern
kern//KernViewController.swift
func refreshLabel(index:Int){
let dict = [NSAttributedStringKey.kern:index]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.7:strikethroughStyle
strikethroughStyle//代码位置:StrikethroughStyleViewController.swift
func refreshLabel(index:Int){
let dict = [NSAttributedStringKey.strikethroughStyle:index]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.8:UnderlineStyle
UnderlineStyle//UnderlineStyleViewController.swift
func refreshLabel(index:Int){
let dict = [NSAttributedStringKey.underlineStyle:index]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.9:StrokeColor
StrokeColor//代码位置:StrokeColorViewController.swift
func refreshLabel(color:UIColor){
let dict = [NSAttributedStringKey.strokeColor:color,NSAttributedStringKey.strokeWidth:1] as [NSAttributedStringKey : Any]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.10:StrokeWidth
StrokeWidth//代码位置:StrokeWidthViewController.swift
func refreshLabel(width:CGFloat){
let dict = [NSAttributedStringKey.strokeColor:UIColor.red,NSAttributedStringKey.strokeWidth:width] as [NSAttributedStringKey : Any]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.11:shadow
shadow//代码位置:ShadowViewController.swift
func refreshLabel(){
let red = CGFloat(arc4random_uniform(255))/CGFloat(255.0)
let green = CGFloat( arc4random_uniform(255))/CGFloat(255.0)
let blue = CGFloat(arc4random_uniform(255))/CGFloat(255.0)
let alpha = CGFloat(arc4random_uniform(255))/CGFloat(255.0)
let color = UIColor.init(red:red, green:green, blue:blue , alpha: alpha)
let shadowBlurRadius = arc4random_uniform(5)
let offsetX = arc4random_uniform(2)
let offsetY = arc4random_uniform(5)
let shadow = NSShadow.init()
shadow.shadowOffset = CGSize(width: CGFloat(offsetX), height:CGFloat(offsetY))
shadow.shadowBlurRadius = CGFloat(shadowBlurRadius)
shadow.shadowColor = color
let dict = [NSAttributedStringKey.shadow:shadow]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.12:textEffect
textEffect感觉这个操作有点卡。。。等有时间了看看咋回事
//代码位置:TextEffectViewController.swift
func refreshLabel(){
let dict = [NSAttributedStringKey.textEffect:NSAttributedString.TextEffectStyle.letterpressStyle]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.13:Attachment
Attachment//代码位置:AttachmentViewController.swift
func refreshLabel(index:Int){
let attach = NSTextAttachment.init()
switch index {
case 0:
attach.contents = Data.init(base64Encoded: "zhangfangtao", options: .ignoreUnknownCharacters)
break
case 1:
attach.fileType = "txt"
break
case 2:
attach.image = UIImage.init(named: "icon")
break
case 3:
attach.bounds = CGRect(x: 0, y: 0, width: 50, height: 50)
break
case 4:
let filewrapper = FileWrapper.init(symbolicLinkWithDestinationURL: URL.init(string: "www.baidu.com")!)
attach.fileWrapper = filewrapper
default:
break
}
attach.bounds = CGRect(x: 0, y: 0, width: 50, height: 50)
let imageStr = NSAttributedString.init(attachment: attach)
let attrStr = NSMutableAttributedString.init(string: self.titleStr)
attrStr.insert(imageStr, at: 6)
label.attributedText = attrStr
}
3.14:Link
Link//代码位置:LinkViewController.swift
func refreshLabel(){
let dict = [NSAttributedStringKey.link:URL.init(string: "www.baidu.com")]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.15:BaselineOffset
BaselineOffset//代码位置:BaselineOffsetViewController.swift
func refreshLabel(index:Int){
let dict = [NSAttributedStringKey.baselineOffset:self.offsetArray[index]]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.16:UnderlineColor
UnderlineColor//代码位置:UnderlineColorViewController.swift
func refreshLabel(color:UIColor){
let dict = [NSAttributedStringKey.underlineStyle:1,NSAttributedStringKey.underlineColor:color] as [NSAttributedStringKey : Any]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.17:StrikethroughColor
StrikethroughColor//代码位置:StrikethroughColorViewController.swift
func refreshLabel(color:UIColor){
let dict = [NSAttributedStringKey.strikethroughStyle:1,NSAttributedStringKey.strikethroughColor:color] as [NSAttributedStringKey : Any]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.18:ObliquenessView
ObliquenessView//代码位置:ObliquenessViewController.swift
func refreshLabel(index:Float){
let dict = [NSAttributedStringKey.obliqueness:index]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.19:ExpansionView
ExpansionView//代码位置:ExpansionViewController.swift
func refreshLabel(index:Float){
let dict = [NSAttributedStringKey.expansion:index]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.20:WritingDirectionView
WritingDirectionView这个还有些小问题,近期也没时间找答案了,怎么赋值?麻烦知道的人留言告诉一下呗。
//代码位置:WritingDirectionViewController.swift
func refreshLabel(index:NSNumber){
let alertController = UIAlertController.init(title: "这个我目前还不知道怎么设置", message: "正在查询相关的资料", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel, handler: nil)
let okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.default, handler: nil)
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
return
let dict = [NSAttributedStringKey.writingDirection:index]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.21:VerticalGlyphForm
VerticalGlyphForm //代码位置:VerticalGlyphFormViewController.swift
func refreshLabel(index:Float){
let dict = [NSAttributedStringKey.verticalGlyphForm:index]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
写在最后
时间有限,很多细节还没有接触到,如果有入门想了解的可以看看。源代码github地址:NSAttributedString。喜欢的麻烦给颗星🙃。
网友评论
github地址:https://github.com/hon-key/Nudeln