UITextView划分了行背景,但行高错误 [英] UITextView ruled line background but wrong line height

查看:90
本文介绍了UITextView划分了行背景,但行高错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UITextView where the user can create notes and save into a plist file. I want to be able to show lines just like a normal notebook. The problem I have is that the text won't align properly.

下面的图片很好地解释了问题。

The image below explains the problem quite well.

这是我用来创建像Notes.app

This is the background I use to create the lines like the Notes.app

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

This is my code for creating the background for my UITextView:

textView.font            = [UIFont fontWithName:@"MarkerFelt-Thin" size:19.0]; 
textView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"Notes.png"]];

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

I know that the UIFont.lineHeight property is only available in > iOS 4.x.

所以我想知道我的问题是否有其他解决方案?

So I wonder if there is another solution to my problem?

推荐答案

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

You should try and draw your lines programmatically rather than using an image. Here's some sample code of how you could accomplish that. You can subclass UITextView and override it's drawRect: method.

NoteView.h

NoteView.h

#import <UIKit/UIKit.h>
@interface NoteView : UITextView <UITextViewDelegate> {
}
@end

NoteView.m

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

MyViewController.h

MyViewController.h

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

    NoteView *note;
}

@property (nonatomic, retain) NoteView *note;

@end

MyViewController.m

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];
}

查看Apple的文档管理键盘,特别是移动内容位于键盘下面。它解释了如何倾听NSNotifcations并正确调整您的观点。

Take a look at Apple's documentation for Managing the Keyboard, specifically "Moving Content That Is Located Under the Keyboard". It explains how to listen for NSNotifcations and adjust your views properly.

这篇关于UITextView划分了行背景,但行高错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆