首页 > 解决方案 > 如何在 Cocoa 下正确设置下划线类型

问题描述

全部,

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。

我错过了什么?

蒂亚!

[编辑]:

    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]

你能帮我吗?

[/编辑]

标签: macosstylesnstextview

解决方案


您缺少下划线样式。

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

推荐阅读