Skip to content

Commit

Permalink
スクロール位置の計算を、行単位から画面全体の縦幅の比率の形に変更します。
Browse files Browse the repository at this point in the history
また、textView.layoutManager.allowsNonContiguousLayout = false にして長い内容の場合にスクロール位置がおかしなことになる(レンダリングされていない部分まで飛ぼうとすると飛べない)ことをなくします。
  • Loading branch information
limura committed Aug 28, 2024
1 parent ceafb10 commit cfd023b
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions NovelSpeaker/SpeechViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ class SpeechViewController: UIViewController, StorySpeakerDeletgate, RealmObserv
nextChapterButton.accessibilityLabel = NSLocalizedString("SpeechViewController_NextChapterButton_VoiceOverTitle", comment: "次のページ")

setCustomUIMenu()

self.textView.layoutManager.allowsNonContiguousLayout = false
}

func assignSwipeRecognizer() {
Expand Down Expand Up @@ -647,28 +649,18 @@ class SpeechViewController: UIViewController, StorySpeakerDeletgate, RealmObserv
}

func textViewScrollTo(readLocation:Int) {
let minLines = 2
let maxLines = 4

let beginOffsetY = textView.contentOffset.y
guard let startPosition = textView.position(from: textView.beginningOfDocument, offset: readLocation) else {
return
}

let startRect = textView.caretRect(for: startPosition)
let lineHeight = textView.font?.lineHeight ?? 0

let minHeight = lineHeight * CGFloat(minLines)
let maxHeight = lineHeight * CGFloat(maxLines)

let visibleHeight = textView.bounds.height
let heightToUse = min(maxHeight, visibleHeight)

let targetY = max(0, startRect.origin.y - visibleHeight * 0.7)
let visibleRect = CGRect(x: startRect.origin.x,
y: startRect.origin.y,
y: targetY,
width: startRect.size.width,
height: min(heightToUse, visibleHeight))

textView.scrollRectToVisible(visibleRect, animated: true)
height: visibleHeight)
self.textView.scrollRectToVisible(visibleRect, animated: true)
}

func pushToEditStory() {
Expand Down

0 comments on commit cfd023b

Please sign in to comment.