0

I have a UITextView in it's own class, and I'm trying to get the maximum number of lines to work so that the text doesn't go beyond the bounds of the UITextView, along with character wrapping to work, as it wants to always be in word wrapping mode. The line height (4.9) is working however.

I'm not sure as to what is causing this to malfunction. I'd appreciate any help offered.

Here is the code I'm using:

CustomTextView.h

#import <UIKit/UIKit.h>

@interface CustomTextView : UITextView <NSLayoutManagerDelegate>

@end

CustomTextView.m

#import "CustomTextView.h"

@implementation CustomTextView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        self.font = [UIFont systemFontOfSize:21.0];
        self.dataDetectorTypes = UIDataDetectorTypeAll;
        self.layoutManager.delegate = self;
        self.tintColor = [UIColor companyBlue];
        [self setLinkTextAttributes:@{NSForegroundColorAttributeName:[UIColor companyBlue]}];
        self.contentInset = UIEdgeInsetsMake(0.5, 0, 0, 0);
        self.scrollEnabled = NO;
        self.textContainer.maximumNumberOfLines = 9;
        self.textContainer.lineBreakMode = NSLineBreakByCharWrapping;
    }
    return self;
}

- (CGFloat)layoutManager:(NSLayoutManager *)layoutManager lineSpacingAfterGlyphAtIndex:(NSUInteger)glyphIndex withProposedLineFragmentRect:(CGRect)rect
{
    return 4.9;
}

@end
klcjr89
  • 5,862
  • 10
  • 58
  • 91
  • Have you tried `self.numberOfLines = 9`. I can't find anything on `maximumNumberOfLines`. – Putz1103 Feb 19 '14 at 02:46
  • maximumNumberOfLines is a property of UITextView's textContainer property – klcjr89 Feb 19 '14 at 02:49
  • That didn't answer the question. Have you tried `self.numberOfLines = 9`? – Putz1103 Feb 19 '14 at 02:54
  • There is no numberOfLines property on UITextView – klcjr89 Feb 19 '14 at 02:54
  • Oh piece of garbage... UILabel has `numberOfLines`, not `UITextView`. – Putz1103 Feb 19 '14 at 02:56
  • This (http://stackoverflow.com/a/5228029/1671729) is pretty much the only way I know of limiting the number of lines in a `UITextView`. Basically during editing you check the size and see if the new edit is too long, then disallow the edit. It would be placed in your delegate, not in your subclass. – Putz1103 Feb 19 '14 at 02:57
  • The answer below that one mentions textContainer for iOS 7, which is what I would like to use – klcjr89 Feb 19 '14 at 03:18

1 Answers1

0

I ended up settling for a different line break mode unfortunately to solve this: I realized that its supposedly not a bug to not wrap space characters, like the stock Messages app does, I just thought it was unusual to not wrap characters, including spaces.

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        self.font = [UIFont systemFontOfSize:21.0];
        self.dataDetectorTypes = UIDataDetectorTypeAll;
        self.layoutManager.delegate = self;
        self.tintColor = [UIColor companyBlue];
        [self setLinkTextAttributes:@{NSForegroundColorAttributeName:[UIColor companyBlue]}];
        self.scrollEnabled = NO;
        self.textContainerInset = UIEdgeInsetsMake(8.5, 0, 0, 0);
        self.textContainer.maximumNumberOfLines = 9;
        self.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;
    }
    return self;
}
klcjr89
  • 5,862
  • 10
  • 58
  • 91