简体   繁体   English

改变Ray Wenderlich的Parser不使用UITextView

[英]Altering Ray Wenderlich's Parser not working with UITextView

I have tried to lift the parser in Ray Wenderlich's simple magazine tutorial into my project. 我试图将Ray Wenderlich的简单杂志教程中的解析器提升到我的项目中。 His parser code is here: how to create a simple magazine app with core text 他的解析器代码在这里: 如何使用核心文本创建一个简单的杂志应用程序

The main controller passes a text string to the parser, which dissects it, adds attributes and then returns the attributed string, using the following code in viewDidLoad: 主控制器将文本字符串传递给解析器,解析器将解析它,添加属性,然后使用viewDidLoad中的以下代码返回属性字符串:

 - (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"zombies" ofType:@"txt"];
    NSString* text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
MarkupParser* p = [[[MarkupParser alloc] init] autorelease];
NSAttributedString* attString = [p attrStringFromMarkup: text];
[(CTView *)[self view] setAttString:attString withImages: p.images];
[(CTView *)[self view] buildFrames];
}

As I am using Ios6 im thinking that the attributed string passed back from the parser can simply be added to a UITextView instead of having to mess around with CoreText. 因为我正在使用Ios6我认为从解析器传回的属性字符串可以简单地添加到UITextView而不必乱用CoreText。 With this in mind ive altered the above code to the following: 考虑到这一点,我已将上述代码更改为以下内容:

    - (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"zombies" ofType:@"txt"];
    NSString* text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
    MarkupParser* p = [[[MarkupParser alloc] init] autorelease];
    NSAttributedString* attString = [p attrStringFromMarkup: text];

    UITextView *view = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];

    view.attributedText = attString;
    [self.view addSubview:view];
}

However this now produces an error saying 'unknown selector sent to instance' on the line which sets the view.attributedText to attString. 但是,现在这会产生一个错误,说明“将未知的选择器发送到实例”这一行将view.attributedText设置为attString。 Im certain the attributed string returned from the parser is to blame, but cant for the life of my figure out why! 我确定从解析器返回的属性字符串是责备,但不能解释为什么我的生活! any ideas out there? 有什么想法吗?


Here is a screenshot of the console during runtime: 以下是运行时控制台的屏幕截图:

在此输入图像描述

在此输入图像描述


The next image is a screenshot of the details in the AttributedString named attString which is causing the problem 下一个图像是名为attString的AttributedString中的详细信息的屏幕截图,它导致了问题

在此输入图像描述

And finally the following section of the code is where the parser gets its attributes set, and which could possibly causing the pointSize error shown in the debugger above. 最后,代码的以下部分是解析器获取其属性集的位置,并且可能导致上面的调试器中显示的pointSize错误。

 CTFontRef fontRef = CTFontCreateWithName((CFStringRef)CFBridgingRetain(self.font),
                                             24.0f, NULL);

 //apply the current text style
 NSDictionary* attrs = [NSDictionary dictionaryWithObjectsAndKeys:
                       (id)self.color.CGColor, kCTForegroundColorAttributeName,
                       (id)CFBridgingRelease(fontRef), kCTFontAttributeName,
                       (id)self.strokeColor.CGColor, (NSString *) kCTStrokeColorAttributeName,
                       (id)[NSNumber numberWithFloat: self.strokeWidth], (NSString *)kCTStrokeWidthAttributeName,
                       nil];

 [aString appendAttributedString:[[NSAttributedString alloc] initWithString:[parts objectAtIndex:0] attributes:attrs] ];

The "bridging" commands above were added automatically by Xcode, I know its something to do with ARC, but I dont really understand them and so dont know if these are causing the problem! 上面的“桥接”命令是由Xcode自动添加的,我知道它与ARC有关,但我真的不了解它们,因此不知道这些是否导致问题!

It looks to me like you must be running in a pre-iOS6 environment - while you might be using the iOS6 SDK, you could be running in the iOS5 simulator. 在我看来,你必须在iOS6之前的环境中运行 - 当你可能正在使用iOS6 SDK时,你可以在iOS5模拟器中运行。 You definitely have a UITextView , if it was iOS6 then setAttributedText: would be fine. 你肯定有一个UITextView ,如果它是iOS6然后setAttributedText:会没事的。 Before iOS6 UITextView did not have the attributedText property and so trying to set it would result in 'unknown selector sent to instance'. 之前iOS6的UITextView没有足够的attributedText物业等试图设置它会导致“发送到实例未知的选择”。

If you find this isn't the case check your console output. 如果您发现不是这种情况,请检查您的控制台输出。 It probably says just what the unknown selector is... 它可能只是说未知选择器是什么......

3 whole days later its sorted!! 整整3天后排序!!

My solution (thanks to @Adam Eberbach for pointing me towards the parser!) is a fairly major overhaul of the parser code used by Ray Wenderlich. 我的解决方案(感谢@Adam Eberbach将我指向解析器!)是Ray Wenderlich使用的解析器代码的相当重大的改进。 Basically the problem was that I was lifting CoreText coding from his example and trying to use those attributes in the UITextView attributed string - so UITextView wasnt understanding what was being passed to it. 基本上问题是我从他的示例中提取CoreText编码并尝试在UITextView属性字符串中使用这些属性 - 所以UITextView并不了解传递给它的内容。

By replacing his parser with basic NS attributes rather than CG attributes (thus changing the majority of his code!) the UITextView was then able to understand what was being passed to it. 通过用基本的NS属性而不是CG属性替换他的解析器(从而改变他的大部分代码!),UITextView随后能够理解传递给它的内容。 I was able to keep the same regex's he created which is a god-send because try as i might, I cant understand what all those symbols mean! 我能够保持他创造的相同的正则表达式,这是一个上帝发送,因为尝试我可能,我不能理解所有这些符号的意思!

My replacement code for the final snippet of code in the original post is as follows: 我在原帖中最后一段代码的替换代码如下:

    for (NSTextCheckingResult* b in chunks) {
    NSArray* parts = [[markup substringWithRange:b.range]
                      componentsSeparatedByString:@"<"]; //1

    NSMutableAttributedString *temp = [[NSMutableAttributedString alloc] initWithString:[parts objectAtIndex:0]];
    [temp addAttribute:NSFontAttributeName value:[UIFont fontWithName:self.font size:20] range:NSMakeRange(0, [temp length])];
    [temp addAttribute:NSForegroundColorAttributeName value:self.color range:NSMakeRange(0, [temp length])];

    [aString appendAttributedString:temp];

Note that this doesnt include the stroke color and width attributes which Ray had in his original because these arent supported by UITextView, only by CoreText. 请注意,这不包括Ray在其原始文件中的笔触颜色和宽度属性,因为这些属性不受UITextView支持,只有CoreText支持。 However any other attributes can now be set in the same way with just a simple addition of another regex and another add:Attribute. 但是,现在可以通过添加另一个正则表达式和另一个add:Attribute来以相同的方式设置任何其他属性。

All in all I now have a very simple way of converting a long .txt file into attributed text just by adding a couple of tags in the text where I want the format to change. 总而言之,我现在有一种非常简单的方法可以将长.txt文件转换为属性文本,只需在文本中添加几个我希望格式更改的标记即可。 My gratitude to Ray and Alan! 我对雷和艾伦表示感谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM