Skip to content

Commit

Permalink
perf: improve EZLabel
Browse files Browse the repository at this point in the history
  • Loading branch information
tisfeng committed Nov 30, 2024
1 parent a8e163b commit a3d48d1
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions Easydict/objc/ViewController/View/EZLabel/EZLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ - (void)setup {
self.editable = NO;
self.backgroundColor = NSColor.clearColor;
[self setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];

self.lineSpacing = 4;
self.paragraphSpacing = 0;
self.font = [NSFont systemFontOfSize:14];
Expand All @@ -31,37 +31,31 @@ - (void)setup {

- (void)setText:(NSString *)text {
_text = text;

self.string = text;
NSRange range = NSMakeRange(0, text.length);

// Character spacing

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];

// Line spacing
NSRange range = NSMakeRange(0, text.length);

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = self.lineSpacing;
paragraphStyle.paragraphSpacing = self.paragraphSpacing;

[attributedString addAttributes:@{
NSParagraphStyleAttributeName : paragraphStyle,
NSKernAttributeName : @(0.2),
NSFontAttributeName : self.font,
}
range:range];


__block NSColor *textColor = self.textForegroundColor ?: nil;
[self excuteLight:^(NSTextView *textView) {
[attributedString addAttributes:@{
NSForegroundColorAttributeName : self.textForegroundColor ?: [NSColor ez_resultTextLightColor],
}
range:range];
[textView.textStorage setAttributedString:attributedString];
textColor = textColor ?: [NSColor ez_resultTextLightColor];
[attributedString addAttribute:NSForegroundColorAttributeName value:textColor range:range];
textView.textStorage.attributedString = attributedString;
} dark:^(NSTextView *textView) {
[attributedString addAttributes:@{
NSForegroundColorAttributeName : self.textForegroundColor ?: [NSColor ez_resultTextDarkColor],
}
range:range];
[textView.textStorage setAttributedString:attributedString];
textColor = textColor ?: [NSColor ez_resultTextDarkColor];
[attributedString addAttribute:NSForegroundColorAttributeName value:textColor range:range];
textView.textStorage.attributedString = attributedString;
}];
}

Expand All @@ -76,7 +70,7 @@ - (CGSize)oneLineSize {

- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];

// Drawing code here.
}

Expand Down

0 comments on commit a3d48d1

Please sign in to comment.