本文用于記錄工作中遇到富文本處理的方法
文本中穿插帶link的鏈接贾惦,link部分自定義顏色(如圖)
- 將整句文本掺炭、需要添加鏈接的部分分別定義為常量
- 計(jì)算出需要添加鏈接部分的Range
- 設(shè)置顏色與鏈接
- 配置整段文本
- 用UITextView裝在這段富文本
struct DisclaimerMaker {
let text = "By clicking “Post”, I agree to The Knot’s Privacy Policy and Terms of Use"
let policyLink = "your policy link"
let termsOfUseLink = "terms of use link"
let policyText = "Privacy Policy"
let termsOfUseText = "Terms of Use"
var attributedText: NSAttributedString {
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center
let attributes: [NSAttributedString.Key: Any] = [.font: UIFont.subhead,
.foregroundColor: UIColor.textSubtle,
.paragraphStyle: paragraph]
let attributedString = NSMutableAttributedString(string: text,
attributes: attributes)
let policyRange = (text as NSString).range(of: policyText)
let policyAttributes: [NSAttributedString.Key: Any] = [.link: policyLink,
.font: UIFont.subhead]
attributedString.setAttributes(policyAttributes,
range: policyRange)
let termsOfUseRange = (text as NSString).range(of: termsOfUseText)
let termsOfUseAttributes: [NSAttributedString.Key: Any] = [.link: termsOfUseLink,
.font: UIFont.subhead]
attributedString.setAttributes(termsOfUseAttributes,
range: termsOfUseRange)
return attributedString
}
}
private(set) lazy var legalDisclaimerTextView: UITextView = {
let textView = UITextView()
textView.textContainerInset = .zero
textView.isEditable = false
textView.isScrollEnabled = false
textView.textAlignment = .center
textView.linkTextAttributes = [.foregroundColor: UIColor.linkOnLight,
.underlineColor: UIColor.clear]
textView.attributedText = DisclaimerMaker().attributedText
return textView
}()
文本中間穿插圖片(如圖)
-
Note:點(diǎn)擊范圍是整段文字学少,所以不需要將最后藍(lán)色的部分單獨(dú)寫(xiě)成UIButton幔崖,而且寫(xiě)成UIButton對(duì)于約束方面也很難寫(xiě)信认,所以可以考慮用富文本來(lái)處理
- 定義一個(gè)NSTextAttachment奴迅,將它的image設(shè)置為最后向上的箭頭以及設(shè)置顏色
- 設(shè)置Close Image Credits的樣式(這里也可以使用上面第一種計(jì)算Range的方法)
- 配置整段富文本
- 這里不需要點(diǎn)擊青责,所以用UILabel裝在富文本即可
private static let creditsString: String = "Marni Rothschild Pictures, Lindsey Pantaleo Photography, Todd France Photography, Corbin Gurkin, Jocelyn Filley Photography, Thinkstock, Braedon Photography, Andrew Chan Photography, Jose Villa, Rhphotoarts, Laura Ivanova Photography, Abby Jiu Photography, Laura Ivanova Photography, KT Merry, Shutterstock, Sergio Kurhajec, Jen Fariello, Eric Kelley Photography, Jasmine Star, Dennis Lee Photography, Pen/Carlson, Carrie Patterson Photography, Jami Thompson Photography, Brian Dorsey Studios, Kate Headley Photography, Antonis Achilleos, Thinkstock, Devon Jarvis, Studio 222 Photography, Laura Ivanova Photography, Lens CAP Productions, Paper Antler, Beaux Arts Photographie, Anita Calero, Mel & Co., Samuel Lippke Studios, Look Wedding Photography, Alea Lovely, Justin & Mary Photography, Thinkstock, Shutterstock, Jess + Nate Studios, Braedon Photography, Joel Serrato" private func expandImageCreditsString() { let attributedString = NSMutableAttributedString(string: VendorCategoryImageCreditCollectionViewCell.creditsString) let iconView = NSTextAttachment() iconView.image = XOKitIcon.caretUp.image?.withTintColor(.iconLink, renderingMode: .alwaysTemplate) iconView.bounds = CGRect(x: 0, y: -4, width: 16, height: 16) let iconString = NSAttributedString(attachment: iconView) let closeString = NSMutableAttributedString(string: " Close Image Credits", attributes: [NSAttributedString.Key.foregroundColor: UIColor.iconLink, NSAttributedString.Key.font: UIFont.caption1]) closeString.append(iconString) attributedString.append(closeString) creditTextLabel.attributedText = attributedString }
優(yōu)化富文本固有代碼
在日常項(xiàng)目中,富文本最主要就是設(shè)置行高等style,無(wú)論要設(shè)置什么其他樣式脖隶,文本扁耐、字體都是必要的
所以可以擴(kuò)展NSMutableAttributedString把固有的代碼進(jìn)行一次封裝,使整體看起來(lái)更加簡(jiǎn)浩村、美觀
extension NSMutableAttributedString {
class func initWithString(_ string: String,
font: UIFont,
foregroundColor: UIColor,
minimumLineHeight: CGFloat,
maximumLineHeight: CGFloat)
-> NSMutableAttributedString {
let style = NSMutableParagraphStyle()
style.maximumLineHeight = maximumLineHeight
style.minimumLineHeight = minimumLineHeight
return initWithString(string,
font: font,
foregroundColor: foregroundColor,
style: style)
}
}
這個(gè)方法返回一個(gè)設(shè)置了字體做葵、顏色、style的NSMutableAttributedString心墅,將UILabel或UITextView的attributedText直接賦值就可以得到原有的效果
實(shí)際使用方法
groupNameLabel.attributedText = NSMutableAttributedString.initWithString(group.name.uppercased(),
font: .tb_font(larsseitMedium, andFontSize: 16),
foregroundColor: .tb_primaryCopy(),
minimumLineHeight: 24,
maximumLineHeight: 24)
未完待續(xù)
未經(jīng)授權(quán)酿矢,請(qǐng)勿轉(zhuǎn)載
謝謝!