【问题标题】:NSAttributedString: underline is not appliedNSAttributedString:未应用下划线
【发布时间】:2014-12-26 02:14:13
【问题描述】:

我正在尝试为UITextView 添加下划线样式,但未应用。如果我使用“阴影”(取消注释阴影样式和注释下划线样式),我可以看到它,但由于某种原因没有应用下划线。我使用“Courier New”字体。

- (void) addDiagHighlighting: (NSMutableAttributedString*)attrString start:(int)start end:(int)end severity:(int)severity {
    // ignore diags that are out of bounds
    if (start > attrString.length || end > attrString.length)
        return;

    NSRange range = NSMakeRange(start, end - start);
    UIColor *diagColor = [self getSeverityColor: severity];

    // shadow
//    NSShadow *shadow = [[NSShadow alloc] init];
//    [shadow setShadowColor: diagColor];
//    [shadow setShadowOffset: CGSizeMake (1.0, 1.0)];
//    [shadow setShadowBlurRadius: 1.0];
//    [attrString addAttribute:NSShadowAttributeName
//                       value:shadow
//                       range:range];

    // underline
    [attrString addAttributes:@{
                                NSUnderlineColorAttributeName : diagColor, // color
                                NSUnderlineStyleAttributeName : @(NSUnderlinePatternSolid) // style
                                }
                       range:range];
}

我可以将添加属性更改为同时添加阴影和下划线,我可以看到阴影但仍然没有下划线:

// shadow + underline
[attrString addAttributes:@{
                            NSShadowAttributeName : shadow, // shadow
                            NSUnderlineColorAttributeName : diagColor, // color
                            NSUnderlineStyleAttributeName : @(NSUnderlinePatternSolid) // style
                            }
                   range:range];

【问题讨论】:

    标签: ios nsattributedstring underline


    【解决方案1】:

    您需要将 NSUnderlinePatternNSUnderlineStyle 进行或运算才能使其正常工作 (see Apple documentation here)

    试试这个:

    [attrString addAttributes:@{
                            NSShadowAttributeName : shadow, // shadow
                            NSUnderlineColorAttributeName : diagColor, // color
                            NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle | NSUnderlinePatternSolid) // style
                            }
                   range:range];
    

    或者用点...

    [attrString addAttributes:@{
                            NSShadowAttributeName : shadow, // shadow
                            NSUnderlineColorAttributeName : diagColor, // color
                            NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle | NSUnderlinePatternDot) // style
                            }
                   range:range];
    

    【讨论】:

      【解决方案2】:

      Apple 开发者文档中提到了 NSUnderlineStyle

      样式、模式和可选的按字掩码进行“或”运算以生成underlineStylestrikethroughStyle 的值。

      因此,在 Swift 5 和 iOS 12.3 中,您可以使用 bitwise OR operator (|) 为 NSAttributedString.Key.underlineStyle 属性设置样式和模式。


      以下 Playground 示例代码显示了如何为 NSAttributedString 实例设置 NSUnderlineStyle.thickNSUnderlineStyle.patternDot 属性:

      import UIKit
      import PlaygroundSupport
      
      let attributes = [NSAttributedString.Key.underlineStyle : (NSUnderlineStyle.thick.rawValue | NSUnderlineStyle.patternDot.rawValue)]
      let attributedString = NSAttributedString(string: "Some text", attributes: attributes)
      
      let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
      label.backgroundColor = .white
      label.attributedText = attributedString
      
      PlaygroundPage.current.liveView = label
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-20
        • 1970-01-01
        • 2012-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多