fileprivatefuncgetAttributeStringWithString(_string:String,font:CGFloat
) ->NSAttributedString{
letattributedString =NSMutableAttributedString(string: string)
letparagraphStye =NSMutableParagraphStyle()
//调整行间距
paragraphStye.lineSpacing= font/2
letrang =NSMakeRange(0,CFStringGetLength(stringasCFString!))
attributedString .addAttribute(NSParagraphStyleAttributeName, value: paragraphStye, range: rang)
returnattributedString
}
//
//String+hexa.swift
//hexa
//
//Created by郭源on 16/8/5.
//Copyright © 2016年vincross. All rights reserved.
//
importUIKit
extensionNSMutableAttributedString{
funcfont(_font:UIFont, range:NSRange? =nil) ->NSMutableAttributedString{
addAttributes([NSFontAttributeName: font], range: range ?? textRange)
returnself
}
functextColor(_textColor:UIColor, range:NSRange? =nil) ->NSMutableAttributedString{
addAttributes([NSForegroundColorAttributeName: textColor], range: range ?? textRange)
returnself
}
funclineSpace(_lineSpace:CGFloat, range:NSRange? =nil) ->NSMutableAttributedString{
guard!string.isEmptyelse{returnself}
letparagraph = (attribute(NSParagraphStyleAttributeName, at:0, effectiveRange:nil)as?NSMutableParagraphStyle) ?? (NSMutableParagraphStyle.default.mutableCopy()as!NSMutableParagraphStyle)
paragraph.lineSpacing = lineSpace
addAttributes([NSParagraphStyleAttributeName: paragraph], range: range ?? textRange)
returnself
}
funcalignment(_alignment:NSTextAlignment, range:NSRange? =nil) ->NSMutableAttributedString{
guard!string.isEmptyelse{returnself}
letparagraph = (attribute(NSParagraphStyleAttributeName, at:0, effectiveRange:nil)as?NSMutableParagraphStyle) ?? (NSMutableParagraphStyle.default.mutableCopy()as!NSMutableParagraphStyle)
paragraph.alignment = alignment
addAttributes([NSParagraphStyleAttributeName: paragraph], range: range ?? textRange)
returnself
}
funcunderlineStyle(_underlineStyle:NSUnderlineStyle, range:NSRange? =nil) ->NSMutableAttributedString{
addAttributes([NSUnderlineStyleAttributeName: underlineStyle.rawValue], range: range ?? textRange)
returnself
}
funcbaselineOffset(_offset:Int, range:NSRange? =nil) ->NSMutableAttributedString{
addAttribute(NSBaselineOffsetAttributeName, value: NSNumber(integerLiteral: offset), range: range ?? textRange)
returnself
}
fileprivatevartextRange:NSRange{
returnNSMakeRange(0, length)
}
}
extensionString{
varattr:NSMutableAttributedString{
returnNSMutableAttributedString(string:self)
}
funcstringRect(with font:UIFont= .hexa_systemFont(ofSize:12) , size:CGSize= CGSize(width: CGFloat.infinity, height: CGFloat.infinity))->CGSize{
letattrStr = attr.font(font)
returnattrStr.boundingRect(with: size, options: NSStringDrawingOptions.usesLineFragmentOrigin, context:nil).size
}
}
网友评论