The paper
In the recent development of OSX application, some places need to mix text and text, and clickable text, then I miss the YYText used in iOS, and then thought, anyway, I have more time to build a Swift wheel, so I wrote MK_Text
At present, I have completed most of the functions of YYLabel, but it is basically enough. Later, I will also enrich the functions inside, you can also mention any ideas, if I think it is more practical, I will try to achieve it. Next, I will introduce MK_Text ~
introduce
Making links: MK_Text
features
Support text and text mix, support rich text and View mix
Support asynchronous layout rendering
Support for highlighting text Settings
Supports OSX and iOS
Support for automatic layout
usage
Graphic mixed
You can customize the image display size. By default, the original image size is ~
You can set the image to its child line. The default is to be consistent with the middle line ~
let imStr = NSMutableAttributedString.mk_image(im: MK_Image.init(named: NSImage.Name.init("face"))! , size: CGSize. Init (width: 30, height: 30), alignType: NSMutableAttributedString. AlignType. Top) / / control the usage and images are basically identicallet v = UISwitch.init()
let viewStr = NSMutableAttributedString.mk_view(view: v, superView: ml, size: v.bounds.size)
Copy the code
Highlighted words
To use the highlight attribute, create the MK_TapResponse structure first. Optionally return two closures: 1, rich text attribute ~ 2 for highlight, and closure ~ for click callback
Here to determine whether the click is complete logic is the same as Button ~
let tap = NSMutableAttributedString.init(string: "Clickable character")
let response = MK_TapResponse.init(highlitedBlock: { (str) -> [NSAttributedStringKey : Any]? in
return [NSAttributedStringKey.foregroundColor : UIColor.red]
}) { (str, range) in
print("Click on string ~")
}
tap.addTapAttr(response: response, range: nil)
Copy the code
alignment
throughMK_LabelThe alignment property is set (left-aligned by default), as shown in the center here
Line breaks in advance
For better reading, sometimes we need to wrap the word in advance, otherwise a full word will be displayed on two lines, just like UILable. I provide the following properties to achieve this effect
// The Label is the rich text to draw. // The Label is the rich text to draw. Our character to the end of the subscript var makeNewLineEarlyConditionBlock: ((NSAttributedString, Int) - > (Bool)) = {(STR, index) - > Boolin
return falseVar wordSeparatorArr:[String] = [""]
Copy the code
The wordSeparatorArr array stores word separators. The default array has only one element: “(space). Just like UILabel, different words are separated by Spaces
MakeNewLineEarlyConditionBlock at the bank line, if there will be a word is divided into two lines show, will be back the closure, to determine whether or not to break in advance, because for the Chinese, then there is no such a word in two line shows the problems, here the user can according to own actual situation to deal with ~
rendering
1. Prewrap is not used
2, use prewrap (delimiter “”, closure returns true)
Asynchronous rendering
Determine ~ by setting the isAsync property of Label to false by default
The effect
At present, only the above functions are realized, and other functions will be enriched in the future
performance
As for the performance, I simply tested it, compared to YY_Text, and here is a simple comparison of the drawing time ~
This is where testing takes time
MK_Text
YY_Text
The result is that MK_Text is faster, probably because my function is relatively simple and I use Swift
Use attention
Modify rich text properties halfway through
If you need to refresh the UI while modifying rich text properties, use the following method
public func mk_setAttrtbute(dic:[NSAttributedStringKey : Any], range: NSRange)->Void
Copy the code
Automatic layout
MK_Text is reference for automatic constraint support UILabel to do, when the View translatesAutoresizingMaskIntoConstraints is true, then according to the View to render the frame.
If it is false, it will judge whether the constraint is bound to the width and height, if so, it will render according to the width and height of the constraint, if not, it will supplement the width and height constraint according to the render content ~
The installation
CocoaPods
Add pod ‘MK_Text’ to Podfile
System requirements
OSX 10.10 or iOS 8.0
conclusion
There aren’t many cameo files (I have one), so have a cameo appearance if you have any bugs or ideas from the cameo file, you may have a Star cameo file (ᴗ• Jun)