- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我的屏幕上有一个 UITextView,用户应该只填写两行,当用户在第二行时,返回键应该变成完成。
如何限制 UITextView 中的行数?我搜索了很多,没有一个有用的结果!
我为 Swift 找到了这个答案:
locationNoteTextView.textContainer.maximumNumberOfLines = 2
self.locationNoteTextView.textContainer.lineBreakMode = NSLineBreakMode.ByClipping
没用,这样用户可以输入无限字符,但是在屏幕上看到的只是两行!
因此,如果您尝试打印 textView 的文本,您会发现一个灾难文本。
最佳答案
您需要实现 textView:shouldChangeTextInRange:replacementText:
。只要文本要更改,就会调用此方法。您可以使用文本属性访问 TextView 的当前内容。
使用 [textView.text stringByReplacingCharactersInRange:range withString:replacementText]
从传递的范围和替换文本构建新内容。
然后您可以计算行数并返回 YES
以允许更改或返回 NO
以拒绝它。
编辑:根据 OP 要求:
swift :
func sizeOfString (string: String, constrainedToWidth width: Double, font: UIFont) -> CGSize {
return (string as NSString).boundingRectWithSize(CGSize(width: width, height: DBL_MAX),
options: NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes: [NSFontAttributeName: font],
context: nil).size
}
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
let newText = (textView.text as NSString).stringByReplacingCharactersInRange(range, withString: text)
var textWidth = CGRectGetWidth(UIEdgeInsetsInsetRect(textView.frame, textView.textContainerInset))
textWidth -= 2.0 * textView.textContainer.lineFragmentPadding;
let boundingRect = sizeOfString(newText, constrainedToWidth: Double(textWidth), font: textView.font!)
let numberOfLines = boundingRect.height / textView.font!.lineHeight;
return numberOfLines <= 2;
}
Obj-C
- (CGSize) sizeOfString:(NSString*)str constrainedToWidth:(CGFloat)width andFont:(UIFont*)font
{
return [str boundingRectWithSize:CGSizeMake(width, DBL_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName: font}
context:nil].size;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSString *newText = [textView.text stringByReplacingCharactersInRange:range withString:text];
CGFloat textWidth = CGRectGetWidth(UIEdgeInsetsInsetRect(textView.frame, textView.textContainerInset));
textWidth -= 2.0 * textView.textContainer.lineFragmentPadding;
CGSize boundingRect = [self sizeOfString:newText constrainedToWidth:textWidth andFont:textView.font];
int numberOfLines = boundingRect.height / textView.font.lineHeight;
return numberOfLines <= 2;
}
关于ios - 如何限制 UITextView 中的行数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32889495/
我有一个包含未定义条目数的数据文件,如下所示: A B C D E.. 1 0 2 5 4 7 4 3 4 1 8 7 4 0 7 1 1 第一行代表工作时间,而不是暂停等交替方式。为了可
我需要有关小型 SQL 查询的帮助。考虑下表: TicketNo | Rules | Audit Result --------------------------------- P
我有一个非常大的表(~1 000 000 行)和带有联合、连接和 where 语句的复杂查询(用户可以选择不同的 ORDER BY 列和方向)。我需要获取分页的行数。如果我运行查询而不计算行数,它会很
我想获取数据帧的行数。 我可以通过 size(myDataFrame)[1] 实现这一点. 有更干净的方法吗? 最佳答案 如果您正在使用 DataFrames具体来说,那么你可以使用 nrow() :
是否可以在带有千位分隔符的 VIM 状态栏中显示行数,最好是自定义千位分隔符? 例子: set statusline=%L 应该导致“1,234,567”而不是“1234567”。 最佳答案 我找到了
我有一个非常基本的问题,但不知道该怎么做。如果 mysql 表中的行数增加,我想刷新页面。我已经尝试了一些不同的事情,比如在表中添加一个单独的列,如果行数和这个值相等,则值为 (id + 1),然后进
我的 mysql TB 中的行数(如 TB 信息中所示)是 11093,而自动递增 ID(从 1 开始)是 11361。为什么会这样? 最佳答案 删除的行不会重置 AI 索引。行数是当前表中的条目数,
我有一个 MySQL 表如下。 emp_no emp_name dob gender 1 A 1978-10-10 Male 2 B
ifstream inFile; inFile.open(filename); //open the input file stringstream strStream; strStream << i
SELECT * FROM table1 WHERE EXISTS (SELECT * FROM table2 WHERE *condition*) 例如,我可以检查是否有 3 行符合 table2
我正在尝试提取 SQL 表中的总行数。 我正在使用以下代码: $rowNum = mysql_query("SELECT COUNT(*) FROM Logs"); $count = mysql_fe
我想知道表格 View 的行宽是多少,UITableViewCell 文本标签的字体是什么,有人可以帮我吗? 最佳答案 NSLog(@"width: %f", cell.frame.size.widt
对于以下内容: def linecount(filename): count = 0 for x in open(filename): count += 1 r
感谢关注。 我用C语言写了一段代码来统计字数、行数和字符数。 while((c = fgetc(fp)) != EOF) { if((char)(c) == ' ' || (char)(c)
我是 matlab 的新手,只需要更改代码中的一个非常小的东西。我有以下矩阵: ans = 1 1 1 1 2 1 2 1
我只是想弄清楚如何确定行数,然后使该数字显示在 HTML 中。 我准备好的声明如下所示: if($stmt = $mysqli -> prepare("SELECT field1, field2, f
PDO 显然无法计算从选择查询返回的行数(mysqli 有 num_rows 变量)。 除了使用 count($results->fetchAll()) 之外,有没有办法做到这一点? 最佳答案 根据手
SELECT count(*) FROM Stack WHERE Id = 33478 GROUP BY SID Output: (No column name) 1 4 对于结果;有两排。怎么退货
IE。如果我们有一个包含400万行的表。 其中具有一个STATUS字段,该字段可以采用以下值:TO_WORK,BLOCKED或WORKED_CORRECTLY。 您是否会在一个仅会更改一次的字段上进行
所以在JTextArea中有一个getLineCount()是否有与JTextPane类似的东西,因为我可以找到任何东西。也许有不同的方法来获得它?我想获取当前存在的行数。 最佳答案 (正如您所指出的
我是一名优秀的程序员,十分优秀!