Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. import Foundation
  2. import UIKit
  3.  
  4. let attrStr = try NSMutableAttributedString(
  5. data: "<b>sdad </b> assdds.".dataUsingEncoding(NSUnicodeStringEncoding)!,
  6. options: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType],
  7. documentAttributes: nil)
  8.  
  9. let fontNormal = UIFont(name: "Helvetica", size: 15)!
  10. let fontBold = UIFont(name: "Helvetica-Bold", size: 15)!
  11.  
  12. func styled(attrStr: NSAttributedString) -> NSAttributedString {
  13. let att = NSMutableAttributedString(attributedString: attrStr)
  14. let rangeAll = NSRange(location: 0, length: att.length)
  15. att.enumerateAttributesInRange(rangeAll, options: .LongestEffectiveRangeNotRequired) { attrs, range, stop in
  16. let font = attrs["NSFont"]!
  17. att.addAttribute(NSFontAttributeName, value: font.fontName.containsString("Bold") ? fontBold : fontNormal, range: range)
  18. }
  19. att.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: rangeAll)
  20. return att
  21. }
  22.  
  23. let s = styled(attrStr)
  24.  
  25. //let rangeAll = NSRange(location: 0, length: attrStr.length)
  26. //attrStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: rangeAll)
  27. //attrStr.addAttribute(NSFontAttributeName, value: UIFont(name: "Times New Roman", size: 18.0)!, range: rangeAll)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement