How to truncate UITextView contents to fit a reduced size?

前端 未结 4 424
轻奢々
轻奢々 2021-02-06 07:05

I\'m having real trouble getting my head around this issue.

As the title suggests, I have several UITextViews on a view in an iPhone application. I am programmatically c

4条回答
  •  一个人的身影
    2021-02-06 07:55

    You can do a character count. For example, if you have UITextField like this:

    +--------------------+
    |This is a string in |
    |a text view.        |
    +--------------------+
    

    you have something like 20 characters per line. If you know that number you can simply truncate your string by -substringToIndex: method.

    int maxCharacters = 40; // change it to your max
    if([myString length] > maxCharacters) {
        myString = [myString substringToIndex:maxCharacters];
    }
    

    You can also think about UILabel. Since you need only 2 lines of text, UILabel's numberOfLines property can solve your problem.

提交回复
热议问题