简体   繁体   中英

UITextView attributed Alignment and size

First assume math is correct.

let textViewText = NSMutableAttributedString(string: "34\n+ 10\n+ 32344\n= 23424")

Im using a Textview to display input from the user. To make it easier to read I'm trying to get the text format like this

  34 + 10 + 32344 = 23424 

The other issue I'm having is with wrapping. Is there a way to resize each line to fit on its line?

  34 = 23424 4356356 

Is your text dynamic or static? If static, then all you need to do is to put the correct amount of spacing between your numbers and plus signs and then right justify the text.

self.textView.attributedText = NSMutableAttributedString(string: "34\n+      10\n+ 32344\n= 23424")
self.textView.textAlignment = NSTextAlignment.Right

Result:

结果

You can accomplish this by using a right-aligned tab stop in your paragraph style, and separating your operators and values with a tab.

let string = "\t34\n+\t10\n+\t32344\n=\t23424"

let paragraph = NSMutableParagraphStyle()
paragraph.tabStops = [NSTextTab(textAlignment: .Right, location: 200, options: [:])]

let attributedString = NSAttributedString(string: string, attributes:[NSParagraphStyleAttributeName: paragraph])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM