2016-03-11 15 views
7

をNSAttributedString私は(NSAttributedStringを使用して)いくつかのテキストからなるUITextView内のカスタマイズ可能なラインを描きたいUITextView内の線を引く -

は、ここで私は

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

しかし、何があっ値Iを試みたものですタブの停止位置(この場合は40)とtailIndent(ここでは-16)を指定すると、行はheadIndentを尊重し、UITextViewの幅全体(もちろんheadIndentを引いた値)に渡ります。

EDIT - 正しいユニコード文字を使用していないため、問題があると確信しています(論理的な選択肢ですが)。これにより誰かにヒントが与えられる場合、2番目のnbspの後、つまり末尾にスペースを追加すると、タブは1つのタブの長さに制限されます

+0

テキストをどのように表示するかの例を挙げることはできますか? –

+0

私はhrタグ(水平ルール)を実装しようとしています。それらは、htmlの – 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

から変換するとき、NSAttributedStringによってサポートされていません。既に動作しませんでした。私はそれがタブの両側にある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でテキストを前置何取り消し線がなかったもので。理由は分かりません。
  • この解決策は、境界が変更されたときにUITextViewNSTextTab.locationを再度呼び出すため、回転などで機能します。
関連する問題