2017-07-21 33 views
0

虽然我发现如何添加缩进到第一行(FirstLineHeadIndent)和其余行(HeadIndent),我找不到如何添加缩进到只前两/三行为了实现这样的事情: long live MS Paint!如何将缩进添加到多行UILabel中?

PS:这不是一个重复的,因为我不如何缩进只有一线,作为一个用户建议。

+0

的[?我怎么能缩进的iOS多的UILabel的只有第一行]可能的复制(https://stackoverflow.com/questions/21430685/how -can-i-indent-only-first-line-of-multiline-uilabel-in-ios) – the4kman

+0

@ the4kman看PS部分 – nicks

+0

看看这个https://stackoverflow.com/questions/39113803/indent-second-line -of-uilabel-swift –

回答

0

您需要使用UILabel.AttributedText的stringattribute属性。

其实际类型的NSMutableAttributedString所以我第一次铸造 label.AttributedText的可变类型,然后我可以操控它。 ‡

要做到这一点,你可以使用如下:

var mutable = Control.AttributedText as NSMutableAttributedString; 
UIStringAttributes uiString=new UIStringAttributes(); 

然后,你需要设置一个缩进第一行(做这个,你已经知道如何的方式),然后你会像下面那样设置它的段落样式的headIndent。

这是从转换目标C所以可能不是完美:&匕首;

NSMutableParagraphStyle paragraphStyle = new NSMutableParagraphStyle(); 
paragraphStyle.headIndent = 14; 

NSDictionary attributes(){ 
    StyleAttributeName = paragraphStyle; 
}; 

mutable.AddAttribute(attributes); 

Control.attributedText = mutable; 

我相信这样的事情,结合你的“FirstLineHeadIndent”代码应该做的伎俩。


‡How to manipulate NSAttributedString

‡Objective-C second line with indent

+0

这不会用于缩进**只有前两行** – nicks

+1

即使它没有,如果我需要缩进三行呢? – nicks

+0

我诚实地说,不要以为你可以简单地缩进任何两行以上的行,而不会有一些相当不合理的属性字符串。 – Digitalsa1nt

4

使用IOS

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]; 

TextKit框架将不包括内部的TextView

的内容
+1

不是一个C#解决方案,但有趣的问题。 =) – Digitalsa1nt

+0

虽然我需要'UILabel'的解决方案。 – nicks

+0

在TextView中禁用用户交互将获得标签。 UILabel是一个只读的UITextView。两者都是相似的。请参阅https://stackoverflow.com/a/6040449/5215474 – Saranjith

1

你需要设置你的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]) 

下面是如何配置的一个例子:

enter image description here

这里是如何配置的缩进:

enter image description here

这里是在模拟器上的例子:

enter image description here

+0

我没有看到我的答案有什么问题?请赐教。 提问者没有指定文本必须根据图片或复选框 –

+0

对齐,因为使用空格缩进不是一个好的解决方案 – nicks

+0

如果不是空间,那么这是什么? – nicks