您需要在Storyboard中将UILabel
文本设置为Attributed
字符串。
然后,您可以编辑每行的缩进,还可以粘贴任何使用文本编辑器创建的文本,并且它将保留其缩进以及其他属性。
当然,您也可以通过编程方式操纵这些属性,以下是一个示例:
@IBOutlet weak var label: UILabel!
let text = "\tfirst line\n \tsecond line\nthird line\nforth line"
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.tabStops = [NSTextTab(textAlignment: NSTextAlignment.left, location: 15, options: [:])]
paragraphStyle.headIndent = 10
label.attributedText = NSAttributedString(string: text, attributes: [NSParagraphStyleAttributeName: paragraphStyle])
这是一个配置示例:
以下是如何配置缩进:
这是模拟器上的示例:
iOS中如何使用TextKit
框架
CGRect checkBoxFrame = [self.textView convertRect:self.checkView.bounds fromView:self.checkView];
checkBoxFrame.origin.x -= self.textView.textContainerInset.left;
checkBoxFrame.origin.y -= self.textView.textContainerInset.top;
UIBezierPath *checkBoxPath = [UIBezierPath bezierPathWithOvalInRect:checkBoxFrame];
self.textView.textContainer.exclusionPaths = @[checkBoxPath];
它将排除TextView内容内部的图像路径。
UILabel
的解决方案。 - nicks您需要使用UILabel.AttributedText的stringattribute属性。
实际上它是NSMutableAttributedString类型,所以我首先将label.AttributedText转换为可变类型,然后才能对其进行操作。‡
要做到这一点,您需要使用以下内容:
var mutable = Control.AttributedText as NSMutableAttributedString;
UIStringAttributes uiString=new UIStringAttributes();
然后您需要在第一行上设置缩进(按照您已经知道的方式进行),然后您将设置其段落样式的headIndent,如下所示。
这是从Objective-C转换而来,因此可能不完美: ‡
NSMutableParagraphStyle paragraphStyle = new NSMutableParagraphStyle();
paragraphStyle.headIndent = 14;
NSDictionary attributes (){
StyleAttributeName = paragraphStyle;
};
mutable.AddAttribute(attributes);
Control.attributedText = mutable;
我相信这样的代码结合你的“FirstLineHeadIndent”代码应该可以解决问题。