属性 X 0.1

属性 X 0.1

-description= 维护。



  • strangeliu

属性 X

Swift Carthage compatible

为构建强类型 AttributedString 提供超级甜美的语法糖。

概览

使用 CountableClosedRange 来设置属性

let mAttString = NSMutableAttributedString(string: "AttributeX")
mAttString[0...9].foregroundColor = UIColor.gray
mAttString[0...9].underlineStyle = .styleSingle
mAttString[0...3].underlineColor = UIColor.orange
mAttString[3...9].underlineColor = UIColor.blue.withAlphaComponent(0.5)
mAttString[9...10].foregroundColor = UIColor.red
mAttString[9...10].font = UIFont.boldSystemFont(ofSize: 20)
label.attributedText = mAttString

这可以获取

与切片协作

let detailString = NSMutableAttributedString(string: " Super sweet syntactic sugar for making AttributedString.")
let syntacticSlice = detailString[2...29]
syntacticSlice.foregroundColor = UIColor.red
syntacticSlice.shadow = {
  let shadow = NSShadow()
  shadow.shadowColor = UIColor.blue
  shadow.shadowBlurRadius = 2
  return shadow
}()
let endSlice = detailString[29...detailString.length]
endSlice.baselineOffset = -3
endSlice.foregroundColor = UIColor.lightGray
endSlice.font = UIFont.systemFont(ofSize: 18, weight: .thin)
detailLabel.attributedText = detailString