我想限制我的iPad
应用的用户在pdf上输出时最多输入20行。用户在UITextView
中输入文本(如下所示),然后将其转换为pdf文档。
我意识到我必须做一些计算,具体取决于输入时输入的文字。有什么方法可以帮助我吗? textFieldDidEndEditing
只允许我在输入后进行检查。
另外,由于这会更简单,有没有办法限制UITextField
可以的行数?
答案 0 :(得分: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上测试过。享受!
答案 1 :(得分:3)
以下内容适用于'\ 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);
}
答案 2 :(得分: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];
}
}
答案 3 :(得分:0)
更容易做到:
夫特:
textView.textContainer.maximumNumberOfLines = 10;
textView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;
的OBJ-C:
textView.textContainer.maximumNumberOfLines = 10;
[textView.layoutManager textContainerChangedGeometry:textView.textContainer];