gpt4 book ai didi

ios - 使用NSAttributedString不正确滚动UITextView

转载 作者:行者123 更新时间:2023-12-01 16:46:22 28 4
gpt4 key购买 nike

我想制作一个同步的节目歌词应用,并将所有歌词显示在UITextView上。为了突出显示当前的歌词,我在NSAttributedStringUITextView中添加了背景色。存储在NSRange中的所有行的NSArray

我的代码非常简单,当点击按钮时,将突出显示线向下移动(通过设置UITextView的contentOffset)。但是这里发生了一个奇怪的问题。一开始,UITextView可以正确滚动,但是当contentOffsetUITextView大于其frame.size.height时,它便被修复。

这是我的代码:

//View controller
#import "ViewController.h"

@interface ViewController () {
NSUInteger globelIndex;
NSArray *textRanges;
NSMutableAttributedString *attributedText;
}

@property (weak, nonatomic) IBOutlet UITextView *lyricView;
@property (strong, nonatomic) NSTimer *mainTimer;

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

NSString *rawText = [self readFile];
globelIndex = 0;
[self initTextLines:rawText];
attributedText = [[NSMutableAttributedString alloc] initWithString:rawText
attributes:@{NSBackgroundColorAttributeName: [UIColor orangeColor]}];

self.lyricView.attributedText = [attributedText copy];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)manualNextLine:(UIButton *)sender {
[self updateTextView];
}

- (IBAction)autoNextLineUsingNSTimer:(UIButton *)sender {
self.mainTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTextView) userInfo:nil repeats:YES];
}

- (void)updateTextView {
if (self.lyricView.contentOffset.y >= self.lyricView.contentSize.height &&
self.mainTimer)
{
self.mainTimer = nil;
return;
}

NSMutableAttributedString *mat = [attributedText mutableCopy];
NSValue *value = [textRanges objectAtIndex:globelIndex];
[mat addAttribute:NSBackgroundColorAttributeName value:[UIColor whiteColor] range:[value rangeValue]];

self.lyricView.attributedText = [mat copy];

globelIndex += 1;

// self.textView.contentOffset = CGPointMake(self.textView.contentOffset.x, self.textView.contentOffset.y + 24);

// [self.textView scrollRangeToVisible:[value rangeValue]];
CGPoint newOffset = CGPointMake(self.lyricView.contentOffset.x, self.lyricView.contentOffset.y + 20);
[self.lyricView setContentOffset:newOffset animated:NO];

NSLog(@"[%@ %@] h: %f b: %f a: %f", NSStringFromClass([self class]), NSStringFromSelector(_cmd), self.lyricView.contentSize.height, newOffset.y, self.lyricView.contentOffset.y);
}

#pragma mark - helper

- (void)initTextLines:(NSString *)rawText {
NSMutableArray *result = [@[] mutableCopy];
NSArray *t = [rawText componentsSeparatedByString:@"\r\n"];
__block int index = 0;

[t enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSString *s = obj;
NSRange range = NSMakeRange(index, s.length + 2);
[result addObject:[NSValue valueWithRange:range]];
index += s.length + 2;
}];


textRanges = [result copy];
}

- (NSString *)readFile {
NSError *error = nil;
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"二人の季節が-ささきのぞみ-想い" withExtension:@"lrc"];
NSString *content = [NSString stringWithContentsOfURL:fileURL encoding:NSUTF8StringEncoding error:&error];
if (error) {
if (DEBUG) NSLog(@"[%@ %@] Error: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), error.localizedDescription);
abort();
}
return content;
}

@end

我所有的代码都在ViewController中,并且 StoryBoard具有两个UIButton和一个UITextView。
我想知道的是“为什么UITextView.contentOffset大于某个大小时不能更改”。

最佳答案

也许重写setContentOffset:animated对您有帮助!

这是您可以借用的一些示例代码:
https://github.com/360/Three20/blob/master/src/Three20UI/Sources/TTTextView.m

#import "Three20UI/TTTextView.h"

// UI
#import "Three20UI/UIViewAdditions.h"

@implementation TTTextView

@synthesize autoresizesToText = _autoresizesToText;
@synthesize overflowed = _overflowed;

- (void)setContentOffset:(CGPoint)offset animated:(BOOL)animated {
if (_autoresizesToText) {
if (!_overflowed) {
// In autosizing mode, we don't ever allow the text view to scroll past zero
// unless it has past its maximum number of lines
[super setContentOffset:CGPointZero animated:animated];

} else {
// If there is an overflow, we force the text view to keep the cursor at the bottom of the
// view.
[super setContentOffset: CGPointMake(offset.x, self.contentSize.height - self.height)
animated: animated];
}

} else {
[super setContentOffset:offset animated:animated];
}
}


@end

关于ios - 使用NSAttributedString不正确滚动UITextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19686674/

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