UITextView统一了行背景,但行高错误

Fer*_*ndo 27 iphone objective-c uitextview

我有一个UITextView,用户可以在其中创建笔记并保存到plist文件中.我希望能够像普通笔记本一样显示线条.我遇到的问题是文本无法正确对齐.

下图很好地解释了问题.

我的打印屏幕很好地解释了这个问题

这是我用来创建像Notes.app这样的行的背景 在此输入图像描述

这是我为我创建背景的代码UITextView:

textView.font            = [UIFont fontWithName:@"MarkerFelt-Thin" size:19.0]; 
textView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"Notes.png"]];
Run Code Online (Sandbox Code Playgroud)

我知道该UIFont.lineHeight属性仅适用于> iOS 4.x.

所以我想知道我的问题是否有另一种解决方案?

Ado*_*lfo 57

您应该尝试以编程方式绘制线条而不是使用图像.这里有一些示例代码,说明如何实现这一目标.您可以子类化UITextView并覆盖它的drawRect:方法.

NoteView.h

#import <UIKit/UIKit.h>
@interface NoteView : UITextView <UITextViewDelegate> {
}
@end
Run Code Online (Sandbox Code Playgroud)

NoteView.m

#import "NoteView.h"

@implementation NoteView

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:1.0f green:1.0f blue:0.6f alpha:1.0f];
        self.font = [UIFont fontWithName:@"MarkerFelt-Thin" size:19];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {

    //Get the current drawing context   
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    //Set the line color and width
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.2f].CGColor);
    CGContextSetLineWidth(context, 1.0f);
    //Start a new Path
    CGContextBeginPath(context);

    //Find the number of lines in our textView + add a bit more height to draw lines in the empty part of the view
    NSUInteger numberOfLines = (self.contentSize.height + self.bounds.size.height) / self.font.leading;

    //Set the line offset from the baseline. (I'm sure there's a concrete way to calculate this.)
    CGFloat baselineOffset = 6.0f;

    //iterate over numberOfLines and draw each line
    for (int x = 0; x < numberOfLines; x++) {
        //0.5f offset lines up line with pixel boundary
        CGContextMoveToPoint(context, self.bounds.origin.x, self.font.leading*x + 0.5f + baselineOffset);
        CGContextAddLineToPoint(context, self.bounds.size.width, self.font.leading*x + 0.5f + baselineOffset);
    }

    //Close our Path and Stroke (draw) it
    CGContextClosePath(context);
    CGContextStrokePath(context);
}

@end
Run Code Online (Sandbox Code Playgroud)

MyViewController.h

#import <UIKit/UIKit.h>
#import "NoteView.h"
@interface MyViewController : UIViewController <UITextViewDelegate> {

    NoteView *note;
}

@property (nonatomic, retain) NoteView *note;

@end
Run Code Online (Sandbox Code Playgroud)

MyViewController.m

#import "MyViewController.h"
#import "NoteView.h"

#define KEYBOARD_HEIGHT 216

@implementation MyViewController
@synthesize note;

- (void)loadView {
    [super loadView];
    self.note = [[[NoteView alloc] initWithFrame:self.view.bounds] autorelease];
    [self.view addSubview:note];
    note.delegate = self;
    note.text = @"This is the first line.\nThis is the second line.\nThis is the ... line.\nThis is the ... line.\nThis is the ... line.\nThis is the ... line.\nThis is the ... line.\n";
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [note setNeedsDisplay];
}

- (void)textViewDidBeginEditing:(UITextView *)textView {
    CGRect frame = self.view.bounds;
    frame.size.height -= KEYBOARD_HEIGHT;
    note.frame = frame;
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    note.frame = self.view.bounds;
}

- (void)dealloc {
    [note release];
    [super dealloc];
}
Run Code Online (Sandbox Code Playgroud)

请查看Apple有关管理键盘的文档,特别是"移动位于键盘下的内容".它解释了如何监听NSNotifcations并正确调整您的视图.

  • 不要忘记在`initWithFrame中添加`self.contentMode = UIViewContentModeRedraw;`这样文本和行一起滚动.否则,绝对是非常棒的解决方案! (5认同)
  • 我不确定它什么时候坏了,但是font.leading属性总是为我返回0.相反,你应该使用在iOS 9上完全正常的font.lineHeight (2认同)