ios - How to format text like a blockquote within a UITextView -
i trying familiarize myself coretext
, started making own note taking application.
i have regex in place in conjunction attributedstrings
, trying mimic functionality of stackoverflow
text box typing in now.
i have of common things like:
bold
italics
headers
emphasis blocks
but struggling make block quote / code block.
something breaks own line , creates box goes edge no matter how long text is.
and changes background color
is possible strictly using attributedstrings? i've seen old examples html / css injected hoping accomplish using special nsattributedstringkey
combination.
yes, it's possible. here's sample playground in swift 3:
import uikit import playgroundsupport let richtext = nsmutableattributedstring() let chunk0 = nsattributedstring(string: "but struggling make block quote / code block.\n\n") richtext.append(chunk0) let paragraphstyle = nsmutableparagraphstyle() paragraphstyle.headindent = 20 // note setting paragraphstyle.firstlineheadindent // doesn't use background color first line's // indentation. use tab stop on first line instead. paragraphstyle.tabstops = [nstexttab(textalignment: .left, location: paragraphstyle.headindent, options: [:])] let chunk1 = nsattributedstring(string: "\tsomething breaks own line , creates box goes edge no matter how long text is.\n", attributes: [ nsparagraphstyleattributename: paragraphstyle, nsbackgroundcolorattributename: uicolor.yellow ]) richtext.append(chunk1) let chunk2 = nsattributedstring(string: "\nis possible strictly using attributedstrings?") richtext.append(chunk2) let textview = uitextview(frame: cgrect(x: 0, y: 0, width: 120, height: 400)) textview.backgroundcolor = .white textview.attributedtext = richtext playgroundpage.current.liveview = textview
here's output:
but it's important keep in mind uitextview
less powerful (for layout , styling) web view. if want fancy (like stackoverflow darker line of color along left edge of blockquote box), should generate html , css , use web view.
Comments
Post a Comment