Created
October 13, 2021 05:36
-
-
Save yosshi4486/549e308222958d864da0482ba1066708 to your computer and use it in GitHub Desktop.
Learn behaviors of lineFragmentRect and lineFragmentUsedRect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// UITextViewPractices | |
// | |
// Created by yosshi4486 on 2021/10/13. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
let textView: UITextView = .init() | |
override func loadView() { | |
let view = UIView() | |
view.backgroundColor = .white | |
textView.isEditable = false | |
textView.isScrollEnabled = false | |
textView.isSelectable = false | |
view.addSubview(textView) | |
textView.translatesAutoresizingMaskIntoConstraints = false | |
NSLayoutConstraint.activate([ | |
textView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor), | |
textView.bottomAnchor.constraint(equalTo: view.bottomAnchor), | |
textView.leadingAnchor.constraint(equalTo: view.leadingAnchor), | |
textView.trailingAnchor.constraint(equalTo: view.trailingAnchor) | |
]) | |
self.view = view | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
textView.text = """ | |
「ではみなさんは、そういうふうに川だと云いわれたり、乳の流れたあとだと云われたりしていたこのぼんやりと白いものがほんとうは何かご承知ですか。」先生は、黒板に吊つるした大きな黒い星座の図の、上から下へ白くけぶった銀河帯のようなところを指さしながら、みんなに問といをかけました。 | |
カムパネルラが手をあげました。それから四五人手をあげました。ジョバンニも手をあげようとして、急いでそのままやめました。たしかにあれがみんな星だと、いつか雑誌で読んだのでしたが、このごろはジョバンニはまるで毎日教室でもねむく、本を読むひまも読む本もないので、なんだかどんなこともよくわからないという気持ちがするのでした。 | |
ところが先生は早くもそれを見附みつけたのでした。 | |
「ジョバンニさん。あなたはわかっているのでしょう。」 | |
ジョバンニは勢いきおいよく立ちあがりましたが、立って見るともうはっきりとそれを答えることができないのでした。ザネリが前の席からふりかえって、ジョバンニを見てくすっとわらいました。ジョバンニはもうどぎまぎしてまっ赤になってしまいました。先生がまた云いました。 | |
「大きな望遠鏡で銀河をよっく調べると銀河は大体何でしょう。」 | |
やっぱり星だとジョバンニは思いましたがこんどもすぐに答えることができませんでした。 | |
先生はしばらく困ったようすでしたが、眼めをカムパネルラの方へ向けて、 | |
「ではカムパネルラさん。」と名指しました。するとあんなに元気に手をあげたカムパネルラが、やはりもじもじ立ち上ったままやはり答えができませんでした。 | |
""" | |
} | |
func printLineFragmentUsedRects() { | |
let leftSideRange = (textView.text as NSString).range(of: "毎日教室") | |
let rightSideRange = (textView.text as NSString).range(of: "本を読む") | |
let leftSideFragmentRect = textView.layoutManager.lineFragmentUsedRect(forGlyphAt: leftSideRange.location, effectiveRange: nil) | |
let rightSideFragmentRect = textView.layoutManager.lineFragmentUsedRect(forGlyphAt: rightSideRange.location, effectiveRange: nil) | |
print("\(#function)\(leftSideFragmentRect)") | |
print("\(#function)\(rightSideFragmentRect)") | |
} | |
func printLineFragmentRects() { | |
let leftSideRange = (textView.text as NSString).range(of: "毎日教室") | |
let rightSideRange = (textView.text as NSString).range(of: "本を読む") | |
let leftSideFragmentUsedRect = textView.layoutManager.lineFragmentRect(forGlyphAt: leftSideRange.location, effectiveRange: nil) | |
let rightSideFragmentUsedRect = textView.layoutManager.lineFragmentRect(forGlyphAt: rightSideRange.location, effectiveRange: nil) | |
print("\(#function)\(leftSideFragmentUsedRect)") | |
print("\(#function)\(rightSideFragmentUsedRect)") | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
let centerCircleExclusionPath = UIBezierPath(arcCenter: .init(x: view.bounds.midX, y: 150), radius: 100, startAngle: 0, endAngle: CGFloat(Double.pi) * 2, clockwise: true) | |
textView.textContainer.exclusionPaths = [centerCircleExclusionPath] | |
printLineFragmentRects() | |
printLineFragmentUsedRects() | |
} | |
} | |
/* | |
Outputs in my environment: | |
printLineFragmentRects()(0.0, 138.0, 114.0, 13.8) | |
printLineFragmentRects()(314.0, 138.0, 114.0, 13.8) | |
printLineFragmentUsedRects()(0.0, 138.0, 105.4, 13.800000000000011) | |
printLineFragmentUsedRects()(314.0, 138.0, 106.0, 13.800000000000011) | |
Used rects are not equal to results of normal lineFragmentRects. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment