简体   繁体   中英

Issue in UITextView beginningOfDocument in iOS 4.3

Below is the code which I am using and working fine in simulator iOS 5+, but giving issue in simulator 4.3

UITextPosition *begin = [self.tvQuestion positionFromPosition:self.tvQuestion.beginningOfDocument offset:nrange.location];

Following is the error statement:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITextView beginningOfDocument]: unrecognized selector sent to instance 0x5586050'

Could somebody advice on this? Thanks in advance!

The UITextInput protocol includes a beginningOfDocument selector since iOS 3.2.
But UITextView only adopted UITextInput since iOS 5.0. It was previously conforming to the UITextInputTraits protocol.

Source: search for UITextView here http://developer.apple.com/library/ios/#releasenotes/General/iOS50APIDiff/index.html

If you want it to work on iOS 4.x and later, you have to perform a check and do the computation by yourself on iOS 4:

CGRect newRect = CGRectZero;

if ([UITextView conformsToProtocol:@protocol(UITextInput)]) {

    // iOS 5 and later
    UITextPosition *begin = [self.tvQuestion positionFromPosition:self.tvQuestion.beginningOfDocument offset:nrange.location]; 
    UITextPosition *end = [self.tvQuestion positionFromPosition:begin offset:nrange.length]; 
    UITextRange *textRange = [self.tvQuestion textRangeFromPosition:begin toPosition:end]; 
    newRect = [self.tvQuestion firstRectForRange:textRange];

} else {

    // iOS 3.2 to 4.3
    // Compute text position manually

#define TEXT_VIEW_PADDING 8.f

    NSString *textContent = [self.tvQuestion.text substringToIndex:NSMaxRange(nrange)];

    NSDictionary *ctFontDescriptorAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                                self.tvQuestion.font.familyName, kCTFontFamilyNameAttribute,
                                                // Uncomment for bold fonts
                                                // [NSDictionary dictionaryWithObjectsAndKeys:
                                                // [NSNumber numberWithInt:kCTFontBoldTrait], kCTFontSymbolicTrait,
                                                // nil], kCTFontTraitsAttribute,
                                                nil];

    CTFontDescriptorRef ctFontDescriptor = CTFontDescriptorCreateWithAttributes((CFDictionaryRef)ctFontDescriptorAttributes);
    CTFontRef ctFont = CTFontCreateWithFontDescriptor(ctFontDescriptor, self.tvQuestion.font.pointSize, NULL);

    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:textContent
                                                                         attributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                                 (id)ctFont, kCTFontAttributeName,
                                                                                 nil]];

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedText);
    CFRange globalRange = CFRangeMake(0, [attributedText length]);

    CGRect textRect = CGRectInset((CGRect){CGPointZero, self.tvQuestion.contentSize}, TEXT_VIEW_PADDING, TEXT_VIEW_PADDING);
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, globalRange, CGPathCreateWithRect((CGRect){CGPointZero, textRect.size}, NULL), NULL);

    CFArrayRef lines = CTFrameGetLines(frame);
    NSInteger nbLines = CFArrayGetCount(lines);
    CGPoint *lineOrigins = calloc(sizeof(CGPoint), nbLines);
    CTFrameGetLineOrigins(frame, CFRangeMake(0, 0), lineOrigins);

    CGFloat ascent = CTFontGetAscent(ctFont);
    CGFloat descent = CTFontGetDescent(ctFont);
    CGFloat leading = CTFontGetLeading(ctFont);

    if (leading < 0) {
        leading = 0;
    }
    leading = floor(leading + 0.5);

    CGFloat lineHeight = floor(ascent + 0.5) + floor(descent + 0.5) + leading;
    CGFloat firstLineYOffset = 0.f;

    for (NSInteger i = 0; i < nbLines; i++) {

        CTLineRef line = CFArrayGetValueAtIndex(lines, i);
        CFRange lineRange = CTLineGetStringRange(line);
        CGPoint lineOrigin = lineOrigins[i];
        CGFloat ascent, descent, leading, width;
        width = CTLineGetTypographicBounds(line, &ascent,  &descent, &leading);

        if (i == 0) {
            firstLineYOffset = lineOrigin.y;
        }
        lineOrigin.y = (lineOrigin.y - firstLineYOffset) * -1.f;


        if (nrange.location >= lineRange.location && nrange.location < lineRange.location + lineRange.length) {

            CGFloat secOffset;
            CGFloat startOffset = CTLineGetOffsetForStringIndex(line, nrange.location, &secOffset);

            CGFloat rectWidth;
            if (nrange.location + nrange.length <= lineRange.location + lineRange.length) {
                CGFloat endOffset = CTLineGetOffsetForStringIndex(line, nrange.location + nrange.length, &secOffset);
                rectWidth = endOffset - startOffset;
            } else {
                rectWidth = width - 5 - startOffset;
            }

            newRect = (CGRect)
            {
                {
                    lineOrigin.x + TEXT_VIEW_PADDING + startOffset,
                    lineOrigin.y + TEXT_VIEW_PADDING + i
                }, {
                    rectWidth,
                    lineHeight
                }
            };

            break;
        }
    }

    free(lineOrigins);
    CFRelease(frame);
    CFRelease(framesetter);
    CFRelease(ctFont);
    CFRelease(ctFontDescriptor);

}

Although UITextView does not conform to UITextInput protocol on iOS 4.x, its subviews does. The following code works iOS 4.x and later.

UIView<UITextInput> *t = [textView.subviews objectAtIndex:0];
assert([t conformsToProtocol:@protocol(UITextInput)]);
// use t for the view conforming to UITextInput
UITextPosition *beginning = t.beginningOfDocument; // OK

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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