选择NSTextField时字体更改

时间:2017-07-22 16:12:32

标签: objective-c macos nsattributedstring nstextfield

我想创建一个带有属性字符串的简单标签。我是这样做的:

NSDictionary *noNotificationsAttrs = [NSDictionary dictionaryWithObjectsAndKeys:centredStyle,
                                      NSParagraphStyleAttributeName,
                                      [NSFont fontWithName:@"Raleway-Bold" size:30],
                                      NSFontAttributeName,
                                      _grey,
                                      NSForegroundColorAttributeName,
                                      nil];
NSMutableAttributedString *noNotificationsString =
[[NSMutableAttributedString alloc] initWithString:@"No Notifications"
                                       attributes:noNotificationsAttrs];

NSTextField* title_field = [[NSTextField alloc] initWithFrame:
                             CGRectMake(
                                        0,
                                        0,
                                        200,
                                        200
                                        )
                             ];
[title_field setWantsLayer:true];
[title_field setSelectable:YES];
[title_field setAllowsEditingTextAttributes:true];
[title_field setAttributedStringValue:noNotificationsString];
[title_field setEditable:false];
[title_field setBordered:false];
title_field.tag = 1;

结果如下:

enter image description here

enter image description here

不幸的是,点击(选择)此标签时,它显示如下:

enter image description here

enter image description here

角落周围有点粗糙和像素化。这种情况正在发生,许多其他标签具有不同的字符串,大小和颜色。我该如何解决?!

请注意,这些标签嵌套在nsscrollview - >内nstableview - > viewForTableColumn

选择时叠加:

enter image description here 我相信问题是NSCell在mousedown上调用了一个编辑函数。

选择时字体也不同!! enter image description here

编辑:

有趣的是,如果我从(2)父视图中删除wantslayer:YES,它就不会这样做。但是他们都需要想要层,否则我就不能有弯角等等......

3 个答案:

答案 0 :(得分:1)

将此行添加到您的代码中。

curl -d 'info={ "EmployeeID": [ "1234567", "7654321" ], "Salary": true, "BonusPercentage": 10}' http://example.com/xyz/php/api/createjob.php

因此在选择时将属性添加到与窗口关联的NSTextView。

您现在所获得的内容,如选择后的粗体和字体更改将被覆盖。

编辑:

工作代码

已将NSTextView *textEditor = (NSTextView *)[[[NSApplication sharedApplication] keyWindow] fieldEditor:YES forObject:title_field]; [textEditor setSelectedTextAttributes:noNotificationsAttrs]; 更改为NSForegroundColorAttributeNameNSBackgroundColorAttributeName_grey

[NSColor clearColor]

答案 1 :(得分:0)

为了解决这个问题,我制作了一个自定义的NSTextField,在这里选择它更新如下:

While going in the project--> Configure--> set the path of the git correctly.
It should be till the bin and then append git.exe like *\bin\git.exe

但有时仍然很有趣

答案 2 :(得分:0)

我已将其修复完美!希望对大家有帮助。

1.lazy property
- (NSTextField *)textFieldOne{
    if (!_textFieldOne) {
        _textFieldOne = ({
            NSTextField *view = [[NSTextField alloc]init];

            view.cell.scrollable = true;

            view.font = [NSFont fontWithName:@"PingFangSC-Light" size:14];
            view.cell.wraps = true;

            view.editable = false;
            view.selectable = true;
            view.allowsEditingTextAttributes = true;
            if (@available(macOS 10.12.2, *)) {
                view.automaticTextCompletionEnabled = true;
            } else {
                // Fallback on earlier versions
            }
            view;
        });
    }
    return _textFieldOne;
}

2.
// @{string: url}
NSDictionary *dic = @{
                      @"github/shang1219178163": @"https://github.com/shang1219178163",
                          };
  self.textFieldOne.stringValue = [NSString stringWithFormat:@"%@\n%@\n%@", NSApplication.appName, NSApplication.appCopyright, @"github/shang1219178163"];
 [self.textFieldOne setHyperlinkDic:dic];


3.
#import "NSTextField+Helper.h"

-(void)setHyperlinkDic:(NSDictionary *)dic{
    // both are needed, otherwise hyperlink won't accept mousedown
    NSTextField *textField = self;
    NSDictionary * attributes = @{
                                  NSFontAttributeName: textField.font,
                                  };

    __block NSMutableAttributedString *mattStr = [[NSMutableAttributedString alloc]initWithString:textField.stringValue attributes:attributes];
    [dic enumerateKeysAndObjectsUsingBlock:^(NSString * key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        NSURL *url = [NSURL URLWithString:obj];
        NSAttributedString * attStr = [NSAttributedString hyperlinkFromString:key withURL:url font:textField.font];
        NSRange range = [mattStr.string rangeOfString:key];
        [mattStr replaceCharactersInRange:range withAttributedString:attStr];

    }];
    textField.attributedStringValue = mattStr;

    textField.cell.wraps = true;
    textField.cell.scrollable = true;
    textField.editable = false;
    textField.selectable = true;
    textField.allowsEditingTextAttributes = true;

}

4.
#import "NSAttributedString+Helper.h"

+(id)hyperlinkFromString:(NSString *)string withURL:(NSURL *)aURL font:(NSFont *)font{
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString: string];

    NSRange range = NSMakeRange(0, attrString.length);

//    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc]init];
    NSDictionary * dic = @{
                           NSFontAttributeName: font,
                           NSForegroundColorAttributeName: NSColor.blueColor,
                           NSLinkAttributeName: aURL.absoluteString,
                           NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),
//                           NSParagraphStyleAttributeName: paraStyle,
//                           NSBaselineOffsetAttributeName: @15,
                           };


    [attrString beginEditing];
    [attrString addAttributes:dic range:range];
    [attrString endEditing];
    return attrString;
}

display