|
| 1 | +import Foundation |
| 2 | +import UIKit |
| 3 | + |
| 4 | +@objcMembers |
| 5 | +@objc(NSLabelUtils) |
| 6 | +class NSLabelUtils: NSObject { |
| 7 | + class func setTextDecorationAndTransformOn(view:UIView!, text:String!, textDecoration:String!, letterSpacing:CGFloat, lineHeight:CGFloat, color:UIColor!) { |
| 8 | + let attrDict:NSMutableDictionary! = NSMutableDictionary() |
| 9 | + var paragraphStyle:NSMutableParagraphStyle! = nil |
| 10 | + let isTextType:Bool = (view is UITextField) || (view is UITextView) || (view is UILabel) || (view is UIButton) |
| 11 | + let isTextView:Bool = (view is UITextView) |
| 12 | + |
| 13 | + if textDecoration.contains("underline") { |
| 14 | + attrDict[NSAttributedString.Key.underlineStyle] = (NSUnderlineStyle.single) |
| 15 | + } |
| 16 | + |
| 17 | + if textDecoration.contains("line-through") { |
| 18 | + attrDict[NSAttributedString.Key.strikethroughStyle] = (NSUnderlineStyle.single) |
| 19 | + } |
| 20 | + |
| 21 | + if letterSpacing != 0 && isTextType && (view as! UILabel).font != nil { |
| 22 | + let kern:NSNumber! = NSNumber.init(value: letterSpacing * (view as! UILabel).font.pointSize) |
| 23 | + attrDict[NSAttributedString.Key.kern] = kern |
| 24 | + } |
| 25 | + var fLineHeight = lineHeight |
| 26 | + if fLineHeight >= 0 { |
| 27 | + if fLineHeight == 0 { |
| 28 | + fLineHeight = 0.00001 |
| 29 | + } |
| 30 | + if paragraphStyle == nil { |
| 31 | + paragraphStyle = NSMutableParagraphStyle() |
| 32 | + } |
| 33 | + paragraphStyle.minimumLineHeight = fLineHeight |
| 34 | + paragraphStyle.maximumLineHeight = fLineHeight |
| 35 | + // make sure a possible previously set text alignment setting is not lost when line height is specified |
| 36 | + |
| 37 | + |
| 38 | + } |
| 39 | + if (paragraphStyle != nil) { |
| 40 | + if (view is UIButton) { |
| 41 | + paragraphStyle.alignment = (view as! UIButton).titleLabel!.textAlignment |
| 42 | + } else { |
| 43 | + paragraphStyle.alignment = (view as! UILabel).textAlignment |
| 44 | + } |
| 45 | + if (view is UILabel) { |
| 46 | + // make sure a possible previously set line break mode is not lost when line height is specified |
| 47 | + paragraphStyle.lineBreakMode = (view as! UILabel).lineBreakMode |
| 48 | + } |
| 49 | + attrDict[NSAttributedString.Key.paragraphStyle] = paragraphStyle |
| 50 | + } |
| 51 | + |
| 52 | + if attrDict.count > 0 { |
| 53 | + if isTextView && ((view as! UITextView).font != nil) { |
| 54 | + // UITextView's font seems to change inside. |
| 55 | + attrDict[NSAttributedString.Key.font] = (view as! UITextView).font |
| 56 | + } |
| 57 | + |
| 58 | + if color != nil { |
| 59 | + attrDict[NSAttributedString.Key.foregroundColor] = color |
| 60 | + } |
| 61 | + |
| 62 | + let result:NSMutableAttributedString! = NSMutableAttributedString(string:text) |
| 63 | + result.setAttributes((attrDict as! [NSAttributedString.Key : Any]), range:NSRange(location: 0, length: text.count)) |
| 64 | + |
| 65 | + if (view is UIButton) { |
| 66 | + (view as! UIButton).setAttributedTitle(result, for:UIControl.State.normal) |
| 67 | + } |
| 68 | + else if(view is UILabel) { |
| 69 | + (view as! UILabel).attributedText = result |
| 70 | + } else if(view is UITextView) { |
| 71 | + (view as! UITextView).attributedText = result |
| 72 | + } |
| 73 | + } else { |
| 74 | + if (view is UIButton) { |
| 75 | + // Clear attributedText or title won't be affected. |
| 76 | + (view as! UIButton).setAttributedTitle(nil, for:UIControl.State.normal) |
| 77 | + (view as! UIButton).setTitle(text, for:UIControl.State.normal) |
| 78 | + } else if(view is UILabel) { |
| 79 | + // Clear attributedText or text won't be affected. |
| 80 | + (view as! UILabel).attributedText = nil |
| 81 | + (view as! UILabel).text = text |
| 82 | + } else if(view is UITextView) { |
| 83 | + // Clear attributedText or text won't be affected. |
| 84 | + (view as! UITextView).attributedText = nil |
| 85 | + (view as! UITextView).text = text |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + class func inset(rect:CGRect, uIEdgeInsets:UIEdgeInsets) -> CGRect { |
| 90 | + return rect.inset(by: uIEdgeInsets) |
| 91 | + } |
| 92 | + enum NSLabelOrNSTextView: Equatable { |
| 93 | + case NSLabel(NSLabel) |
| 94 | + case NSTextView(NSTextView) |
| 95 | + } |
| 96 | + enum Component { |
| 97 | + case `switch`(UISwitch) |
| 98 | + case stepper(UIStepper) |
| 99 | + |
| 100 | + var control: UIControl { |
| 101 | + switch self { |
| 102 | + case .switch(let comp): |
| 103 | + return comp |
| 104 | + case .stepper(let comp): |
| 105 | + return comp |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + class func updateFontRatio(_ view: UIView, ratio: CGFloat){ |
| 111 | + var currentAttributedString: NSAttributedString? = nil |
| 112 | + switch view { |
| 113 | + case is NSLabel: |
| 114 | + currentAttributedString = (view as! NSLabel).attributedText |
| 115 | + case is NSTextView: |
| 116 | + currentAttributedString = (view as! NSTextView).attributedText |
| 117 | + default: break |
| 118 | + } |
| 119 | + if (currentAttributedString == nil) { |
| 120 | + return; |
| 121 | + } |
| 122 | + let toChange: NSMutableAttributedString = (currentAttributedString as? NSMutableAttributedString) ?? NSMutableAttributedString.init(attributedString: currentAttributedString!) |
| 123 | + var found = false; |
| 124 | + toChange.enumerateAttribute(NSAttributedString.Key(rawValue: "OriginalFontSize"), in: NSRange(location: 0, length: toChange.length)) { value, range, stop in |
| 125 | + if ((value != nil) && range.length > 0) { |
| 126 | + toChange.enumerateAttribute(NSAttributedString.Key.font, in: range) { value2, range2, stop2 in |
| 127 | + if let font = (value2 as? UIFont) { |
| 128 | + if ( ratio != font.pointSize) { |
| 129 | + let newFont = font.withSize(round(value as! CGFloat * ratio)) |
| 130 | + found = true; |
| 131 | + toChange.removeAttribute(NSAttributedString.Key.font, range: range) |
| 132 | + toChange.addAttribute(NSAttributedString.Key.font, value: newFont, range: range) |
| 133 | + |
| 134 | + } |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + if (found) { |
| 143 | + switch view { |
| 144 | + case is NSLabel: |
| 145 | + (view as! NSLabel).attributedText = toChange; |
| 146 | + case is NSTextView: |
| 147 | + (view as! NSTextView).attributedText = toChange; |
| 148 | + default: break |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +@objc extension NSAttributedString { |
| 155 | + func hasAttribute(_ attribute: String) -> Bool { |
| 156 | + var result = false; |
| 157 | + self.enumerateAttribute(NSAttributedString.Key(rawValue: attribute), in: NSRange(location: 0, length: length)) { value, range, stop in |
| 158 | + if ((value != nil) && range.length > 0) { |
| 159 | + result = true |
| 160 | + stop.pointee = true |
| 161 | + } |
| 162 | + } |
| 163 | + return result; |
| 164 | + } |
| 165 | +} |
0 commit comments