我希望在UITextView
内部绘制一条可自定义的行,其中包含一些文字(使用NSAttributedString
)
这是我试过的
NSString *unicodeStr = [NSString stringWithFormat:@"%C%C%C", 0x00A0, 0x0009, 0x00A0]; //nbsp, tab, nbsp
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:unicodeStr];
NSRange strRange = NSMakeRange(0, str.length);
NSMutableParagraphStyle *const tabStyle = [[NSMutableParagraphStyle alloc] init];
tabStyle.headIndent = 16; //padding on left and right edges
tabStyle.firstLineHeadIndent = 16;
tabStyle.tailIndent = -16;
NSTextTab *listTab = [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentCenter location:40 options:@{}]; //this is how long I want the line to be
tabStyle.tabStops = @[listTab];
[str addAttribute:NSParagraphStyleAttributeName value:tabStyle range:strRange];
[str addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:2] range:strRange];
但无论我为制表位置(本例中为40)和tailIndent(此处为-16)提供的值,该行仅尊重headIndent并跨越整个UITextView宽度(当然减去headIndent)。 / p>
编辑 - 我很确定问题是因为我没有使用正确的unicode字符(尽管它们似乎是合乎逻辑的选择)。如果这给某人一个提示,如果我在第二个之后添加一个空格,即向末尾添加一个空格,则该标签仅限于一个标签长度
答案 0 :(得分:2)
这是你的预期结果吗?
你可以试试这个:
WHERE d.ID = @DeptID
这是完整的代码:
NSTextTab *listTab = [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentCenter location:self.textView.frame.size.width - tabStyle.firstLineHeadIndent + tabStyle.tailIndent options:@{}];
答案 1 :(得分:0)
以下是我为解决同一问题所做的工作。它使用NSTextTab
的子类:
import UIKit
class RightAnchoredTextTab : NSTextTab {
weak var textContainer : NSTextContainer!
required init(textAlignment alignment: NSTextAlignment, location loc: CGFloat, options: [String : AnyObject], textContainer aTextContainer : NSTextContainer) {
super.init(textAlignment: alignment, location: loc, options: options)
textContainer = aTextContainer
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override var location: CGFloat {
get {
return textContainer.size.width-textContainer.lineFragmentPadding*2-super.location
}
}
}
类似于其他解决方案的位:
func horizontalLine(indent : CGFloat = 30, width : CGFloat = 1) -> NSAttributedString
{
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.tabStops = []
paragraphStyle.addTabStop(NSTextTab(textAlignment: .Left, location: indent, options: [:]))
paragraphStyle.addTabStop(RightAnchoredTextTab(textAlignment: .Right, location: indent, options: [:], textContainer : textView.textContainer))
paragraphStyle.alignment = .Left
let attributes = [NSParagraphStyleAttributeName : paragraphStyle, NSStrikethroughStyleAttributeName : width]
textView.backgroundColor = UIColor.yellowColor()
let ZeroWidthNonBreakingSpace = "\u{FEFF}"
return NSAttributedString(string: "\t\(ZeroWidthNonBreakingSpace)\t\(ZeroWidthNonBreakingSpace)\n", attributes: attributes)
}
一些注意事项:
NSTextContainer
有一个lineFragmentPadding
。如果你们都想知道为什么你必须将右对齐标签缩进10,那就是:默认值为5. \n
意味着没有删除线。不知道为什么。UITextView
再次调用NSTextTab.location
。答案 2 :(得分:0)
快速5,
let unicodeStr = "\n\u{00a0}\t\t\n"
let str = NSMutableAttributedString(string: unicodeStr)
let strRange = NSRange(location: 0, length: str.length)
let tabStyle = NSMutableParagraphStyle()
tabStyle.headIndent = 0 //padding on left and right edges
tabStyle.firstLineHeadIndent = 0
//tabStyle.tailIndent = -16
let listTab = NSTextTab(textAlignment: .center, location: textView.frame.size.width-10, options: [:]) //this is how long I want the line to be
tabStyle.tabStops = [listTab]
str.addAttribute(.paragraphStyle, value: tabStyle, range: strRange)
str.addAttribute(.strikethroughStyle, value: NSNumber(value: 2), range: strRange)
str.addAttribute(.strikethroughColor, value: UIColor.label, range: strRange)
str.addAttribute(NSAttributedString.Key.font, value: self.currentFont, range: strRange)
str.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.label, range: strRange)
textView.textStorage.append(str)
//To set cursor end of document
let endPosition = self.textView.endOfDocument
self.textView.selectedTextRange = self.textView.textRange(from: endPosition, to: endPosition)