//
|
// AttributedStringbuilder.swift
|
// qrcode
|
//
|
// Created by Chen, Harry (CHE-MLP) on 2017/11/15.
|
// Copyright © 2017年 Chen, Harry (CHE-MLP). All rights reserved.
|
//
|
|
import UIKit
|
|
class AttributedStringbuilder: NSObject {
|
|
var mutableAttributedString: NSMutableAttributedString = NSMutableAttributedString()
|
|
class func build() -> AttributedStringbuilder {
|
return AttributedStringbuilder()
|
}
|
|
@discardableResult
|
func add(string: String,withFont: UIFont,withColor: UIColor,lineSpace: CGFloat) -> AttributedStringbuilder {
|
let style = NSMutableParagraphStyle()
|
style.lineSpacing = lineSpace
|
mutableAttributedString.append(NSAttributedString(string: string, attributes: [NSAttributedString.Key.foregroundColor: withColor,NSAttributedString.Key.font: withFont, NSAttributedString.Key.paragraphStyle: style]))
|
return self
|
}
|
|
@discardableResult
|
func add(string: String,withFont: UIFont,withColor: UIColor,backColor: UIColor) -> AttributedStringbuilder {
|
let style = NSMutableParagraphStyle()
|
mutableAttributedString.append(NSAttributedString(string: string, attributes: [NSAttributedString.Key.foregroundColor: withColor,NSAttributedString.Key.font: withFont,NSAttributedString.Key.backgroundColor: backColor, NSAttributedString.Key.paragraphStyle: style]))
|
return self
|
}
|
|
@discardableResult
|
func add(string: String,withFont: UIFont,withColor: UIColor,indent: CGFloat) -> AttributedStringbuilder {
|
let style = NSMutableParagraphStyle()
|
style.firstLineHeadIndent = indent
|
mutableAttributedString.append(NSAttributedString(string: string, attributes: [NSAttributedString.Key.foregroundColor: withColor,NSAttributedString.Key.font: withFont, NSAttributedString.Key.paragraphStyle: style]))
|
return self
|
}
|
|
@discardableResult
|
func add(string: String,withFont: UIFont,withColor: UIColor,indent: CGFloat,lineSpace: CGFloat) -> AttributedStringbuilder {
|
let style = NSMutableParagraphStyle()
|
style.firstLineHeadIndent = indent
|
style.lineSpacing = lineSpace
|
mutableAttributedString.append(NSAttributedString(string: string, attributes: [NSAttributedString.Key.foregroundColor: withColor,NSAttributedString.Key.font: withFont, NSAttributedString.Key.paragraphStyle: style]))
|
return self
|
}
|
|
@discardableResult
|
func underLine() -> AttributedStringbuilder {
|
let range1 = NSRange(location: 0, length: mutableAttributedString.string.count)
|
let number = NSNumber(value:NSUnderlineStyle.single.rawValue)
|
mutableAttributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: number, range: range1)
|
return self
|
}
|
|
@discardableResult
|
func add(string: String,withFont: UIFont,withColor: UIColor) -> AttributedStringbuilder {
|
mutableAttributedString.append(NSAttributedString(string: string, attributes: [NSAttributedString.Key.foregroundColor: withColor,NSAttributedString.Key.font: withFont]))
|
return self
|
}
|
|
@discardableResult
|
func add(string: String,withSize: CGFloat,withColor: UIColor) -> AttributedStringbuilder {
|
mutableAttributedString.append(NSAttributedString(string: string, attributes: [NSAttributedString.Key.foregroundColor: withColor,NSAttributedString.Key.font: UIFont.systemFont(ofSize: withSize)]))
|
return self
|
}
|
|
@discardableResult
|
func attach(attachment: NSTextAttachment) -> AttributedStringbuilder {
|
mutableAttributedString.append(NSAttributedString(attachment: attachment))
|
return self
|
}
|
|
@discardableResult
|
func attach(image: UIImage) -> AttributedStringbuilder {
|
let attachment = NSTextAttachment()
|
attachment.image = image
|
mutableAttributedString.append(NSAttributedString(attachment: attachment))
|
return self
|
}
|
|
}
|
|
extension NSMutableAttributedString{
|
func heightOfAttributedString(_ width:Double) -> CGFloat {
|
let height = self.boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil).height
|
return ceil(height)
|
}
|
}
|