【问题标题】:How to count number of lines in UITextField?如何计算 UITextField 中的行数?
【发布时间】:2014-11-26 05:29:14
【问题描述】:

我需要计算UITextField 中的行数。

我的UITextField 有一个UIBezierPath 数组,用于沿着UIImage 绘制文本。

UIBezierPath *rect = [UIBezierPath bezierPathWithRoundedRect:_image.frame cornerRadius:0];
[_txt addSubview:_image];
_txt.textContainer.exclusionPaths = @[rect];

显然,行数是不同的,因为有一个排除路径和这种通用方式,

 CGSize size  = [_txt.text
                sizeWithFont:_txt.font constrainedToSize:(CGSize){_txt.frame.size.width, INTMAX_MAX}
                lineBreakMode:NSLineBreakByWordWrapping];

返回错误的结果。

注意:_txtUITextField

谢谢。


测试用例:

参考:http://goo.gl/Ch9al6

【问题讨论】:

标签: ios objective-c iphone uitextfield uibezierpath


【解决方案1】:

UITextInputProtocol 中有一个方法叫做firstRectForRange: (doc here)。这可能会奏效。尝试将文本范围设置为支持字符串的末尾。

// given a UITextView* called textView
UITextPosition *beginning = textView.beginningOfDocument;
UITextPosition *p0 = [textView positionFromPosition:beginning offset:textView.text.length-1];
UITextPosition *p1 = [textView positionFromPosition:p0 offset:1];
UITextRange *textRange = [textView textRangeFromPosition:p0 toPosition:p1];
CGRect rect = [textView firstRectForRange:textRange];
// rect will be given in the coordinate space of textView

免责声明:即使是一个简单的案例,我也没有尝试过,而且您的案例看起来很重要。

【讨论】:

    猜你喜欢
    • 2013-05-16
    • 1970-01-01
    • 2015-09-15
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 2012-12-04
    • 1970-01-01
    • 2013-10-09
    相关资源
    最近更新 更多