5

I am using storyboard to design my UIView. But I've stuck at the task: I have a multiline UILabel, UIView and UIButton. I want to make UILabel to wrap my button - the fist line of the text has a trailing constraint to UIButton another one to it's super view. And if my UILabel has no text I got a view at the bottom of label and I need to make this view trailing constraint to UIButton but if I got a free space - to it's superview. Screenshot example:

enter image description here

I want to jump second line word after 'pyat' . Sorry for my poor english, hope that picture could help to explain my question. Is it possible to make it directly in IB?

Jagveer Singh
  • 2,258
  • 19
  • 34
Artem Z.
  • 1,243
  • 2
  • 14
  • 36
  • 3
    I'd suggest to use a `UITextView` instead of a `UILabel` (with some quick modification, it would look like and act like a `UILabel`) and use `yourTextView.textContainer.exclusionPaths`, but that would be with code and not IB. – Larme Oct 05 '16 at 11:28
  • Sorry , It is not possible with interfacebuilder – Jitendra Modi Oct 05 '16 at 12:11
  • @Jecky Can you advice me how to do this? Obj-c language – Artem Z. Oct 05 '16 at 12:14
  • As above comment, You can easily do with textview and its method but with label you can only find width of label. I will try it ,if possible I will post my code – Jitendra Modi Oct 05 '16 at 12:17
  • @Jecky @Larme the problem using `UITextView` is that sometimes I have zero text in label and I need to wrap this button with `UIView` below my label – Artem Z. Oct 05 '16 at 12:39
  • But as per your screenshot your button is fixed by constraint I think, So If you have text of textview as zero then it is remain same as it were – Jitendra Modi Oct 05 '16 at 12:46
  • @Jecky I mean that if my text is zero - the trailing constraint of the next view should be to UIButton not to super view – Artem Z. Oct 05 '16 at 12:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/124994/discussion-between-jecky-and-artem-z). – Jitendra Modi Oct 05 '16 at 12:56

2 Answers2

1

Perhaps instead of using UILabel you can try UITextView by using its textView.textContainer.exclusionPaths property to define a button container area to exclude.

Have a look at the sample code with a reported issue for selected and editable case.

As I remember you must limit the app deployment target minimum to 7 or later if using this property.

Hope that helps!

Community
  • 1
  • 1
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
0

What you want to do is not supported out of the box and will take a lot of work because it will require subclassing UILabel and overriding drawRect.

An easy solution is to set attributed text on the label and use that to format the specific word to look like a button. UILabel attributed text supports hyperlink like behavior.