【问题标题】:NSAttributedString Strikethrough makes my attributed text disappearNSAttributedString 删除线使我的属性文本消失
【发布时间】:2014-01-02 17:16:13
【问题描述】:

我正在尝试制作带有删除线的属性字符串。我可以设置其他属性,例如前景色和字体大小,但是当我尝试通过部分文本设置删除线时,该部分文本会消失。任何想法可能是什么原因造成的?

下面是代码。感谢收看!

// ...    
    //Price
    NSLog(@"RESULT NUMBER %d", cell.result.resultId);
    priceString = (priceString == nil) ? @"$150.00\n$100.00" : priceString;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:priceString];
    NSRange linebreak = [priceString rangeOfString:@"\n"];



    if (linebreak.location != NSNotFound) {
        [attributedString beginEditing];
        // RegPrice
        NSRange firstLine = NSMakeRange(0, linebreak.location);
//        [attributedString addAttribute:NSFontAttributeName
//                                 value:[UIFont boldSystemFontOfSize:11]
//                                 range:firstLine];
        [attributedString addAttribute:NSForegroundColorAttributeName
                                 value:[UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1]
                                 range:firstLine];
        @try {
            [attributedString addAttribute:NSStrikethroughStyleAttributeName
                                     value:@(NSUnderlineStyleSingle)
                                     range:firstLine];
        } @catch (NSException *e) {
            NSLog(@"ATTRIBUTE EXCEPTION: %@", e);
        }

        // Sale Price
        [attributedString addAttribute:NSFontAttributeName
                                 value:[UIFont boldSystemFontOfSize:14]
                                 range:NSMakeRange(linebreak.location + 1, priceString.length - (linebreak.location + 1))];
        [attributedString endEditing];
    }
    cell.lblPrice.attributedText = attributedString;

    return cell;
}

【问题讨论】:

    标签: ios cocoa-touch nsattributedstring


    【解决方案1】:

    在我意识到 NSStrikethroughStyleAttributeName 的值应该是 NSNumber 之前,我遇到了字符串消失的相同问题。

    因此上面的代码应该是这样的:

    目标-c

    [attributedString addAttribute:NSStrikethroughStyleAttributeName 
                             value: [NSNumber numberWithInt: NSUnderlineStyleSingle] 
                             range:firstLine];
    

    在 Swift 中,使用 [NSAttributedStringKey: Any] 类型的字典传递 4 个属性,而罢工槽样式属性如下所示:

    [NSAttributedStringKey.strikethroughStyle: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue)]
    

    【讨论】:

    • 这是唯一对我有用的答案。这显然是一个错误/不是苹果的意图......
    • 很高兴有帮助。不确定这是一个错误,但绝对不是很“迅速”
    • 非常感谢!!这救了我!
    【解决方案2】:

    对于我来说,boldSystemFontOfSize:返回一个带有 HelveticaNeueInterface-MediumP4 字体的字体对象。你确定这个字体有删除线吗?并非所有字体都可以。也许尝试使用不同的字体,你知道有删除线。

    【讨论】:

    • 谢谢,这是个好主意。但是,我尝试将其切换为 arial,但似乎没有用。你知道任何支持删除线的字体吗?
    • 我没有。我遇到了同样的问题,最终受够了,并按照stackoverflow.com/a/2489560/2033515 的建议自己画了贯穿线
    【解决方案3】:

    只有在 iOS 7.0 中,删除线文本在 UILabel 中消失时遇到问题。它在 iOS 6.1 和 7.1 中运行良好。

    iOS 7.0 中似乎存在一个错误,如果标签对于文本来说太高一定量,删除线文本就会消失。当我将标签的框架设置为足够小的高度或在设置属性文本后调用 sizeToFit 时,它会起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多