Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 改变Ray Wenderlich';s分析器不使用UITextView_Iphone_Xcode_Nsattributedstring - Fatal编程技术网

Iphone 改变Ray Wenderlich';s分析器不使用UITextView

Iphone 改变Ray Wenderlich';s分析器不使用UITextView,iphone,xcode,nsattributedstring,Iphone,Xcode,Nsattributedstring,我试图将Ray Wenderlich的简单杂志教程中的解析器提升到我的项目中。他的解析器代码如下: 主控制器将文本字符串传递给解析器,解析器将对其进行解析,添加属性,然后使用viewDidLoad中的以下代码返回属性字符串: - (void)viewDidLoad { [super viewDidLoad]; NSString *path = [[NSBundle mainBundle] pathForResource:@"zombies" ofType:@"txt"];

我试图将Ray Wenderlich的简单杂志教程中的解析器提升到我的项目中。他的解析器代码如下:

主控制器将文本字符串传递给解析器,解析器将对其进行解析,添加属性,然后使用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];
}
当我使用Ios6时,我认为从解析器传回的属性字符串可以简单地添加到UITextView中,而不必处理CoreText。考虑到这一点,我将上述代码更改为:

    - (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];
}
但是,这现在会在将view.attributeText设置为attString的行上产生一个错误,称为“未知选择器已发送到实例”。我确信解析器返回的属性字符串是罪魁祸首,但在我的生命中,我无法找出原因!有什么想法吗


以下是运行时控制台的屏幕截图:


下一幅图像是导致问题的AttributeString中名为attString的详细信息的屏幕截图

最后,下面的代码部分是解析器获取其属性集的地方,这可能会导致上面的调试器中显示的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] ];

上面的“桥接”命令是由Xcode自动添加的,我知道这与ARC有关,但我并不真正理解它们,因此不知道这些是否是导致问题的原因

在我看来,您必须在iOS6之前的环境中运行—虽然您可能正在使用iOS6 SDK,但也可以在iOS5模拟器中运行。您肯定有一个
UITextView
,如果它是iOS6,那么
setAttributedText:
就可以了。 在iOS6之前,
UITextView
没有
attributedText
属性,因此尝试设置它将导致“未知选择器发送到实例”


如果您发现情况并非如此,请检查您的控制台输出。它可能会说未知选择器是什么…

在我看来,您一定是在iOS6之前的环境中运行的-虽然您可能正在使用iOS6 SDK,但您可能正在iOS5模拟器中运行。您肯定有一个
UITextView
,如果它是iOS6,那么
setAttributedText:
就可以了。 在iOS6之前,
UITextView
没有
attributedText
属性,因此尝试设置它将导致“未知选择器发送到实例”


如果您发现情况并非如此,请检查您的控制台输出。它可能会说未知的选择器是什么…

3整天后它被排序了

我的解决方案(感谢@Adam Eberbach为我指明了解析器!)是对Ray Wenderlich使用的解析器代码的一次相当大的改进。基本上,问题是我从他的示例中取消了CoreText编码,并试图在UITextView属性字符串中使用这些属性,因此UITextView不理解传递给它的内容

通过将解析器替换为基本NS属性而不是CG属性(从而改变了大部分代码!),UITextView就能够理解传递给它的内容。我能够保留他创造的同样的正则表达式,这是上帝的恩赐,因为我尽可能尝试,我无法理解所有这些符号的含义

我在原始帖子中最后一段代码的替换代码如下:

    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];
for(NSTextCheckingResult*b分块){
NSArray*parts=[[标记子字符串WithRange:b.range]

组件通过字符串分离:@“3整天后其已排序

我的解决方案(感谢@Adam Eberbach将我指向解析器!)是对Ray Wenderlich使用的解析器代码的一次相当大的改进。基本上,问题是我从他的示例中取消了CoreText编码,并试图在UITextView属性字符串中使用这些属性,因此UITextView不理解传递给它的是什么

通过用基本的NS属性而不是CG属性替换他的解析器(从而改变了他的大部分代码!),UITextView就能够理解传递给它的是什么。我能够保留他创建的相同正则表达式,这是上帝派来的,因为我尽我所能,我无法理解所有这些符号的含义

我在原始帖子中最后一段代码的替换代码如下:

    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];
for(NSTextCheckingResult*b分块){
NSArray*parts=[[标记子字符串WithRange:b.range]

componentsSeparatedByString:@“我是这么想的,我仔细检查了Ios6中的im是否存在。我还尝试向uitextview传递一个手动创建的字符串(而不是使用来自解析器的字符串)我完全不知道控制台怎么说-未知选择器是什么?请看添加到控制台输出原始问题中的屏幕截图只是一个想法-他创建的解析器会生成CoreText可以使用但UITextView不能使用的自定义属性吗?我注意到在他的自定义UIView中他s添加了#import,但由于我使用默认的UIView,我还没有向类中添加coretext功能?你能发布控制台的图片吗?NSLog吐出文本的位置?在那里你可以看到实际未识别的选择器是什么。这就是我的想法,我仔细检查了Ios6中的im是否存在。我还尝试将uitextview传递给手动创建的字符串(而不是使用来自解析器的字符串)我完全不知道控制台怎么说-未知选择器是什么?请看添加到控制台输出原始问题中的屏幕截图只是一个想法-他创建的解析器会生成CoreText可以使用但UITextView不能使用的自定义属性吗?我注意到在他的自定义UIView中他添加了#导入,但由于我使用默认的UIView,我没有添加coretext函数