从左边插入UILabel文本

use*_*428 5 uilabel ios swift

我有一个UILabel,我用它来显示多行文字.在显示文本的那一刻,它正好在标签的左侧,看起来不太好.我希望文字从左边稍微插入.

到目前为止这是我的代码:

        if notes.objectAtIndex(indexPath.row) as NSString == "" {
            cell.notesLabel.text = "No notes to display."
            cell.notesLabel.textAlignment = NSTextAlignment.Center
        } else {
            cell.notesLabel.textAlignment = NSTextAlignment.Left
        }
Run Code Online (Sandbox Code Playgroud)

我正在查看一些Objective-C示例,但我无法让它们工作,我真的不认为它们是我想要的.

另外,我试图用不同的标签做同样的事情,在那种情况下,我假设我可以在字符串的末尾添加""(因为它是单行标签)从右边移动它,但我很惊讶地看到这不起作用?

谢谢.

rde*_*mar 11

要从左边缘插入文本,您应该创建一个UILabel子类,并覆盖drawTextInRect:,

class RDLabel: UILabel {

    override func drawTextInRect(rect: CGRect) {
        let newRect = CGRectOffset(rect, 10, 0) // move text 10 points to the right
        super.drawTextInRect(newRect)
    }
}
Run Code Online (Sandbox Code Playgroud)