2017-01-31 15 views
1

私はUITextViewを含むView Controllerを持っています。このUITextViewは、テキストを制御するデータモデル(およそ30種類のテキストオプション)に応じてプログラムによって更新されたテキストを取得します。 UITextViewのコンテンツは、常にそれぞれ約450語のかなり長いものになります。段落の範囲は2〜5です。UITextViewに段落見出しをプログラムで追加する

すべてが完璧です。私は段落を分割するための視覚的な方法が必要です。今は、\ n \ nを使用して段落の間に2つの改行を作成しています。これが機能する間、私は最終的に見出しを必要とします。

IBでは、テキストプロパティをPlainからAttributedに変更しました。これにより、XcodeのWYSIWYG風のテキストを修正することができます。素晴らしいですが、私はIBを使ってテキストを変更していません。

私はWebView(以下のコード)を使ってみました。私は、H1タグとPタグを持つ非常に基本的なHTMLファイルを作成しました。残念ながら私のプロジェクトでは30以上のHTMLファイルが作成されます。私は管理性の観点からはこれが嫌いです。テキストはまた、WebViewの(私はこの問題を軽減するためにCSS [-webkit-ユーザーの選択]に頼る必要はありません。)

let path: String? = Bundle.main.path(forResource: "myHTML", ofType: "html") 
    let file = try? String(contentsOfFile: path!, encoding: String.Encoding.utf8) 
    let baseURL = URL(fileURLWithPath: Bundle.main.bundlePath) 
    self.myWebView.loadHTMLString(file!, baseURL: baseURL) 

私の理想的なソリューションである、選択することができません

私の現在の設定に固執することはできますか?プログラムで、データモデルでUITextViewテキストを変更すると、段落見出しを作成するためにコード内のテキスト属性を変更することができますか?

This is my current UI without any attributed text This is my ultimate end-goal

編集 - これはMVC

allFormattedDescriptions: [ 
Formatted(heading: "heading 1", descriptionText: "Lorem Ipsum Paragraph 1"), 
Formatted(heading: "heading 2", descriptionText: "Lorem Ipsum Paragraph 2"), 
Formatted(heading: "heading 3", descriptionText: "Lorem Ipsum Paragraph 3") 
] 
// Ideal formatting; every paragraph will have a heading. Can handle that with one object that requires both a heading and description text (paragraph). 

struct Formatted { 

var heading: String! 
var descriptionText: String! 

var bodyParagraphStyle: NSMutableParagraphStyle = { 
    let style = NSMutableParagraphStyle() 
    style.lineSpacing = 10 
    style.paragraphSpacingBefore = 6 
    style.paragraphSpacing = 6 
    return style 
}() 

var headerParagraphStyle: NSMutableParagraphStyle = { 
    let style = NSMutableParagraphStyle() 
    style.paragraphSpacingBefore = 24 
    return style 
}() 

var bodyAttributes: [String: AnyObject]! 
var headerAttributes: [String: AnyObject]! 

} 
+0

はテキストビューの編集可能ですか? – AdamPro13

+0

あなたは間違いなくコードでそれを行うことができます。 NSMutableAttributedStringを使用すると、自由に属性を追加したり削除したりできます。また、HTMLを表示するために使用することもできます。 HTMLを表示する必要がない場合は、 'enumerateAttributesInRange'を呼び出してすべての属性を取得して変更するか、' addAttributes'または 'setAttributes'を呼び出して様々なスタイルを設定することができます。これには、段落のスタイルとインデント、行間、色、下線、取り消し線、フォントなどが含まれます。 – Brandon

+0

私はあなたが望むものを手に入れません。モックアップを提供できますか? –

答えて

3

使用NS(Mutable)ParagraphStyleNS(Mutable)AttributedStringで動作するように取得しようとしています。たとえば:

class ViewController: UIViewController { 
    @IBOutlet weak var textView: UITextView! 

    var allFormattedDescriptions = [ 
     Formatted(heading: "Introduction to Bacon Ipsum", descriptionText: "Bacon ipsum dolor amet jerky pig pastrami capicola biltong turkey, ball tip fatback andouille porchetta flank swine brisket bacon pork loin. Tongue shank cupim, pastrami spare ribs meatball drumstick pork pork chop. Sirloin flank tenderloin bresaola doner, cupim ribeye drumstick ham hock t-bone pork short ribs shoulder. Fatback ribeye pastrami pancetta, chuck turkey andouille boudin burgdoggen shoulder tongue kielbasa doner shankle turducken. Rump strip steak drumstick, shankle cupim prosciutto jerky bacon doner. Pork chop jowl burgdoggen, cow turkey ball tip doner. Cow ham meatball chuck flank meatloaf prosciutto."), 
     Formatted(heading: "Kielbasa?", descriptionText: "Spare ribs boudin ham leberkas landjaeger filet mignon. Short loin fatback hamburger leberkas chicken. Frankfurter chuck short ribs ball tip, ground round cupim shank brisket venison turducken boudin. Pig sirloin pork loin meatloaf short loin turkey swine.") 
    ] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let textContent = NSMutableAttributedString() 
     for (index, desc) in allFormattedDescriptions.enumerated() { 
      let includeLinebreak = index < allFormattedDescriptions.count - 1 
      textContent.append(desc.attributeString(includeLineBreak: includeLinebreak)) 
     } 
     textView.attributedText = textContent 
    } 
} 

struct Formatted { 
    var heading: String 
    var descriptionText: String 

    var bodyParagraphStyle: NSMutableParagraphStyle = { 
     let style = NSMutableParagraphStyle() 
     style.lineSpacing = 10 
     style.paragraphSpacingBefore = 6 
     style.paragraphSpacing = 6 
     return style 
    }() 

    var headerParagraphStyle: NSMutableParagraphStyle = { 
     let style = NSMutableParagraphStyle() 
     style.paragraphSpacingBefore = 24 
     return style 
    }() 

    var bodyAttributes: [String: AnyObject] 
    var headerAttributes: [String: AnyObject] 

    func attributeString(includeLineBreak: Bool = true) -> NSAttributedString { 
     let result = NSMutableAttributedString() 
     result.append(NSAttributedString(string: self.heading + "\n", attributes: self.headerAttributes)) 
     result.append(NSAttributedString(string: self.descriptionText, attributes: self.bodyAttributes)) 
     if includeLineBreak { 
      result.append(NSAttributedString(string: "\n", attributes: self.bodyAttributes)) 
     } 

     return result as NSAttributedString 
    } 

    init(heading: String, descriptionText: String) { 
     self.heading = heading 
     self.descriptionText = descriptionText 
     self.bodyAttributes = [ 
      NSFontAttributeName: UIFont(name: "Hoefler Text", size: 14)!, 
      NSParagraphStyleAttributeName: bodyParagraphStyle 
     ] 
     self.headerAttributes = [ 
      NSFontAttributeName: UIFont(name: "Avenir", size: 22)!, 
      NSParagraphStyleAttributeName: headerParagraphStyle, 
      NSForegroundColorAttributeName: UIColor.red 
     ] 
    } 
} 

結果:

Formatted text

+1

これはとてもいいです。私はあなたがどのように本文と段落の属性を分けたのが好きです。私はこれをMVCに分解しようとしていますが、厳しい状況にあります。私はモデルが見出しと段落のテキスト入力を処理するようにしたい。元の投稿には、私が頭を下ろそうとしている経路を示す小さな編集があります。それ以上のアドバイスは非常に感謝されるでしょう。 – Joe

+0

私の編集した答えを見る –

+1

Mmmmmm、Kielbasa。 – matt

関連する問題