Sample code for creating a NSTextField “label”?

前端 未结 6 1079
灰色年华
灰色年华 2020-12-24 01:53

In my desktop Mac OS X app, I\'d like to programatically create a NSTextField \"label\" which has the same behavior and properties as a typical label created in Interface Bu

6条回答
  •  死守一世寂寞
    2020-12-24 02:31

    Disassembled AppKit in Objective-C:

    BOOL TMPSierraOrLater() {
        static BOOL result = NO;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            result = [NSProcessInfo.processInfo isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){ 10, 12, 0 }];
        });
        return result;
    }
    
    @implementation NSTextField (TMP)
    
    + (instancetype)TMP_labelWithString:(NSString *)stringValue {
        if (TMPSierraOrLater()) {
            return [self labelWithString:stringValue];
        }
        NSParameterAssert(stringValue);
        NSTextField *label = [NSTextField TMP_newBaseLabelWithoutTitle];
        label.lineBreakMode = NSLineBreakByClipping;
        label.selectable = NO;
        [label setContentHuggingPriority:(NSLayoutPriorityDefaultLow + 1) forOrientation:NSLayoutConstraintOrientationHorizontal];
        [label setContentHuggingPriority:NSLayoutPriorityDefaultHigh forOrientation:NSLayoutConstraintOrientationVertical];
        [label setContentCompressionResistancePriority:NSLayoutPriorityDefaultHigh forOrientation:NSLayoutConstraintOrientationHorizontal];
        [label setContentCompressionResistancePriority:NSLayoutPriorityDefaultHigh forOrientation:NSLayoutConstraintOrientationVertical];
        label.stringValue = stringValue;
        [label sizeToFit];
        return label;
    }
    
    + (instancetype)TMP_wrappingLabelWithString:(NSString *)stringValue {
        if (TMPSierraOrLater()) {
            return [self wrappingLabelWithString:stringValue];
        }
        NSParameterAssert(stringValue);
        NSTextField *label = [NSTextField TMP_newBaseLabelWithoutTitle];
        label.lineBreakMode = NSLineBreakByWordWrapping;
        label.selectable = YES;
        [label setContentHuggingPriority:NSLayoutPriorityDefaultLow forOrientation:NSLayoutConstraintOrientationHorizontal];
        [label setContentHuggingPriority:NSLayoutPriorityDefaultHigh forOrientation:NSLayoutConstraintOrientationVertical];
        [label setContentCompressionResistancePriority:NSLayoutPriorityDefaultLow forOrientation:NSLayoutConstraintOrientationHorizontal];
        [label setContentCompressionResistancePriority:NSLayoutPriorityDefaultHigh forOrientation:NSLayoutConstraintOrientationVertical];
        label.stringValue = stringValue;
        label.preferredMaxLayoutWidth = 0;
        [label sizeToFit];
        return label;
    }
    
    + (instancetype)TMP_labelWithAttributedString:(NSAttributedString *)attributedStringValue {
        if (CRKSierraOrLater()) {
            return [self labelWithAttributedString:attributedStringValue];
        }
        NSParameterAssert(attributedStringValue);
        NSTextField *label = [NSTextField TMP_newBaseLabelWithoutTitle];
        [label setContentHuggingPriority:NSLayoutPriorityDefaultLow forOrientation:NSLayoutConstraintOrientationHorizontal];
        [label setContentHuggingPriority:NSLayoutPriorityDefaultHigh forOrientation:NSLayoutConstraintOrientationVertical];
        [label setContentCompressionResistancePriority:NSLayoutPriorityDefaultLow forOrientation:NSLayoutConstraintOrientationHorizontal];
        [label setContentCompressionResistancePriority:NSLayoutPriorityDefaultHigh forOrientation:NSLayoutConstraintOrientationVertical];
        label.attributedStringValue = attributedStringValue;
        [label sizeToFit];
        return label;
    }
    
    #pragma mark - Private API
    
    + (instancetype)TMP_newBaseLabelWithoutTitle {
        NSTextField *label = [[self alloc] initWithFrame:CGRectZero];
        label.textColor = NSColor.labelColor;
        label.font = [NSFont systemFontOfSize:0.0];
        label.alignment = NSTextAlignmentNatural;
        label.baseWritingDirection = NSWritingDirectionNatural;
        label.userInterfaceLayoutDirection = NSApp.userInterfaceLayoutDirection;
        label.enabled = YES;
        label.bezeled = NO;
        label.bordered = NO;
        label.drawsBackground = NO;
        label.continuous = NO;
        label.editable = NO;
        return label;
    }
    
    @end
    

提交回复
热议问题