gpt4 book ai didi

iphone - UITextView 格线背景但错误的行高

转载 作者:太空狗 更新时间:2023-10-30 03:10:24 27 4
gpt4 key购买 nike

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

下图很好地解释了这个问题。

My print screen explains the problem quite well

这是我用来创建像 Notes.app 这样的线条的背景 enter image description here

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

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

我知道 UIFont.lineHeight 属性仅在 > iOS 4.x 中可用。

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

最佳答案

您应该尝试以编程方式绘制线条,而不是使用图像。以下是您如何完成该操作的一些示例代码。您可以子类化 UITextView 并覆盖它的 drawRect: 方法。

笔记 View .h

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

笔记 View .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

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

NoteView *note;
}

@property (nonatomic, retain) NoteView *note;

@end

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 的 Managing the Keyboard 文档,特别是“移动位于键盘下方的内容”。它解释了如何监听 NSNotifcations 并正确调整您的 View 。

关于iphone - UITextView 格线背景但错误的行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5375350/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com