UILabel上的剪切文字

时间:2018-01-23 11:58:15

标签: swift uilabel

我为UILabel使用自定义字体,但是有一个奇怪的问题,对于某些字符文本被剪裁:

enter image description here

标签应该是这样的(标签框架已手动放大):

enter image description here

这是将标签添加到灰色视图的代码:

let string = "کیخسروی"
let font = UIFont(name: "IranNastaliq", size: 30)!
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .byWordWrapping
paragraphStyle.alignment = .center

let attributes: [NSAttributedStringKey: Any] = [
    .font: font,
    .paragraphStyle: paragraphStyle
    ]


let mutabbleAttributedString = NSMutableAttributedString(string: string, attributes: attributes)
let rectSize = mutabbleAttributedString.size()
let label = UILabel(frame: CGRect(x: 0, y: 0, width: rectSize.width, height: rectSize.height))
label.font = font
label.attributedText = mutabbleAttributedString
label.backgroundColor = UIColor.yellow
label.textAlignment = .center
let size = CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
let width = label.sizeThatFits(size).width
let height =  label.sizeThatFits(size).height
let frame = CGRect(x: 0, y: 0, width: width, height: height)
label.frame = frame
self.myView.addSubview(label)
label.center = self.myView.center

似乎sizeThatFits()没有正确计算边界。 我不得不提到这种剪辑只在使用一些自定义字体时发生。

0 个答案:

没有答案