Mac开发-Label
2019-08-01 本文已影响0人
KeyboardLife
cocoa中并没有UILabel,也没有NSLabel,cocoa中的label实际上是NSTextField,并将editable = No,来实现显示效果。
NSTextField * label0 = [[NSTextField alloc] init];
label0.editable = NO;
label0.bordered = NO;//不显示边框
label0.backgroundColor = [NSColor greenColor];//控件背景色
label0.textColor = [NSColor magentaColor];//文字颜色
label0.font = [NSFont systemFontOfSize:10];
label0.alignment = NSTextAlignmentCenter;//水平显示方式
label0.maximumNumberOfLines = 2;//最多显示行数
label0.frame = NSMakeRect(100, 100, 100, 100);
//label0.stringValue = @"测试文字聚会旧时代加夫里什江东父老j";//现实的文字内容
//富文本用法
NSString * linkURLText = @"http://www.apple.com/";
NSString * text = [NSString stringWithFormat:@"please visit %@",linkURLText];
NSURL * linkURL = [NSURL URLWithString:linkURLText];
NSRange selectedRange = [text rangeOfString:linkURLText];
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:text];
[string beginEditing];
[string addAttribute:NSLinkAttributeName value:linkURL range:selectedRange];
[string addAttribute:NSForegroundColorAttributeName value:[NSColor blueColor] range:selectedRange];
[string addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:selectedRange];
[string endEditing];
label0.attributedStringValue = string;
[self.view addSubview:label0];
xib显示多行文字
A1389BBD-03F9-4F23-943A-7C85FD9067D8.png运行效果图如下:
3392AD1D-607F-4504-B5BD-0AD3642C8E3E.png