【问题标题】:NSAttributedString Strike attribute not workingNSAttributedString Strike 属性不起作用
【发布时间】:2018-10-20 13:44:10
【问题描述】:

我知道这个问题已经被问过很多次了,但是我阅读的答案都没有帮助我解决我的问题...

在我的 iOS 应用程序中,我正在使用默认框架 (UIGraphicsBeginPDFContextToFile...) 生成 PDF 文件。 一切正常,我可以更改文本颜色、基础样式等。

但我没有敲弦。

这是我的代码:

[toBeConfirmedText addAttributes:@{ NSBaselineOffsetAttributeName: @(0), NSStrikethroughStyleAttributeName: @(NSUnderlineStyleThick), NSStrikethroughColorAttributeName: [UIColor redColor] } range:NSMakeRange(0, toBeConfirmedText.length)];

但这不起作用...

有人有想法吗?

【问题讨论】:

    标签: ios text nsattributedstring strikethrough


    【解决方案1】:

    我应该加个注释说你的代码在Xcode 10没有问题。但是因为我需要加测试代码,所以你可以把它当作一个答案。

            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"myTest.pdf"];
            BOOL result =    UIGraphicsBeginPDFContextToFile(filePath, CGRectZero, nil);
    
            UIGraphicsBeginPDFPage();
    
            NSMutableAttributedString * toBeConfirmedText = [[NSMutableAttributedString alloc]initWithString:@"mytest"];
    
            [toBeConfirmedText addAttributes:@{ NSBaselineOffsetAttributeName: @(0), NSStrikethroughStyleAttributeName: @(NSUnderlineStyleThick), NSStrikethroughColorAttributeName: [UIColor redColor] } range:NSMakeRange(0, toBeConfirmedText.length)];
    
            [toBeConfirmedText drawInRect: CGRectMake(0, 0, 600, 200)];
            UIGraphicsEndPDFContext();
    

    【讨论】:

    • 感谢您的回答!我直接在 application:didFinishLaunchingWithOptions: 中尝试了你的代码,它运行良好......我不知道我是否对此感到满意,因为我真的看不出我的原始代码有什么问题!这可能是因为我使用 Core Text 来呈现我的 PDF 文件吗?我将属性字符串封装在 CTFramesetterRef...
    • 哇!我终于找到了解决方案.. 那是因为使用 CTFramesetterRef... 我不明白这有什么问题(老实说,我不记得为什么我更喜欢这种方式而不是直接调用 -drawInRect:),但是现在我正在使用这种方法,正如你所建议的那样,效果很好。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-11-03
    • 2016-02-06
    • 2011-03-31
    • 2018-11-19
    • 2012-07-23
    • 2016-07-16
    • 2020-01-24
    • 2015-09-05
    相关资源
    最近更新 更多