Mit Hilfe von @yaslam habe ich in Core Text ein UILabel erstellt, das japanischen Text in horizontaler und vertikaler Richtung mit Furigana unter Verwendung von CTRubyAnnotation zeigt. Leider habe ich ein Problem. Ich muss dieses Label in einer benutzerdefinierten Zelle verwenden, und ich brauche, dass die Zelle die Höhe der Zelle basierend auf Text dynamisch ändert. aber arbeite nicht. die Zelle erweitert nichtBenutzerdefiniertes UITableViewCell mit Kerntext
Können Sie mir helfen?
Vielen Dank
Hier Code ist
import UIKit
protocol SimpleVerticalGlyphViewProtocol {
}
extension SimpleVerticalGlyphViewProtocol {
func drawContext(_ attributed:NSMutableAttributedString, textDrawRect:CGRect, isVertical:Bool) {
guard let context = UIGraphicsGetCurrentContext() else { return }
var path:CGPath
if isVertical {
context.rotate(by: .pi/2)
context.scaleBy(x: 1.0, y: -1.0)
path = CGPath(rect: CGRect(x: textDrawRect.origin.y, y: textDrawRect.origin.x, width: textDrawRect.height, height: textDrawRect.width), transform: nil)
}
else {
context.textMatrix = CGAffineTransform.identity
context.translateBy(x: 0, y: textDrawRect.height)
context.scaleBy(x: 1.0, y: -1.0)
path = CGPath(rect: textDrawRect, transform: nil)
}
let framesetter = CTFramesetterCreateWithAttributedString(attributed)
let frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, attributed.length), path, nil)
CTFrameDraw(frame, context)
}
}
class CustomLabel: UILabel, SimpleVerticalGlyphViewProtocol {
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
override func drawText(in rect: CGRect) {
let attributed = NSMutableAttributedString(attributedString: self.attributedText!)
let isVertical = false // if Vertical Glyph, true.
attributed.addAttributes([NSAttributedStringKey.verticalGlyphForm: isVertical], range: NSMakeRange(0, attributed.length))
attributed.addAttribute(NSAttributedStringKey.font, value: UIFont(name: "Hiragino Mincho ProN", size: 27)!, range: NSMakeRange(0, attributed.length))
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineHeightMultiple = 2
paragraphStyle.lineSpacing = 4
attributed.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, (attributed.length)))
drawContext(attributed, textDrawRect: rect, isVertical: isVertical)
}
}
Tableview Klasse
import UIKit
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
let label = cell.viewWithTag(10) as! CustomLabel
let attributedText = Utility.sharedInstance.furigana(String: "|銀行《ぎんこう》と|郵便局《ゆうびんきょく》の|間《あいだ》の|道《みち》をまっすぐ|行《い》くと、|学校《がっこう》の|前《まえ》に|出《で》ます。")
label.attributedText = attributedText
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
//return 70
}
dies ist das Ergebnis:
Vielen Dank für diese Antwort. es ist toll, aber es gibt nur das letzte Problem: wenn man das Gerät im Querformat dreht, wird die Saite gestreckt. Ich habe versucht, ersetzen die uiview mit uilabel und die Methode zeichnen (in :) mit drawText (in :). Arbeitet, aber die Zelle wird nicht skaliert, da sich die Einschränkungen nicht ändern. Wie kann ich dieses letzte Problem beheben? –
@GabrieleQuatela Es tut mir leid, es gab ein Problem im Code, wenn die Anzahl der Daten oder rotierende Ansicht erhöht wurde. Es musste neu gezeichnet werden, wenn die Zelle erneut verwendet oder die Ansicht gedreht wurde. Daher habe ich setNeedsDisplay() hinzugefügt. Wenn sich die Ansicht dreht, rufen Sie tableView.ReloadData() auf. Dies ruft "cellForRowAt indexPath" auf und führt setNeedsDisplay() erneut aus. – yaslam
vielen Dank für Ihre Hilfe. Ich habe das Problem an meine Situation angepasst und es funktioniert. Jetzt habe ich ein Problem mit dem NSMutableParagraphStyle.Wenn ich zB lineHeightMultiple = 1.1 einstelle, wird die Höhe nicht gut berechnet. und Sie können nur eine Zeile sehen. aber vielleicht liegt es daran, dass ich nicht weiß, wie es funktioniert. aber es ist ok. –