如何在可可下正确设置下划线类型

时间:2019-06-26 04:00:43

标签: macos styles nstextview

全部

NSMutableDictionary* constattrs = [NSMutableDictionary dictionaryWithCapacity:5];
[attrs setObject:[NSNumber numberWithInt:( NSUnderlinePatternDot )] forKey:NSUnderlineStyleAttributeName];
[attrs setValue:style.GetUnderlineColour() forKey:NSUnderlineColorAttributeName];
[m_textView setTypingAttributes:attrs];

上面的代码可以正常编译并执行,但是视图中的文本不会带有下划线。

我正在OSX 10.13下进行测试,最低要求为10.9。

我想念什么?

TIA!

[编辑]:

    NSRange range = NSMakeRange(start, end-start);

    NSTextStorage* storage = [m_textView textStorage];
    if( style.HasFontUnderlined() )
    {
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        [dict setObject:[NSNumber numberWithInt:( NSUnderlinePatternDot )] forKey:NSUnderlineStyleAttributeName];
        [dict setValue:style.GetUnderlineColour() forKey:NSUnderlineColorAttributeName];

        [storage addAtributes:dict range:range];
        [dict release];

我在上面的代码中收到编译警告:

warning: instance method '-addAtributes:range:' not found (return type defaults to 'id'); did you mean '-addAttributes:range:'? [-Wobjc-method-access]

你能帮忙吗?

[/ EDIT]

1 个答案:

答案 0 :(得分:0)

您缺少下划线样式。

[attrs setObject:[NSNumber numberWithInt:( NSUnderlineStyleSingle | NSUnderlinePatternDot )] forKey:NSUnderlineStyleAttributeName];