1

How can you access specific lines of a UITextView? If the text view is 10 lines long and I wanted to change the style properties of the first two lines how could I do that?

Response to duplicate mark: Question is not a duplicate. Moderator you directed to an Objective-C tag.

Max Phillips
  • 6,991
  • 9
  • 44
  • 71
  • Is it one continuous string that wraps or are the 10 lines separates by new lines? – lucianomarisi Sep 19 '15 at 22:05
  • One consecutive string. – Max Phillips Sep 19 '15 at 22:09
  • @lucianomarisi Even if I could just access the index of the first and last characters on a line I think I could use `NSAttributedString` to accomplish what I'm looking for. – Max Phillips Sep 19 '15 at 22:20
  • @matt What you pointed to is obj-c, not Swift – Max Phillips Sep 20 '15 at 01:51
  • That's as may be, but you can surely translate. Don't let a little thing like language stop you. The point is, that's how you find where the line wraps - you use the NSLayoutManager. – matt Sep 20 '15 at 02:38
  • @matt it has been 2 years since, I needed this, and I need it in Swift. Please think more long term. 100s more people will need this answer in swift over the coming years. – demiculus Jun 15 '17 at 09:20
  • 1
    @demiculus i have added a swift translation here: https://stackoverflow.com/a/56964242/10708998 – brontea Jul 10 '19 at 06:05

1 Answers1

0

To get access to the UITextView content while user is typing you have to call func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool. Here you can take care of what user is typing.

Then to access a particular line you can call the code you see in this question and then .

Community
  • 1
  • 1
adolfosrs
  • 9,286
  • 5
  • 39
  • 67
  • I wrote the wrong thing. I am using a UITextView. Do you know how to do it with a text view? – Max Phillips Sep 20 '15 at 00:45
  • My issue is that I'm not trying to handle user interaction. I'm trying to style text as it's put into the container. I want to make the first three lines a certain color, the next three lines a certain color, and so on. – Max Phillips Sep 20 '15 at 00:47