【问题标题】:Limiting Characters/ rows In UITextView using height, width of TextView & font size of character.在 UITextView 中使用 TextView 的高度、宽度和字符的字体大小来限制字符/行。
【发布时间】:2011-11-17 18:11:31
【问题描述】:

我在单个页面上有 n 个 UITextView,UITextView 的高度和宽度分别为 200*300(因不同的文本视图而异)。我需要限制最大值。用户可以在每个 textview 中输入的文本行数/字符数,具体取决于 TextView 的 ht 和宽度。我有需要在文本视图中输入的字符的字体大小。那么我该如何实施呢?

【问题讨论】:

    标签: iphone objective-c ios ipad uitextview


    【解决方案1】:
    textfield number of lines:
    
    If you are using iOS 3, you need to use the leading property:
    int numLines = txtview.contentSize.height / txtview.font.leading;
    If you are using iOS 4, you need to use the lineHeight property:
    int numLines = txtview.contentSize.height / txtview.font.lineHeight;
    

    【讨论】:

    • @如何知道用户当前在哪一行输入文字?
    • 最后一个数字是什么意思
    • numlines =2 表示你正在输入 2 行
    【解决方案2】:

    你可以做的是,你可以计算文本的高度,你可能会限制文本视图的高度和宽度。 现在您可以比较文本高度和文本视图高度。并按照您的要求做任何事情。

    计算文字高度:-

    CGSize labelsize;
    NSString *text=@"your text";
    [textView setFont:[UIFont fontWithName:@"Helvetica"size:14]];
    labelsize=[text sizeWithFont:textView.font constrainedToSize:CGSizeMake(280, 2000.0) lineBreakMode:UILineBreakModeWordWrap];
    NSLog(@"%f",labelsize.height);
    if(labelsize.height>300)
    {
    //show text with 300 height
    }
    else
    {
    //show full text
    }
    

    【讨论】:

      猜你喜欢
      • 2015-12-14
      • 1970-01-01
      • 2011-12-10
      • 2012-12-25
      • 1970-01-01
      • 2017-06-10
      • 2011-07-19
      • 1970-01-01
      • 2015-06-26
      相关资源
      最近更新 更多