7

我想限制我的iPad应用程序的用户在 pdf 上输出时最多输入 20 行。用户在其中输入文本UITextView(如下所示),然后将其转换为 pdf 文档。

在此处输入图像描述

我意识到我将不得不根据他们键入时输入的文本进行一些计算。有什么方法可以帮助我吗?textFieldDidEndEditing只允许我在输入后检查。

另外,由于这会简单得多,有没有办法限制 a 的行数UITextField

4

4 回答 4

11

您与textFieldDidEndEditing委托方法很接近。

更好的方法是响应textView:shouldChangeTextInRange:replacementText:委托方法,然后查看即将发生的更改是否会导致更多行被允许。

这是有关如何执行此操作的示例:

- (BOOL) textView:(UITextView *)textView
    shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    static const NSUInteger MAX_NUMBER_OF_LINES_ALLOWED = 3;

    NSMutableString *t = [NSMutableString stringWithString:
        self.textView.text];
    [t replaceCharactersInRange: range withString: text];

    NSUInteger numberOfLines = 0;
    for (NSUInteger i = 0; i < t.length; i++) {
        if ([[NSCharacterSet newlineCharacterSet]
                characterIsMember: [t characterAtIndex: i]]) {
            numberOfLines++;
        }
    }

    return (numberOfLines < MAX_NUMBER_OF_LINES_ALLOWED);
}

我在https://github.com/st3fan/StackOverflow/tree/master/TextViewWithLineLimit上放了一个完整的示例。在 iOS 7.0.2 上测试。享受!

于 2013-10-20T15:08:06.277 回答
4

以下适用于 '\n' + 换行支持。

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.AutoresizingMask   = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        self.AutocorrectionType = UITextAutocorrectionTypeNo;
        self.KeyboardAppearance = UIKeyboardAppearanceAlert;
        self.font               = [UIFont fontWithName:@"Avenir" size:14];

        self.maximumNumberOfLinesAllowed = 4;

        self.delegate           = self;
    }

    return self;
}


- (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString*)aText
{
    NSMutableString *t = [NSMutableString stringWithString: self.text];
    [t replaceCharactersInRange: aRange withString: aText];

    // First check for standard '\n' (newline) type characters.
    NSUInteger numberOfLines = 0;
    for (NSUInteger i = 0; i < t.length; i++) {
        if ([[NSCharacterSet newlineCharacterSet] characterIsMember: [t characterAtIndex: i]]) {
            numberOfLines++;
        }
    }

    if (numberOfLines >= self.maximumNumberOfLinesAllowed)
        return NO;


    // Now check for word wrapping onto newline.
    NSAttributedString *t2 = [[NSAttributedString alloc]
                             initWithString:[NSMutableString stringWithString:t] attributes:@{NSFontAttributeName:self.font}];

    __block NSInteger lineCount = 0;

    CGFloat maxWidth   = self.frame.size.width;
    NSLog(@"maxWidth = %.02f", maxWidth);

    NSTextContainer *tc = [[NSTextContainer alloc] initWithSize:CGSizeMake(maxWidth, CGFLOAT_MAX)];
    NSLayoutManager *lm = [[NSLayoutManager alloc] init];
    NSTextStorage   *ts = [[NSTextStorage alloc] initWithAttributedString:t2];
    [ts addLayoutManager:lm];
    [lm addTextContainer:tc];
    [lm enumerateLineFragmentsForGlyphRange:NSMakeRange(0,lm.numberOfGlyphs)
                                 usingBlock:^(CGRect rect,
                                              CGRect usedRect,
                                              NSTextContainer *textContainer,
                                              NSRange glyphRange,
                                              BOOL *stop)
     {
         lineCount++;
     }];

//    NSLog(@"%d", lineCount);

    return (lineCount <= self.maximumNumberOfLinesAllowed);

}
于 2013-11-24T04:54:33.960 回答
2

为了限制 UITextView 中的行数,我只是这样做:

在以下 UITextViewDelegate 方法中:

- (void)textViewDidChange:(UITextView *)textView
{
    NSUInteger maxNumberOfLines = 5;
    NSUInteger numLines = textView.contentSize.height/textView.font.lineHeight;
    if (numLines > maxNumberOfLines)
    {
        textView.text = [textView.text substringToIndex:textView.text.length - 1];
    }
}
于 2014-04-02T14:17:35.953 回答
0

更容易做的是:

迅速:

textView.textContainer.maximumNumberOfLines = 10;
textView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;

对象-C:

textView.textContainer.maximumNumberOfLines = 10;
[textView.layoutManager textContainerChangedGeometry:textView.textContainer];
于 2017-07-20T13:17:32.890 回答