Swift 4 と iOS 11 では、NSTextContainer
というプロパティがありますexclusionPaths
。exclusionPaths
次の宣言があります。
テキスト コンテナーでテキストが表示されない領域を表すパス オブジェクトの配列。
var exclusionPaths: [UIBezierPath] { get set }
また、UIBezierPath
というプロパティがありますusesEvenOddFillRule
。usesEvenOddFillRule
次の宣言があります。
パスの描画に偶奇ワインディング ルールが使用されているかどうかを示すブール値。
var usesEvenOddFillRule: Bool { get set }
を使用usesEvenOddFillRule
すると、わずか数行のコードで円を囲む除外パスを作成できます。
var exclusionPath: UIBezierPath {
let path = UIBezierPath(ovalIn: bounds)
path.append(UIBezierPath(rect: bounds))
path.usesEvenOddFillRule = true
return path
}
次のUITextView
およびサブクラスは、 およびプロパティUIViewController
を使用して円内にテキストを表示する方法を示しています。NSTextContainer
exclusionPaths
UIBezierPath
usesEvenOddFillRule
TextView.swift
import UIKit
class TextView: UITextView {
convenience init() {
self.init(frame: .zero, textContainer: nil)
}
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
isScrollEnabled = false
isEditable = false
textContainerInset = .zero
self.textContainer.lineBreakMode = .byTruncatingTail
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
var exclusionPath: UIBezierPath {
let path = UIBezierPath(ovalIn: bounds)
path.append(UIBezierPath(rect: bounds))
path.usesEvenOddFillRule = true
return path
}
}
extension TextView {
// Draw circle
override func draw(_ rect: CGRect) {
UIColor.orange.setFill()
let path = UIBezierPath(ovalIn: rect)
path.fill()
}
// Draw exclusion path
/*
override func draw(_ rect: CGRect) {
UIColor.orange.setFill()
exclusionPath.fill()
}
*/
}
ViewController.swift
import UIKit
class ViewController: UIViewController {
let textView = TextView()
override func viewDidLoad() {
super.viewDidLoad()
let string = "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda."
textView.attributedText = NSAttributedString(string: string)
view.addSubview(textView)
textView.translatesAutoresizingMaskIntoConstraints = false
let horizontalConstraint = textView.centerXAnchor.constraint(equalTo: view.centerXAnchor)
let verticalConstraint = textView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
let widthConstraint = textView.widthAnchor.constraint(equalToConstant: 240)
let heightConstraint = textView.heightAnchor.constraint(equalToConstant: 240)
NSLayoutConstraint.activate([horizontalConstraint, verticalConstraint, widthConstraint, heightConstraint])
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
textView.textContainer.exclusionPaths = [textView.exclusionPath]
}
}
のいずれかの実装を選択draw(_:)
すると、次のように表示されます。
