目前的代码允许我附上徽标。像这样:
但是我怎么能只调整" Logo"在右边签名?
let paragraph = NSMutableAttributedString()
let font = UIFont(name: "Helvetica Neue", size: 15.0) ?? UIFont.systemFontOfSize(18.0)
let align = NSTextAlignment.Center
let textFont = [
NSFontAttributeName : font]
let attrString1 = NSAttributedString(string: "\n Logo", attributes:textFont)
let attrString2 = NSAttributedString(attributedString: textview.attributedText)
paragraph.appendAttributedString(attrString2)
paragraph.appendAttributedString(attrString1)
let paraStyle = NSMutableParagraphStyle()
paraStyle.alignment = .Center
paraStyle.firstLineHeadIndent = 15.0
paraStyle.paragraphSpacingBefore = 3.0
paragraph.addAttribute(NSParagraphStyleAttributeName, value: paraStyle, range: NSRange(location: 0,length: paragraph.length))
shareTextView.attributedText = paragraph
答案 0 :(得分:1)
如果要为不同部分进行不同的对齐,请为两个段落样式指定不同的范围。因此,您可以执行以下操作:
let centerStyle = NSMutableParagraphStyle()
centerStyle.alignment = .Center
let rightStyle = NSMutableParagraphStyle()
rightStyle.alignment = .Right
let font = UIFont(name: "Helvetica Neue", size: 24.0) ?? UIFont.systemFontOfSize(24.0)
let fontAttributes = [NSFontAttributeName : font]
let attributedText = NSMutableAttributedString(string: "Foo\nBar", attributes: fontAttributes)
attributedText.addAttribute(NSParagraphStyleAttributeName, value: centerStyle, range: NSRange(location: 0, length: 4))
attributedText.addAttribute(NSParagraphStyleAttributeName, value: rightStyle, range: NSRange(location: 4, length: 3))
textView.attributedText = attributedText
或者,更简单,您可以追加具有不同属性的属性字符串:
let font = UIFont(name: "Helvetica Neue", size: 24.0) ?? UIFont.systemFontOfSize(24.0)
let centerAttributes = [NSFontAttributeName : font, NSParagraphStyleAttributeName : centerStyle]
let attributedText = NSMutableAttributedString(string: "Foo\n", attributes: centerAttributes)
let rightAttributes = [NSFontAttributeName : font, NSParagraphStyleAttributeName : rightStyle]
attributedText.appendAttributedString(NSMutableAttributedString(string: "Bar", attributes: rightAttributes))
textView.attributedText = attributedText
产量:
答案 1 :(得分:0)
我相信你必须使两个部分具有不同的对齐方式不同的段落。