2016-03-11 36 views
7

我想画一个UITextView包括一些文本的内部可定制线(使用NSAttributedString绘制一个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)。

编辑 - 我很确定这个问题是因为我没有使用正确的Unicode字符(虽然他们似乎是合乎逻辑的选择)。如果这给了某人一个提示,如果我在第二个nbsp之后添加一个空格,即结束,则该选项卡被限制为单个标签长度

+0

你能提供一个你想让文字看起来像什么的例子吗? –

+0

我试图实施hr标签(水平线)。从html转换时,它们不受NSAttributedString支持 – lostInTransit

回答

2

它是您的预期结果吗?

enter image description here enter image description here

你可以试试这个:

NSTextTab *listTab = [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentCenter location:self.textView.frame.size.width - tabStyle.firstLineHeadIndent + tabStyle.tailIndent options:@{}]; 

这是全码:

- (void) viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
    NSString *unicodeStr = @"\n\u00a0\t\t\n"; 
    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 = -70; 
    NSTextTab *listTab = [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentCenter location:self.textView.frame.size.width - tabStyle.headIndent + tabStyle.tailIndent 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]; 

    NSAttributedString *htmlStr = [[NSAttributedString alloc] initWithData:[@"<h1>Lorem ipsum dolor sit er elit lamet</h1>" dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil]; 

    [str insertAttributedString:htmlStr atIndex:0]; 
    [str appendAttributedString:htmlStr]; 

    self.textView.attributedText = str; 
} 
+0

已经完成,没有工作。我很确定它与选项卡两侧的nbsp有关,但我不明白为什么 – lostInTransit

+0

这是我想要的 - 是的。但我有一些前后文本(html),似乎搞乱了横向规则(我猜?)。代码似乎与我所拥有的不一样。不知道为什么它不适合我。 – lostInTransit

+0

,似乎工作!谢谢 – lostInTransit

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时,边界被改变。